The nyc
command-line-client for Istanbul works well with most JavaScript testing frameworks: tap, mocha, AVA, etc. nyc
is a command line tool for instrumenting code with Istanbul coverage (the successor to the istanbul command line tool). For more information go to Istanbul's website: https://istanbul.js.org/integrations/ and click the nyc
integration:
Let's get onto usage.
First clone my repo:
git clone https://github.com/Montana/travis-nyc.git
You'll see under Mongoose these three variables in app.js
:
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false
Remove them.
Fetch MongoDB, in your .travis.yml
file under the script:
hook, do:
- wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
- sudo apt-get install -y mongodb-org
You clearly see we are fetching Mongo's apt-key
's and then using apt-get
to finish off the install, make sure you have gnupg
installed as well you can do that by doing:
sudo apt-get install gnupg
You'll want to run the following in your project root:
npm init --yes
npm i express mocha chai supertest nyc mongoose
Depending on your version of node, you may have to run:
npm install -g nyc
For nyc
to install completely. Let's now connect our app to our mock Mongo server:
sudo systemctl start mongo
Now that we are connected, let's run our app:
node bin/www &
This is how my final .travis.yml
file came out after I was done creating it:
services:
- docker
language: node_js
node_js:
- 17
script:
- npm init --yes
- npm i express mocha chai supertest nyc mongoose
- sudo apt-get install gnupg
- wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
- sudo apt-get install -y mongodb-org
- sudo systemctl start mongod
- node bin/www &
- npm install -g nyc
- nyc npm run test
We call on Docker for services, we are going to use Node version 17, and also start installing the nyc
packages, and you kind of see how the rest progresses. Important to have &
after node bin/www
or your build will get stuck in Travis, this tells Travis to have it run as a background process.
Now, time to test your project with nyc
, lets run:
nyc npm run test
If it's successful you should see this in Travis:
If you run it locally, you can get a GUI version of what you're seeing in the Travis build, which looks like this:
This is the simplest way to use nyc
and Travis together. This was made by myself (Montana Mendy) and as per usual if you have any questions please email me at [email protected].