我正在尝试在运行nyc时进行调试,而不是仅在运行Mocha测试时进行调试,因此我不必每次都运行两次测试。
VSCode运行覆盖并将其显示给我,但是它不会停止或验证断点,如何设置它以进行正确调试?
可能吗
我的启动配置:
{
"type": "node",
"request": "launch",
"name": "Coverge",
"program": "/usr/local/bin/nyc",
"args": [
"${workspaceFolder}/node_modules/mocha/bin/_mocha",
"-u",
"tdd",
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/tests/*/*"
],
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js"
],
"env": {},
"outputCapture": "std",
"internalConsoleOptions": "openOnSessionStart"
}
Run Code Online (Sandbox Code Playgroud) 因此,nyc正在修改我的文件,如下所示:
at _onCreate (src/post/admin.js:1:10453)
at doQuery (src/db.js:59:216)
at process._tickCallback (internal/process/next_tick.js:68:7)
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用源地图解开这个.文档说明:
使用源映射准确的堆栈跟踪.
当produce-source-map设置为true时,已检测的源文件将包含instrumenter转换的内联源映射.与源映射支持结合使用时,已检测代码的堆栈跟踪将反映其原始行.
所以我尝试了以下npm run命令:
"NODE_ENV=test nyc mocha --require ./tests/setup.js --require source-map-support/register --produce-source-map true --bail ./tests/unit/$FILE"
Run Code Online (Sandbox Code Playgroud)
结合纽约设置:
"nyc": {
"include": [
"src"
],
"exclude": [
"./tmp/**/*",
"./tests"
],
"instrument": true,
"report-dir": "./tests/coverage",
"temp-dir": "./tests/temp",
"source-map": true,
"produce-source-map": true
}
Run Code Online (Sandbox Code Playgroud)
但线路仍然受损.
nyc 中使用的仪器是什么?
nyc 的仪器命令可用于检测单元测试上下文之外的源文件:
我认为它会在单元测试之外进行覆盖。我试过了
nyc instrument src coverage/instrument
Run Code Online (Sandbox Code Playgroud)
然后运行应用程序并尝试访问端点
npm start
Run Code Online (Sandbox Code Playgroud)
但是当我执行上述操作时,它不会生成文件,nyc_output因此无法报告任何内容。
我必须完成nyc instrument命令吗?怎么办?
虽然我的测试完全正常
$ cross-env NODE_ENV=test nyc --require babel-register mocha
Run Code Online (Sandbox Code Playgroud)
一切都是零,在./coverage目录内也没有覆盖信息:(
我的项目非常基础(ES2015):
./lib.js
./test/lib.spec.js
Run Code Online (Sandbox Code Playgroud)
这是我的.babelrc:
{
"presets": [
"es2015",
"stage-0"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
],
"env": {
"test": {
"plugins": [
"istanbul"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
和.nycrc文件
{
"check-coverage": false,
"lines": 99,
"statements": 99,
"functions": 99,
"branches": 99,
"include": [
"./lib.js"
],
"reporter": [
"lcov",
"text-summary"
],
"require": [],
"extension": [
".js"
],
"cache": false,
"all": false,
"report-dir": "./coverage"
}
Run Code Online (Sandbox Code Playgroud)
}
我也使用ES5进行了相同的设置,结果相同.有什么建议为什么我没有报道?
更新:解决方案可以在这里找到
我正在使用istanbul和nyc命令为NodeJs运行代码覆盖率报告.
我正在使用mocha进行单元测试
我按照预期得到了每个文件的报告,但我希望看到的是一个包含单个目录摘要的报告.让我更详细地解释一下,我正在阅读我想要看到的内容
我的所有源文件都在一个文件夹中,我想查看该文件夹的摘要,而不是该文件夹中每个文件的完整列表
这是我的文件夹结构的样子
// This is the folder where all the sources are at
src
// This is the folder where coverage is output
coverage
NodeJs
index.html
file1.js
file2.js
file3.js
// This is the folder where all tests are at
tests
test_file1.js
test_file2.js
test_file3.js
Run Code Online (Sandbox Code Playgroud)
我的.babelrc文件看起来像这样
{
"presets": ["es2015", "stage-2"],
"plugins": [
[
"istanbul",
{"exclude": ["**/tests/*.js"]}
]
]
}
Run Code Online (Sandbox Code Playgroud)
我正在使用以下命令来运行我的测试覆盖率
node ./node_modules/.bin/nyc --reporter=html \
--report-dir=./src/coverage/NodeJs \
./node_modules/mocha/bin/_mocha \
--require babel-core/register \
--ui bdd src/tests/test*.js
Run Code Online (Sandbox Code Playgroud)
我的所有测试运行正常,它们通过,报告src/coverage/NodeJs/index.html按预期输出到文件.在浏览器中,该报告看起来像这样: …
我想nyc ava和babel所有人在一起好好相处.我有一个问题,async/await分支显示为未覆盖,所以这是有效的,我无法集成babel-plugin-istanbul插件进行测试.
我在项目中有两个文件,src/index.js和src/test.js.
{
"name": "example",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "ava",
"coverage": "NODE_ENV=test nyc npm run test",
"report": "nyc report --reporter=html",
"report:open": "open ./coverage/index.html",
"cover": "npm run coverage && npm run report && npm run report:open"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-runtime": "^6.26.0",
"bluebird": "^3.5.0",
"lodash": "^4.17.4"
},
"devDependencies": {
"ava": "^0.22.0",
"babel-cli": "^6.26.0",
"babel-plugin-istanbul": "^4.1.4",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.0",
"babel-register": …Run Code Online (Sandbox Code Playgroud) 我们是否需要包含.nyc_output,或者我们应该将该目录放在.gitignore中
我在一家@std/esm未获批准的公司工作。我的摩卡测试是这样工作的......
// suite.js
async function wire(){
await import("./Sanity.spec.mjs"); // eslint-disable-line
run();
}
wire();
//Sanity.spec.mjs
import chai from "chai"
describe("My Test", ()=>{
it("Should do what I want", ()=>chai.assert.equal(1,1, "One is in fact 1")); // eslint-disable-line no-self-compare
});
Run Code Online (Sandbox Code Playgroud)
但是当我运行时,nyc mocha --delay --exit ./test/suite.js我看到 mocha 测试运行但代码覆盖率结果为空。
My Test
? Should do what I want
When using base App
info: Healthcheck server is listening on 420
? App should not be null
? The port should be correct
3 passing …Run Code Online (Sandbox Code Playgroud) 我在 Puppeteer 的帮助下编写了一个小型爬虫。现在我面临的挑战是我的测试相当缓慢(每次测试 > 3 秒)。我已经能够将其追踪到launchPuppeteer的功能和Istanbul/nyc的使用。
mocha,测试将在 400 毫秒内完成。nyc,测试的持续时间超过 3000 毫秒我正在使用的只是
'use strict';
const puppeteer = require('puppeteer');
module.exports = async function startBrowser() {
const options = {
args: [
// '--no-sandbox',
// '--disable-setuid-sandbox',
// '--disable-dev-shm-usage',
// '--disable-accelerated-2d-canvas',
// '--disable-gpu'
],
headless: false // true
};
return await puppeteer.launch(options);
};
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的测试:
'use strict';
/* global describe: false, before: false, it: false,
beforeEach: false, afterEach: false, after: false, window: false, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 nyc + mocha 来获取使用 es6 模块语法的单元测试的测试覆盖率。当我运行时mocha my_test.mjs一切正常。my_test.mjs 中的依赖项(使用本机导入)已正确解析。但是当我在这个命令前面加上 nyc:nyc mocha my_test.mjs它不再工作,抛出这个错误:
node_modules/mocha/lib/esm-utils.js:6
return import(url.pathToFileURL(file));
^^^^^^
SyntaxError: Unexpected token import
at Module._extensions..js (module.js:663:10)
at Object.replacementCompile (nodejs/core/tests/cache.install.nodejs_install/node_modules/append-transform/index.js:60:13)
at Module._extensions..js (module.js:663:10)
Run Code Online (Sandbox Code Playgroud)
我尝试了不同的变体nyc --require esm mocha my_test.mjs或强制--experimental-modules使用节点,但似乎没有任何效果。
注意,我使用的是最新版本的node、nyc和mocha
任何想法?