Adding Shields.io Badges, AVA Tests, nyc Coverage, Codecov Reporting, and Travis CI to a Node.js Project
This guide shows how to generate GitHub badges with shields.io, write unit tests using AVA, measure coverage with nyc, report results to Codecov, and automate the whole process with Travis CI, providing a complete CI/CD workflow for Node.js projects.
Many open‑source repositories on GitHub display small badges, which can be generated automatically from shields.io . By selecting a badge type (e.g., npm version) and copying the generated Markdown, you can paste it into a README file.
AVA testing
AVA is a modern JavaScript test runner that supports ES6+ out of the box. Install it with npm init ava and write test files such as test.js :
import test from 'ava';
import Memcached from '../lib/memcached';
test.before(t => {
const memcached = new Memcached(['127.0.0.1:11211'], {
pool: { max: 2, min: 0 },
timeout: 5000
});
t.context.memcached = memcached;
});
test('memcached get/set', async t => {
t.plan(3);
const key = 'testkey';
const testdata = 'testest\r\n\\stese';
const r = await t.context.memcached.set(key, testdata);
t.is(r, 'STORED');
const g = await t.context.memcached.get(key, testdata);
t.is(g, testdata);
const dr = await t.context.memcached.del(key);
t.is(dr, 'DELETED');
});
test('unit test title', t => {
t.pass();
});AVA supports hooks such as test.before and the t.context object for sharing state between tests. Use test.serial when tests must run sequentially.
Code coverage with nyc
Install nyc as a dev dependency and add a test script to package.json :
npm install --save-dev nyc
{
"scripts": {
"test": "nyc ava"
}
}Running npm test will execute the AVA tests and generate an .nyc_output directory. Add the output folder to .gitignore to avoid committing it.
Reporting to Codecov
Install the Codecov client and add a reporting script:
npm install --save-dev codecov
{
"scripts": {
"report-coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov"
}
}Set the environment variable CODECOV_TOKEN=<token> in your CI environment (the token is generated on the Codecov website) and run npm run report-coverage to upload the coverage data.
Continuous integration with Travis CI
Create a .travis.yml file similar to the following:
language: node_js
node_js:
- "12"
sudo: required
before_install:
- sudo apt-get install libevent-dev -y
install:
- wget -O memcached.tar.gz http://memcached.org/files/memcached-1.5.20.tar.gz
- tar -zxvf memcached.tar.gz
- cd memcached-1.5.20
- ./configure && make && sudo make install
- memcached -d
script:
- npm ci && npm test && npm run report-coverageDefine the CODECOV_TOKEN variable in the Travis Settings page. When a push is made to GitHub, Travis will install dependencies, run the tests, generate coverage, and upload it to Codecov, resulting in build‑passing badges.
Conclusion
Shields.io offers many badge types; by configuring them you can display build status, test coverage, and other metrics. The article demonstrated using AVA, nyc, Codecov, and Travis CI as a complete workflow for a Node.js project.
System Architect Go
Programming, architecture, application development, message queues, middleware, databases, containerization, big data, image processing, machine learning, AI, personal growth.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.