使用 Jest 在 Node-RED 中对自定义节点进行单元测试时出现 setTimeout 错误 [英] setTimeout error when unit testing a custom node in Node-RED using Jest
本文介绍了使用 Jest 在 Node-RED 中对自定义节点进行单元测试时出现 setTimeout 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我注意到在 Node-RED 中测试自定义节点通常不是使用 Jest 完成的 - 我认为没有特别的原因,但这是我最熟悉的,所以无论如何我都在尝试.
I've noticed that in general testing custom nodes in Node-RED is not done using Jest - I don't think there is a particular reason but it's what I'm the most familiar with so I'm trying it anyway.
我的测试设置看起来像
const n = require('../src/index.js');
describe('custom node', () => {
const nodeType = 'custom node';
beforeAll(done => {
helper.startServer(done);
});
afterEach(() => {
helper.unload();
});
afterAll(done => {
helper.stopServer(done);
});
it('should be loaded', () =>
new Promise((resolve, reject) => {
const flow = [{ id: 'n1', type: nodeType, name: 'test name' }];
helper.load(n, flow, () => {
const n1 = helper.getNode('n1');
try {
expect(n1).toBeTruthy();
expect(n1).toHaveProperty('name', 'test name');
resolve();
} catch (e) {
reject(e);
}
});
}));
});
产生的错误是
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:289
Unhandled error
console.error node_modules/jest-jasmine2/build/jasmine/Env.js:290
TypeError: setTimeout(...).unref is not a function
at Immediate.<anonymous> (/dev/custom_nodes/node-red-contrib-x/node_modules/stoppable/lib/stoppable.js:43:39)
at processImmediate (internal/timers.js:439:21)
at process.topLevelDomainCallback (domain.js:126:23)
推荐答案
解决方案最终很简单:将 Jest 配置中的 testEnvironment
设置为 node
以便 setTimeout 为没有被 Jest 替换为一个没有 unref 功能的.
The solution ended up being simple: set the testEnvironment
in Jest config to node
so that setTimeout is not replaced by Jest by one which does not have the unref function.
例如在 jest.config.js 中
e.g. in jest.config.js
module.exports = {
testEnvironment: 'node',
};
这篇关于使用 Jest 在 Node-RED 中对自定义节点进行单元测试时出现 setTimeout 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文