# Can we use Jest for unit test?

**URL:** https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154
**Category:** General
**Created:** [11 July 2019 08:17 UTC](https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154 "2019-07-11T08:17:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![fangzhu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fangzhu/32/5796_2.png) [@fangzhu](https://discourse.nodered.org/u/fangzhu)
#### Post date: [11 July 2019 08:17 UTC](https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154/1 "2019-07-11T08:17:16Z")

</div>

The example in the document uses mocha for unit test using [`de-red-node-test-helper`](https://www.npmjs.com/package/node-red-node-test-helper).  
Can we use Jest to test node?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [11 July 2019 08:21 UTC](https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154/2 "2019-07-11T08:21:05Z")

</div>

We've not tried it, but please be our guest and try it for us. Feedback would be welcome.

---

<div class="post-metadata">

### Author: ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)
#### Post date: [11 July 2019 10:45 UTC](https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154/3 "2019-07-11T10:45:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kakyoism](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kakyoism/32/46283_2.png) [@kakyoism](https://discourse.nodered.org/u/kakyoism)
#### Post date: [9 December 2022 09:41 UTC](https://discourse.nodered.org/t/can-we-use-jest-for-unit-test/13154/4 "2022-12-09T09:41:17Z")

</div>

I gave it a try and got stumbled upon async loading

```nohighlight
$ 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

```nohighlight
"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)
      }
    });
  });
});

```
