The example in the document uses mocha for unit test using de-red-node-test-helper
.
Can we use Jest to test node?
We've not tried it, but please be our guest and try it for us. Feedback would be welcome.
You can use whatever you want. There is nothing tied to mocha in the test helper - it just provides a convenient way to start/stop the runtime with a test config.
I gave it a try and got stumbled upon async loading
$ jest
FAIL test/flow.test.js
Dummy Node
✓ should be loaded (22 ms)
✕ should make payload lower case (6 ms)
● Dummy Node › should make payload lower case
TypeError: Attempted to wrap log which is already wrapped
39 | { id: "n2", type: "helper" }
40 | ];
> 41 | helper.load(targetNode, flow, function () {
| ^
42 | var n2 = helper.getNode("n2");
43 | var n1 = helper.getNode("n1");
44 | n2.on("input", function (msg) {
at checkWrappedMethod (node_modules/sinon/lib/sinon/util/core/wrap-method.js:64:21)
at wrapMethod (node_modules/sinon/lib/sinon/util/core/wrap-method.js:135:13)
at Function.spy (node_modules/sinon/lib/sinon/spy.js:180:16)
at Sandbox.spy (node_modules/sinon/lib/sinon/sandbox.js:383:35)
at NodeTestHelper.load (node_modules/node-red-node-test-helper/index.js:178:53)
at Object.load (test/flow.test.js:41:12)
--------------
Error: Stack Trace for original
at extendObjectWithWrappedMethods (node_modules/sinon/lib/sinon/util/core/wrap-method.js:169:34)
at wrapMethod (node_modules/sinon/lib/sinon/util/core/wrap-method.js:157:5)
at Function.spy (node_modules/sinon/lib/sinon/spy.js:180:16)
at Sandbox.spy (node_modules/sinon/lib/sinon/sandbox.js:383:35)
at NodeTestHelper.load (node_modules/node-red-node-test-helper/index.js:178:53)
at Object.load (test/flow.test.js:25:12)
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 passed, 2 total
Snapshots: 0 total
Time: 1.044 s, estimated 2 s
Ran all test suites.
here is my test code
"use strict";
var should = require('should');
var helper = require("node-red-node-test-helper");
const fs = require('fs');
const Path = require('path');
var targetNode = require("../node.js");
helper.init(require.resolve('node-red'));
beforeAll(function (done) {
helper.startServer(done);
});
afterAll( (done) => {
helper.unload().then(function() {
helper.stopServer(done);
});
});
describe('Dummy Node', () => {
// this.timeout(10000);
it('should be loaded', (done) => {
var flow = [{ id: "n1", type: "dummy", name: "dummy node" }];
helper.load(targetNode, flow, function () {
var n1 = helper.getNode("n1");
try {
expect(n1.name).toEqual('dummy node');
done();
} catch (e) {
done(e)
}
});
});
});