BlockLune / blocklune.github.io

My blog powered by Hexo.
https://fishg.top/
4 stars 0 forks source link

Testing Typescript project with Mocha and Istanbul NYC | Fish Pond #84

Open BlockLune opened 4 months ago

BlockLune commented 4 months ago

https://fishg.top/posts/402d9408.html

Mocha is a popular JS test framework, and Istanbul is a popular JS test coverage tool. How to use them when it comes to Typescript? This post shows a simple demo.

iTonyYo commented 1 month ago

it's not working... I followed the article exactly, but the end result is this.

BlockLune commented 1 month ago

it's not working... I followed the article exactly, but the end result is this.

@iTonyYo I have no idea what's happening either. At first I thought I was overlooking something in my post, but I just tested it in a project that I'm sure used to be able to run nyc, and it also came up as not testing properly!

I found this and I think the problem might be caused by node or nyc itself.

One possible solution is to use c8 for the coverage test. Run the command below to install it:

npm i c8 -g

And test coverage with:

c8 npm test

My output:

~/Documents/ts_mocha_nyc_demo> npm run coverage

> ts_mocha_nyc_demo@1.0.0 coverage
> nyc npm test

> ts_mocha_nyc_demo@1.0.0 test
> cross-env NODE_OPTIONS='--import tsx' mocha 'test/**/*.test.ts'

  Division Test
    ✔ should return 2 when divide(4, 2) called
    ✔ should throw error when divide(4, 0) called

  2 passing (2ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |       0 |        0 |       0 |       0 |                   
----------|---------|----------|---------|---------|-------------------

=============================== Coverage summary ===============================
Statements   : Unknown% ( 0/0 )
Branches     : Unknown% ( 0/0 )
Functions    : Unknown% ( 0/0 )
Lines        : Unknown% ( 0/0 )
================================================================================
~/Documents/ts_mocha_nyc_demo> c8 npm test

> ts_mocha_nyc_demo@1.0.0 test
> cross-env NODE_OPTIONS='--import tsx' mocha 'test/**/*.test.ts'

  Division Test
    ✔ should return 2 when divide(4, 2) called
    ✔ should throw error when divide(4, 0) called

  2 passing (3ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |    88.88 |     100 |     100 |                   
 divide.ts |     100 |    88.88 |     100 |     100 | 1                 
-----------|---------|----------|---------|---------|-------------------

=============================== Coverage summary ===============================
Statements   : 100% ( 4/4 )
Branches     : 88.88% ( 8/9 )
Functions    : 100% ( 6/6 )
Lines        : 100% ( 4/4 )
================================================================================
BlockLune commented 1 month ago

By the way my envinfo (run npx envinfo@latest --preset nyc to check yours):

  System:
    OS: macOS 14.4.1
    CPU: (8) arm64 Apple M1
    Memory: 304.23 MB / 8.00 GB
  Binaries:
    Node: 20.11.0 - ~/.nvm/versions/node/v20.11.0/bin/node
    npm: 10.2.4 - ~/.nvm/versions/node/v20.11.0/bin/npm
    pnpm: 9.0.6 - ~/.nvm/versions/node/v20.11.0/bin/pnpm
  npmPackages:
    nyc: ^15.1.0 => 15.1.0 
    typescript: ^5.4.5 => 5.4.5 
iTonyYo commented 1 month ago

Finally, I replaced nyc with https://github.com/bcoe/c8 and it worked fine.