当我尝试使用nyc
. 我还使用库node-worker-threads-pool来创建工作线程池。
我创建了一个最小的示例来模拟这种行为。在这个例子中,行为是相同的,当我执行测试时,一切正常,但是当我试图获得覆盖率时,我有一个异常。
这是我创建新线程的文件
// thread.js
const os = require('os');
const { DynamicPool } = require('node-worker-threads-pool');
const Pool = new DynamicPool(os.cpus().length, { shareEnv: true });
const someFunction = async (params) =>
await Pool.exec({
task: async function() {
const { params } = this.workerData;
console.log('params', params);
return params;
},
workerData: {
params,
},
});
module.exports = {
someFunction,
};
这是测试
// thread.test.js
const { someFunction } = require('../thread');
describe('Some Function in another thread', () => {
it('it works', async () => {
const params = 1;
const res = await someFunction(params);
expect(res).to.be.eq(params);
});
});
当我执行NODE_ENV=test mocha --exit 'thread.test.js' --maxWorkers=2
测试通过时,但是当我尝试使用 nyc 获得覆盖率时,会NODE_ENV=test nyc --reporter=text --reporter=html --reporter=lcov mocha --exit 'thread.test.js' --maxWorkers=2
出现以下异常:
1) Some Function in another thread
it works:
ReferenceError: cov_vyj5vtdds is not defined
at Object.task (evalmachine.<anonymous>:1:40)
at evalmachine.<anonymous>:1:204
at Script.runInThisContext (vm.js:120:20)
at Object.runInThisContext (vm.js:311:38)
at MessagePort.<anonymous> ([worker eval]:11:29)
at MessagePort.onmessage (internal/worker/io.js:78:8)
at MessagePort.exports.emitMessage (internal/per_context/messageport.js:11:10)
From previous event:
at PoolWorker.work (node_modules/node-worker-threads-pool/src/pool-worker.js:22:12)
at DynamicPool.runTask (node_modules/node-worker-threads-pool/src/pool.js:110:47)
at DynamicPool.exec (node_modules/node-worker-threads-pool/src/dynamic-pool.js:51:17)
at someFunction (src/modules/templates/thread.js:1:1577)
at Context.<anonymous> (test/modules/templates/thread.test.js:6:23)
at processImmediate (internal/timers.js:456:21)
难道我做错了什么?