Skip to content

Commit b9e6737

Browse files
Restructure example-node into a single calculator app (#53)
Modernize the repository to mirror example-python: a single calculator app under app/ with runnable tests, using Node's built-in test runner and c8 for coverage. Removes the outdated istanbul-mocha tree and the deprecated codecov uploader (which was the source of all 31 Dependabot alerts), drops Travis CI in favor of GitHub Actions, and adds npm/github-actions Dependabot config. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 30d57f6 commit b9e6737

14 files changed

Lines changed: 1216 additions & 980 deletions

File tree

.github/dependabot.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: npm
4+
directory: "/"
5+
schedule:
6+
interval: daily
7+
open-pull-requests-limit: 10
8+
- package-ecosystem: github-actions
9+
directory: "/"
10+
schedule:
11+
interval: daily
12+
open-pull-requests-limit: 10

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Workflow for Codecov example-node
2+
on: [push, pull_request]
3+
jobs:
4+
run:
5+
runs-on: ubuntu-latest
6+
permissions:
7+
id-token: write
8+
steps:
9+
- name: Checkout
10+
uses: actions/checkout@v4
11+
- name: Set up Node.js
12+
uses: actions/setup-node@v4
13+
with:
14+
node-version: '20'
15+
- name: Install dependencies
16+
run: npm install
17+
- name: Run tests and collect coverage
18+
run: npm test
19+
- name: Upload test results to Codecov
20+
if: ${{ !cancelled() }}
21+
uses: codecov/test-results-action@v1
22+
with:
23+
token: ${{ secrets.CODECOV_TOKEN }}
24+
- name: Upload coverage to Codecov (arg token)
25+
uses: codecov/codecov-action@v4
26+
with:
27+
fail_ci_if_error: true
28+
token: ${{ secrets.CODECOV_TOKEN }}
29+
verbose: true
30+
- name: Upload coverage to Codecov (oidc)
31+
uses: codecov/codecov-action@v4
32+
with:
33+
fail_ci_if_error: true
34+
use_oidc: true
35+
verbose: true

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.DS_Store
33
coverage
4+
junit.xml

.travis.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

README.md

Lines changed: 13 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1,232 +1,24 @@
1-
# [Codecov][0] NodeJS / Javascript Example
1+
# [Codecov](https://codecov.io) NodeJS / JavaScript Example
2+
[![codecov](https://codecov.io/github/codecov/example-node/branch/main/graph/badge.svg)](https://app.codecov.io/github/codecov/example-node)
23
[![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)
34

5+
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.
46

5-
## Guide
7+
## Running locally
68

7-
You can install Codecov by adding
89
```sh
9-
npm install -g codecov
10+
npm install
11+
npm test
1012
```
1113

12-
### Travis Setup
13-
14-
Add the following to your `.travis.yml`:
15-
16-
```yml
17-
language:
18-
node_js
19-
before_install:
20-
- npm install -g codecov
21-
script:
22-
- istanbul cover ./node_modules/mocha/bin/_mocha --reporter lcovonly -- -R spec
23-
- codecov
24-
25-
```
26-
27-
The first script line will change depending on your coverage collecting tool, see below.
28-
### Produce Coverage Reports
29-
### [Mocha](http://mochajs.org/) + [Blanket.js](https://github.com/alex-seville/blanket)
30-
- Install [blanket.js](http://blanketjs.org/)
31-
- Configure blanket according to [docs](https://github.com/alex-seville/blanket/blob/master/docs/getting_started_node.md).
32-
- Run your tests with a command like this:
33-
34-
```sh
35-
NODE_ENV=test YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha \
36-
--require blanket \
37-
--reporter mocha-lcov-reporter
38-
codecov
39-
```
40-
### [Mocha](http://mochajs.org/) + [JSCoverage](https://github.com/fishbar/jscoverage)
41-
42-
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.
43-
44-
In mocha, if you've got your code instrumented for coverage, the command for a travis build would look something like this:
45-
```sh
46-
YOURPACKAGE_COVERAGE=1 ./node_modules/.bin/mocha test -R mocha-lcov-reporter
47-
```
48-
49-
### [Istanbul](https://istanbul.js.org/)
50-
51-
**With Mocha:**
52-
53-
```sh
54-
nyc --reporter=lcov mocha && codecov
55-
```
56-
57-
**With Jasmine:**
58-
59-
```sh
60-
istanbul cover ./node_modules/jasmine/bin/jasmine.js
61-
```
62-
63-
***With Karma:***
64-
65-
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:
66-
67-
```javascript
68-
module.exports = function karmaConfig (config) {
69-
config.set({
70-
...
71-
reporters: [
72-
...
73-
// Reference: https://github.com/karma-runner/karma-coverage
74-
// Output code coverage files
75-
'coverage'
76-
],
77-
// Configure code coverage reporter
78-
coverageReporter: {
79-
reporters: [
80-
// generates ./coverage/lcov.info
81-
{type:'lcovonly', subdir: '.'},
82-
// generates ./coverage/coverage-final.json
83-
{type:'json', subdir: '.'},
84-
]
85-
},
86-
...
87-
});
88-
};
89-
```
90-
91-
In `package.json` supply either `lcov.info` or `coverage-final.json` to `codecov`:
92-
93-
```javascript
94-
{
95-
"scripts": {
96-
"report-coverage": "codecov",
97-
...
98-
}
99-
...
100-
}
101-
```
102-
103-
### [Nodeunit](https://github.com/caolan/nodeunit) + [JSCoverage](https://github.com/fishbar/jscoverage)
104-
105-
Depend on nodeunit and jscoverage:
106-
107-
```sh
108-
npm install nodeunit jscoverage codecov --save-dev
109-
```
110-
111-
Add a codecov script to "scripts" in your `package.json`:
112-
113-
```javascript
114-
"scripts": {
115-
"test": "nodeunit test",
116-
"codecov": "jscoverage lib && YOURPACKAGE_COVERAGE=1 nodeunit --reporter=lcov test && codecov"
117-
}
118-
```
119-
120-
Ensure your app requires instrumented code when `process.env.YOURPACKAGE_COVERAGE` variable is defined.
121-
122-
Run your tests with a command like this:
123-
124-
```sh
125-
npm run codecov
126-
```
127-
128-
### [GitHub Actions](https://github.com/codecov/codecov-action)
129-
130-
```yaml
131-
- name: Codecov
132-
uses: codecov/codecov-action@v2
133-
with:
134-
token: ${{ secrets.CODECOV_TOKEN }}
135-
flags: unittests
136-
```
137-
138-
### [Poncho](https://github.com/deepsweet/poncho)
139-
Client-side JS code coverage using [PhantomJS](https://github.com/ariya/phantomjs), [Mocha](http://mochajs.org/) and [Blanket](https://github.com/alex-seville/blanket):
140-
- [Configure](http://mochajs.org/#running-mocha-in-the-browser) Mocha for browser
141-
- [Mark](https://github.com/deepsweet/poncho#usage) target script(s) with `data-cover` html-attribute
142-
- Run your tests with a command like this:
143-
144-
```sh
145-
./node_modules/.bin/poncho -R lcov test/test.html && codecov
146-
```
147-
148-
### [Lab](https://github.com/hapijs/lab)
149-
```sh
150-
istanbul cover ./node_modules/lab/bin/lab --report lcovonly -- -l && codecov
151-
```
152-
153-
### [nyc](https://github.com/bcoe/nyc)
154-
```javascript
155-
{
156-
"scripts": {
157-
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
158-
...
159-
}
160-
...
161-
}
162-
```
163-
164-
### [Jest](https://facebook.github.io/jest/)
165-
Run
166-
```sh
167-
jest --ci --coverage && codecov
168-
```
169-
170-
or you can add it in your package.json:
171-
```javascript
172-
"jest": {
173-
"coverageDirectory": "./coverage/",
174-
"collectCoverage": true
175-
}
176-
```
177-
Jest will now generate coverage files into `coverage/` and you can run your tests with a command like this:
178-
```sh
179-
jest && codecov
180-
```
181-
182-
### JSX
183-
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).
184-
185-
### [Tape](https://github.com/substack/tape)
186-
187-
```
188-
istanbul cover node_modules/.bin/tape ./test/*.js
189-
```
190-
191-
Or simply run:
192-
193-
```
194-
istanbul cover ./test/*.js
195-
```
196-
197-
Or add in package.json:
198-
199-
```
200-
"test": "istanbul cover test/*.js",
201-
```
202-
203-
After test:
204-
205-
```
206-
codecov --token=:token
207-
```
208-
209-
## Caveats
210-
#### Private Repo
211-
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"`
212-
213-
## Common Pitfalls
214-
215-
### [`mock-fs`](https://github.com/tschaub/mock-fs)
216-
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.
217-
218-
## Support
219-
220-
### FAQ
221-
- Q: Is there a TypeScript example?<br/>A: Yes [codecov/example-typescript](https://github.com/codecov/example-typescript).
222-
223-
1. More documentation at https://docs.codecov.io
224-
2. Configure codecov through the `codecov.yml` https://docs.codecov.io/docs/codecov-yaml
225-
226-
[0]: https://codecov.io/
227-
228-
We are happy to help if you have any questions. Please contact email our Support at [support@codecov.io](mailto:support@codecov.io)
14+
This runs the test suite and writes an `lcov` coverage report to `coverage/` along with a JUnit report at `junit.xml`.
22915

16+
## Links
17+
- [Quick Start](https://docs.codecov.com/docs/quick-start)
18+
- [GitHub Tutorial](https://docs.codecov.com/docs/github-tutorial)
19+
- [Community Boards](https://community.codecov.io)
20+
- [Support](https://codecov.io/support)
21+
- [Documentation](https://docs.codecov.io)
23022

23123
## License
23224
[![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)

app/calculator.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Calculator {
2+
static add(x, y) {
3+
return x + y;
4+
}
5+
6+
static subtract(x, y) {
7+
return x - y;
8+
}
9+
10+
static multiply(x, y) {
11+
return x * y;
12+
}
13+
14+
static divide(x, y) {
15+
if (y === 0) {
16+
return 'Cannot divide by 0';
17+
}
18+
return (x * 1.0) / y;
19+
}
20+
}
21+
22+
module.exports = Calculator;

app/calculator.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const Calculator = require('./calculator');
4+
5+
test('add', () => {
6+
assert.strictEqual(Calculator.add(1, 2), 3.0);
7+
assert.strictEqual(Calculator.add(1.0, 2.0), 3.0);
8+
assert.strictEqual(Calculator.add(0, 2.0), 2.0);
9+
assert.strictEqual(Calculator.add(2.0, 0), 2.0);
10+
assert.strictEqual(Calculator.add(-4, 2.0), -2.0);
11+
});
12+
13+
test('subtract', () => {
14+
assert.strictEqual(Calculator.subtract(1, 2), -1.0);
15+
assert.strictEqual(Calculator.subtract(2, 1), 1.0);
16+
assert.strictEqual(Calculator.subtract(1.0, 2.0), -1.0);
17+
assert.strictEqual(Calculator.subtract(0, 2.0), -2.0);
18+
assert.strictEqual(Calculator.subtract(2.0, 0.0), 2.0);
19+
assert.strictEqual(Calculator.subtract(-4, 2.0), -6.0);
20+
});
21+
22+
test('multiply', () => {
23+
assert.strictEqual(Calculator.multiply(1, 2), 2.0);
24+
assert.strictEqual(Calculator.multiply(1.0, 2.0), 2.0);
25+
assert.strictEqual(Calculator.multiply(0, 2.0), 0.0);
26+
assert.strictEqual(Calculator.multiply(2.0, 0.0), 0.0);
27+
assert.strictEqual(Calculator.multiply(-4, 2.0), -8.0);
28+
});
29+
30+
test('divide', () => {
31+
assert.strictEqual(Calculator.divide(1.0, 2.0), 0.5);
32+
assert.strictEqual(Calculator.divide(0, 2.0), 0);
33+
assert.strictEqual(Calculator.divide(-4, 2.0), -2.0);
34+
});

codecov.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
flag_management:
2+
individual_flags:
3+
- name: smart-tests
4+
carryforward: true
5+
carryforward_mode: "labels"
6+
statuses:
7+
- type: "project"
8+
- type: "patch"

istanbul-mocha/lib/script.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)