Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Workflow for Codecov example-node
on: [push, pull_request]
jobs:
run:
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install dependencies
run: npm install
- name: Run tests and collect coverage
run: npm test
- name: Upload test results to Codecov
if: ${{ !cancelled() }}
uses: codecov/test-results-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
- name: Upload coverage to Codecov (arg token)
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
verbose: true
- name: Upload coverage to Codecov (oidc)
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
use_oidc: true
verbose: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
coverage
junit.xml
12 changes: 0 additions & 12 deletions .travis.yml

This file was deleted.

234 changes: 13 additions & 221 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,232 +1,24 @@
# [Codecov][0] NodeJS / Javascript Example
# [Codecov](https://codecov.io) NodeJS / JavaScript Example
[![codecov](https://codecov.io/github/codecov/example-node/branch/main/graph/badge.svg)](https://app.codecov.io/github/codecov/example-node)
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node?ref=badge_shield)

This example repository shows how Codecov can be integrated with a simple Node.js project. It uses **GitHub Actions** as the CI/CD provider and the built-in [Node.js test runner](https://nodejs.org/api/test.html) with [`c8`](https://github.com/bcoe/c8) as the coverage provider.

## Guide
## Running locally

You can install Codecov by adding
```sh
npm install -g codecov
npm install
npm test
```

### Travis Setup

Add the following to your `.travis.yml`:

```yml
language:
node_js
before_install:
- npm install -g codecov
script:
- istanbul cover ./node_modules/mocha/bin/_mocha --reporter lcovonly -- -R spec
- codecov

```

The first script line will change depending on your coverage collecting tool, see below.
### Produce Coverage Reports
### [Mocha](http://mochajs.org/) + [Blanket.js](https://github.com/alex-seville/blanket)
- Install [blanket.js](http://blanketjs.org/)
- Configure blanket according to [docs](https://github.com/alex-seville/blanket/blob/master/docs/getting_started_node.md).
- Run your tests with a command like this:

```sh
NODE_ENV=test YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha \
--require blanket \
--reporter mocha-lcov-reporter
codecov
```
### [Mocha](http://mochajs.org/) + [JSCoverage](https://github.com/fishbar/jscoverage)

Instrumenting your app for coverage is probably harder than it needs to be (read [here](http://www.seejohncode.com/2012/03/13/setting-up-mocha-jscoverage/)), but that's also a necessary step.

In mocha, if you've got your code instrumented for coverage, the command for a travis build would look something like this:
```sh
YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha test -R mocha-lcov-reporter
```

### [Istanbul](https://istanbul.js.org/)

**With Mocha:**

```sh
nyc --reporter=lcov mocha && codecov
```

**With Jasmine:**

```sh
istanbul cover ./node_modules/jasmine/bin/jasmine.js
```

***With Karma:***

The `lcov.info` can be used as in other configurations. Some projects experienced better results using `json` output but it is no longer enabled by default. In `karma.config.js` both can be enabled:

```javascript
module.exports = function karmaConfig (config) {
config.set({
...
reporters: [
...
// Reference: https://github.com/karma-runner/karma-coverage
// Output code coverage files
'coverage'
],
// Configure code coverage reporter
coverageReporter: {
reporters: [
// generates ./coverage/lcov.info
{type:'lcovonly', subdir: '.'},
// generates ./coverage/coverage-final.json
{type:'json', subdir: '.'},
]
},
...
});
};
```

In `package.json` supply either `lcov.info` or `coverage-final.json` to `codecov`:

```javascript
{
"scripts": {
"report-coverage": "codecov",
...
}
...
}
```

### [Nodeunit](https://github.com/caolan/nodeunit) + [JSCoverage](https://github.com/fishbar/jscoverage)

Depend on nodeunit and jscoverage:

```sh
npm install nodeunit jscoverage codecov --save-dev
```

Add a codecov script to "scripts" in your `package.json`:

```javascript
"scripts": {
"test": "nodeunit test",
"codecov": "jscoverage lib && YOURPACKAGE_COVERAGE=1 nodeunit --reporter=lcov test && codecov"
}
```

Ensure your app requires instrumented code when `process.env.YOURPACKAGE_COVERAGE` variable is defined.

Run your tests with a command like this:

```sh
npm run codecov
```

### [GitHub Actions](https://github.com/codecov/codecov-action)

```yaml
- name: Codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: unittests
```

### [Poncho](https://github.com/deepsweet/poncho)
Client-side JS code coverage using [PhantomJS](https://github.com/ariya/phantomjs), [Mocha](http://mochajs.org/) and [Blanket](https://github.com/alex-seville/blanket):
- [Configure](http://mochajs.org/#running-mocha-in-the-browser) Mocha for browser
- [Mark](https://github.com/deepsweet/poncho#usage) target script(s) with `data-cover` html-attribute
- Run your tests with a command like this:

```sh
./node_modules/.bin/poncho -R lcov test/test.html && codecov
```

### [Lab](https://github.com/hapijs/lab)
```sh
istanbul cover ./node_modules/lab/bin/lab --report lcovonly -- -l && codecov
```

### [nyc](https://github.com/bcoe/nyc)
```javascript
{
"scripts": {
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
...
}
...
}
```

### [Jest](https://facebook.github.io/jest/)
Run
```sh
jest --ci --coverage && codecov
```

or you can add it in your package.json:
```javascript
"jest": {
"coverageDirectory": "./coverage/",
"collectCoverage": true
}
```
Jest will now generate coverage files into `coverage/` and you can run your tests with a command like this:
```sh
jest && codecov
```

### JSX
There have been reports of [gotwarlost/istanbul](https://github.com/gotwarlost/istanbul) not working properly with JSX files, which provide inaccurate coverage results. Please try using [ambitioninc/babel-istanbul](https://github.com/ambitioninc/babel-istanbul).

### [Tape](https://github.com/substack/tape)

```
istanbul cover node_modules/.bin/tape ./test/*.js
```

Or simply run:

```
istanbul cover ./test/*.js
```

Or add in package.json:

```
"test": "istanbul cover test/*.js",
```

After test:

```
codecov --token=:token
```

## Caveats
#### Private Repo
Repository tokens are required for (a) all private repos, (b) public repos not using Travis-CI, CircleCI or AppVeyor. Find your repository token at Codecov and provide via `codecov --token=:token` or `export CODECOV_TOKEN=":token"`

## Common Pitfalls

### [`mock-fs`](https://github.com/tschaub/mock-fs)
When using `mock-fs` make sure to run `mock.restore()` when your tests are done running, or else the reports wont get generated on the CI.

## Support

### FAQ
- Q: Is there a TypeScript example?<br/>A: Yes [codecov/example-typescript](https://github.com/codecov/example-typescript).

1. More documentation at https://docs.codecov.io
2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml

[0]: https://codecov.io/

We are happy to help if you have any questions. Please contact email our Support at [support@codecov.io](mailto:support@codecov.io)
This runs the test suite and writes an `lcov` coverage report to `coverage/` along with a JUnit report at `junit.xml`.

## Links
- [Quick Start](https://docs.codecov.com/docs/quick-start)
- [GitHub Tutorial](https://docs.codecov.com/docs/github-tutorial)
- [Community Boards](https://community.codecov.io)
- [Support](https://codecov.io/support)
- [Documentation](https://docs.codecov.io)

## License
[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fcodecov%2Fexample-node?ref=badge_large)
22 changes: 22 additions & 0 deletions app/calculator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Calculator {
static add(x, y) {
return x + y;
}

static subtract(x, y) {
return x - y;
}

static multiply(x, y) {
return x * y;
}

static divide(x, y) {
if (y === 0) {
return 'Cannot divide by 0';
}
return (x * 1.0) / y;
}
}

module.exports = Calculator;
34 changes: 34 additions & 0 deletions app/calculator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const test = require('node:test');
const assert = require('node:assert');
const Calculator = require('./calculator');

test('add', () => {
assert.strictEqual(Calculator.add(1, 2), 3.0);
assert.strictEqual(Calculator.add(1.0, 2.0), 3.0);
assert.strictEqual(Calculator.add(0, 2.0), 2.0);
assert.strictEqual(Calculator.add(2.0, 0), 2.0);
assert.strictEqual(Calculator.add(-4, 2.0), -2.0);
});

test('subtract', () => {
assert.strictEqual(Calculator.subtract(1, 2), -1.0);
assert.strictEqual(Calculator.subtract(2, 1), 1.0);
assert.strictEqual(Calculator.subtract(1.0, 2.0), -1.0);
assert.strictEqual(Calculator.subtract(0, 2.0), -2.0);
assert.strictEqual(Calculator.subtract(2.0, 0.0), 2.0);
assert.strictEqual(Calculator.subtract(-4, 2.0), -6.0);
});

test('multiply', () => {
assert.strictEqual(Calculator.multiply(1, 2), 2.0);
assert.strictEqual(Calculator.multiply(1.0, 2.0), 2.0);
assert.strictEqual(Calculator.multiply(0, 2.0), 0.0);
assert.strictEqual(Calculator.multiply(2.0, 0.0), 0.0);
assert.strictEqual(Calculator.multiply(-4, 2.0), -8.0);
});

test('divide', () => {
assert.strictEqual(Calculator.divide(1.0, 2.0), 0.5);
assert.strictEqual(Calculator.divide(0, 2.0), 0);
assert.strictEqual(Calculator.divide(-4, 2.0), -2.0);
});
8 changes: 8 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
flag_management:
individual_flags:
- name: smart-tests
carryforward: true
carryforward_mode: "labels"
statuses:
- type: "project"
- type: "patch"
15 changes: 0 additions & 15 deletions istanbul-mocha/lib/script.js

This file was deleted.

Loading
Loading