{"id":238334,"date":"2022-05-20T08:30:00","date_gmt":"2022-05-20T12:30:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=238334"},"modified":"2025-05-30T07:26:40","modified_gmt":"2025-05-30T07:26:40","slug":"22-05-20-nyc","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/22-05-20-nyc\/","title":{"rendered":"Nyc and Istanbul Integration with Travis"},"content":{"rendered":"\n<p>Nyc is a command line tool for instrumenting code with Istanbul coverage (the successor to the istanbul command line tool). Let\u2019s see how we can\u2019t integrate nyc into our build and try it out!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-istanbul-works\">How Istanbul works<\/h2>\n\n\n\n<p>Istanbul instruments your ES5 and ES2015+ JavaScript code with line counters, so that you can track how well your unit-tests exercise your codebase.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-nyc-works\">How nyc works<\/h2>\n\n\n\n<p>The&nbsp;<code>nyc<\/code>&nbsp;command-line-client for Istanbul works well with most JavaScript testing frameworks: tap, mocha, AVA, etc.&nbsp;<code>nyc<\/code>&nbsp;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\u2019s website: https:\/\/istanbul.js.org\/integrations\/ and click the&nbsp;<code>nyc<\/code>&nbsp;integration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168933210-05e6f227-d524-46e6-91ee-70e3a6ec417d-1024x506-1.png\" alt=\"\" class=\"wp-image-242767\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168933210-05e6f227-d524-46e6-91ee-70e3a6ec417d-1024x506-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168933210-05e6f227-d524-46e6-91ee-70e3a6ec417d-1024x506-1-300x148.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168933210-05e6f227-d524-46e6-91ee-70e3a6ec417d-1024x506-1-768x380.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now that we have a fairly broad understanding of what&nbsp;<code>nyc<\/code>&nbsp;is and Istanbul is, let\u2019s get onto using it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"usage\">Usage<\/h2>\n\n\n\n<p>Somethings you\u2019ll need to get started and to run this successfully on Travis are:&nbsp;<code>Mongo<\/code>,&nbsp;<code>Express<\/code>,&nbsp;<code>Mocha<\/code>,&nbsp;<code>Chai<\/code>&nbsp;and&nbsp;<code>Jasmine<\/code>. You can grab these using&nbsp;<code>npm install -g<\/code>. Now first clone my repo:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/Montana\/travis-nyc.git<\/code><\/pre>\n\n\n\n<p>You\u2019ll see under Mongoose these three variables in&nbsp;<code>app.js<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> useNewUrlParser: true,\n useCreateIndex: true,\n useFindAndModify: false<\/code><\/pre>\n\n\n\n<p>Remove them. Now use your package manager (yarn, npm) to add it as a dev dependency:&nbsp;<code>npm i -D nyc<\/code>&nbsp;or&nbsp;<code>yarn add -D nyc<\/code>. You can use&nbsp;<code>nyc<\/code>&nbsp;to call&nbsp;<code>npm<\/code>&nbsp;scripts (assuming they don\u2019t already have&nbsp;<code>nyc<\/code>&nbsp;executed in them), if that\u2019s the case you\u2019ll have to replace your test runners with Mocha:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"scripts\": {\n    \"test\": \"mocha\",\n    \"coverage\": \"nyc npm run test\"\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"enforcing-overrides-in-nyc-and-istanbul\">Enforcing overrides in nyc and Istanbul<\/h2>\n\n\n\n<p>The great thing about&nbsp;<code>nyc<\/code>&nbsp;is that it allows you to inherit other configurations, that said, you can already imagine the flexibility. Using the key extends in the&nbsp;<code>package.json<\/code>&nbsp;stanza, .<code>nycrc<\/code>, or YAML files. You can then add the specific configuration options you want that aren\u2019t in that particular shared configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>{\n  \"extends\": \"@istanbuljs\/nyc-config-typescript\",\n  \"all\": true,\n  \"check-coverage\": true\n}<\/code><\/pre>\n\n\n\n<p>Now let\u2019s get to an up and running demo!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"mongodb\">MongoDB<\/h2>\n\n\n\n<p>Fetch MongoDB, in your&nbsp;<code>.travis.yml<\/code>&nbsp;file under the&nbsp;<code>script:<\/code>&nbsp;hook, do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>- wget -qO - https:\/\/www.mongodb.org\/static\/pgp\/server-5.0.asc | sudo apt-key add -\n  - sudo apt-get install -y mongodb-org<\/code><\/pre>\n\n\n\n<p>You clearly see we are fetching Mongo\u2019s&nbsp;<code>apt-key<\/code>\u2019s and then using&nbsp;<code>apt-get<\/code>&nbsp;to finish off the install, make sure you have&nbsp;<code>gnupg<\/code>&nbsp;installed as well you can do that by doing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get install gnupg<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-nyc-and-connecting-to-mongo-with-mock-credentials\">Installing nyc and connecting to Mongo with mock credentials<\/h2>\n\n\n\n<p>You\u2019ll want to run the following in your project root:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm init --yes\nnpm i express mocha chai supertest nyc mongoose<\/code><\/pre>\n\n\n\n<p>Depending on your version of node, you may have to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install -g nyc<\/code><\/pre>\n\n\n\n<p>For&nbsp;<code>nyc<\/code>&nbsp;to install completely. Let\u2019s now connect our app to our mock Mongo server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start mongo<\/code><\/pre>\n\n\n\n<p>Now that we are connected, let\u2019s run our app:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node bin\/www &amp; <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"the-travisyml-file\">The&nbsp;<code>.travis.yml<\/code>&nbsp;file<\/h2>\n\n\n\n<p>This is how my final&nbsp;<code>.travis.yml<\/code>&nbsp;file came out after I was done creating it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>services:\n  - docker\nlanguage: node_js\nnode_js:\n  - 17\nscript: \n  - npm init --yes\n  - npm i express mocha chai supertest nyc mongoose\n  - sudo apt-get install gnupg\n  - wget -qO - https:\/\/www.mongodb.org\/static\/pgp\/server-5.0.asc | sudo apt-key add -\n  - sudo apt-get install -y mongodb-org\n  - sudo systemctl start mongod\n  - node bin\/www &amp; \n  - npm install -g nyc\n  - nyc npm run test<\/code><\/pre>\n\n\n\n<p>We call on Docker for services, we are going to use Node version 17, and also start installing the&nbsp;<code>nyc<\/code>&nbsp;packages, and you kind of see how the rest progresses.&nbsp;<em>Important to have&nbsp;<code>&amp;<\/code>&nbsp;after&nbsp;<code>node bin\/www<\/code>&nbsp;or your build will get stuck in Travis, this tells Travis to have it run as a background process.<\/em><\/p>\n\n\n\n<p>Now you\u2019ll see this (or something similar) if&nbsp;<code>nyc<\/code>&nbsp;was ran successfully in Travis:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"430\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/169586975-7e8a19e5-5d39-47ef-8d5d-38e43f48832a-1024x430-1.png\" alt=\"\" class=\"wp-image-242762\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/169586975-7e8a19e5-5d39-47ef-8d5d-38e43f48832a-1024x430-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/169586975-7e8a19e5-5d39-47ef-8d5d-38e43f48832a-1024x430-1-300x126.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/169586975-7e8a19e5-5d39-47ef-8d5d-38e43f48832a-1024x430-1-768x323.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"testing-with-nyc\">Testing with nyc<\/h2>\n\n\n\n<p>Now, time to test your project with&nbsp;<code>nyc<\/code>, lets run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nyc npm run test<\/code><\/pre>\n\n\n\n<p>If it\u2019s successful you should see this in Travis:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"318\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932012-2e06635f-4448-4d96-bfbb-ce504949f53f-1024x318-1.png\" alt=\"\" class=\"wp-image-242764\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932012-2e06635f-4448-4d96-bfbb-ce504949f53f-1024x318-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932012-2e06635f-4448-4d96-bfbb-ce504949f53f-1024x318-1-300x93.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932012-2e06635f-4448-4d96-bfbb-ce504949f53f-1024x318-1-768x239.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you run it locally, you can get a GUI version of what you\u2019re seeing in the Travis build, which looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"177\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932096-a1426ea2-68f9-47b0-b5f9-e100168c8064-1024x177-1.png\" alt=\"\" class=\"wp-image-242766\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932096-a1426ea2-68f9-47b0-b5f9-e100168c8064-1024x177-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932096-a1426ea2-68f9-47b0-b5f9-e100168c8064-1024x177-1-300x52.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2022\/05\/168932096-a1426ea2-68f9-47b0-b5f9-e100168c8064-1024x177-1-768x133.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>This is the simplest way to use&nbsp;<code>nyc<\/code>&nbsp;and Travis together. This was made by myself (Montana Mendy) and as per usual if you have any questions please email me at&nbsp;<a href=\"mailto:montana@travis-ci.org\">montana@travis-ci.org<\/a>.<\/p>\n\n\n\n<p>Happy building!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nyc is a command line tool for instrumenting code with Istanbul coverage (the successor to the istanbul command line tool). Let\u2019s see how we can\u2019t integrate nyc into our build and try it out! How Istanbul works Istanbul instruments your ES5 and ES2015+ JavaScript code with line counters, so that you can track how well [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-238334","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=238334"}],"version-history":[{"count":3,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238334\/revisions"}],"predecessor-version":[{"id":242768,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238334\/revisions\/242768"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=238334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=238334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=238334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}