-
-
Notifications
You must be signed in to change notification settings - Fork 364
Open
Description
Hi, I have a webserver made with Express.js and I want check the coverage of some request made with Postman, but nyc don't show any line as reached by the requests.
I have made a small reprocucible example:
File structure
├── package.json
├── .nycrc.json
└── index.js
Files
package.json
{
"name": "test",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"cover": "nyc node index.js",
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"express": "^5.1.0"
},
"devDependencies": {
"nyc": "^17.1.0"
}
}
.nycrc.json
{
"all": true,
"check-coverage": false,
"extension": [
".js"
],
"include": [
"**/*.js"
],
"reporter": [
"text",
"lcov",
"html"
]
}
index.js
import express from 'express';
const app = express();
app.get('/', (req, res) => {
res.status(200).send('Hello World');
});
app.listen(3000, () => {
console.log('Server started on: http://localhost:3000/');
});
Steps to reproduce
- start the web server:
npm run cover
> [email protected] cover
> nyc node index.js
Server started on: http://localhost:3000/
- open http://localhost:3000/ in a browser. output:
Hello World
- close the terminal with
CTRL + C
, this is the final nyc output:
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 0 | 100 | 0 | 0 |
index.js | 0 | 100 | 0 | 0 | 3-10
----------|---------|----------|---------|---------|-------------------
The workaround I have found is manually trigger at runtime the coverage by calling an api /write-coverage
:
write-coverage.js
import { mkdirSync } from 'fs';
import { join } from 'path';
export const writeCoverage = async () => {
const coverage = (global as any).__coverage__;
if (!coverage) {
return false;
}
const mIstanbulLibReporter = await import('istanbul-lib-report');
const mIstanbulLibCoverage = await import('istanbul-lib-coverage');
const mIstanbulReports = await import('istanbul-reports');
const { createContext } = mIstanbulLibReporter.default;
const { createCoverageMap } = mIstanbulLibCoverage.default;
const { create } = mIstanbulReports.default;
const coverageMap = createCoverageMap(coverage);
const dir = join(process.cwd(), 'my-coverage');
mkdirSync(dir, { recursive: true });
const context = createContext({ dir, coverageMap });
const report = create('html'); // html, json, text, etc.
report.execute(context, coverageMap);
console.log('Coverage:' + dir);
return true;
};
index.js
import express from 'express';
import { writeCoverage } from './write-coverage.js'
const app = express();
app.get('/', (req, res) => {
res.status(200).send('Hello World');
});
app.get('/write-coverage', async(req, res) => {
res.status(200).send(await writeCoverage());
});
app.listen(3000, () => {
console.log('Server started on: http://localhost:3000/');
});
Metadata
Metadata
Assignees
Labels
No labels