# This async unittest never fails when it should

**URL:** https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912
**Category:** Developing Nodes
**Created:** [9 December 2022 11:07 UTC](https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912 "2022-12-09T11:07:42Z")
**Posts on this page:** 4
**Page:** 1

<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 11:07 UTC](https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912/1 "2022-12-09T11:07:42Z")

</div>

I'm bothered by this test below, where it never fails

```nohighlight
var should = require('should');
var helper = require("node-red-node-test-helper");
var targetNode = require("../node.js");

helper.init(require.resolve('node-red'));

describe('Dummy Node', function () {
// this.timeout(10000);

  beforeEach(function (done) {
      helper.startServer(done);
  });

  afterEach(function (done) {
      helper.unload().then(function() {
        helper.stopServer(done);
    });
  });

  it('should be loaded', async () => {
    var flow = [{ id: "n1", type: "dummy", name: "dummy node" }];
	try {
	    await helper.load(targetNode, flow);
		var n1 = helper.getNode("n1");
		n1.should.have.property('name', 'dummy node');
	} catch (e) {
	}
  });

  it('should make payload lower case', async () => {
    var flow = [
	    { id: "n1", type: "dummy", name: "dummy node", wires:[["n2"]] },
	    { id: "n2", type: "helper" }
	];
    await helper.load(targetNode, flow);
	var n2 = helper.getNode("n2");
	var n1 = helper.getNode("n1");
    n2.on("input", (msg) => {
		try {
	        msg.should.have.property('payload', {'name': 'x'});
		} catch (e) {
		}
    });
	n1.receive({ topic: "", payload: "toupper" });
  });
});

```

This test is adapted from the `node-red-node-test-helper` example. The callback version used in the original example works fine.  
But after I tried to convert it to the async-await style. It always passes, to my surprise.  
For example, in the above code, I made the expected result a wrong value "x", where the correct result should be "TOUPPER". However, the test always passes.

Where am I wrong?

---

<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: [9 December 2022 11:17 UTC](https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912/2 "2022-12-09T11:17:36Z")

</div>

If you make the test an async function, then whether it passes or fails will depend on how the promise it returns is resolved.

Currently, you don't return anything, so the function is resolving cleanly. Where you have the `msg.should.have..` statement is inside the `input` event handler, which is called asynchronously to the call to `n1.receive`. This means there is no relationship between the async event of the `input` handler and what the test case is returning. Plus you have the `msg.should` statement wrapped in a try/catch, so the error doesn't go anywhere useful.

One possible approach would be to wrap the test in a promise:

```auto
  it('should make payload lower case', async () => {
    var flow = [
	    { id: "n1", type: "dummy", name: "dummy node", wires:[["n2"]] },
	    { id: "n2", type: "helper" }
	];
    await helper.load(targetNode, flow);
    return new Promise((resolve, reject) => {
        var n2 = helper.getNode("n2");
        var n1 = helper.getNode("n1");
        n2.on("input", (msg) => {
			try {
		        msg.should.have.property('payload', {'name': 'x'});
				resolve()
			} catch (e) {
				reject(e)
			}
	    });
		n1.receive({ topic: "", payload: "toupper" });
	})
  });

```

But personally, I prefer to use the callback method described in the docs.

---

<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 12:00 UTC](https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912/3 "2022-12-09T12:00:13Z")

</div>

Thanks a lot, Nick! I'm still quite a noob at async coding. Your help is much appreciated.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [23 December 2022 12:00 UTC](https://discourse.nodered.org/t/this-async-unittest-never-fails-when-it-should/71912/4 "2022-12-23T12:00:59Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
