Skip to content

Commit

Permalink
Merge pull request #46 from mrubli/doc/issue-45
Browse files Browse the repository at this point in the history
Document how to avoid assertions causing test timeout (issue #45)
  • Loading branch information
knolleary authored Feb 2, 2021
2 parents 0a663e1 + 942b1f6 commit 55d45b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ describe('lower-case Node', function () {
var flow = [{ id: "n1", type: "lower-case", name: "lower-case" }];
helper.load(lowerNode, flow, function () {
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'lower-case');
done();
try {
n1.should.have.property('name', 'lower-case');
done();
} catch(err) {
done(err);
}
});
});

Expand All @@ -87,8 +91,12 @@ describe('lower-case Node', function () {
var n2 = helper.getNode("n2");
var n1 = helper.getNode("n1");
n2.on("input", function (msg) {
msg.should.have.property('payload', 'uppercase');
done();
try {
msg.should.have.property('payload', 'uppercase');
done();
} catch(err) {
done(err);
}
});
n1.receive({ payload: "UpperCase" });
});
Expand All @@ -100,9 +108,11 @@ In this example, we require `should` for assertions, this helper module, as well

We then have a set of mocha unit tests. These tests check that the node loads correctly, and ensures it makes the payload string lower case as expected.

Note how the assertion failures are caught explicitly and passed to the `done()` call. Node-RED swallows exceptions that are raised in the flow, so we make sure the test framework is aware of them. Not doing so would simply lead to a test timeout because `done()` is never called in case of an assertion failure.

## Initializing Helper

To get started, we need to tell the helper where to find the node-red runtime. this is done by calling `helper.init(require.resolve('node-red'))` as shown.
To get started, we need to tell the helper where to find the node-red runtime. This is done by calling `helper.init(require.resolve('node-red'))` as shown.

The helper takes an optional `userSettings` parameter which is merged with the runtime defaults.

Expand Down
24 changes: 18 additions & 6 deletions examples/lower-case_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,25 @@ describe('lower-case Node', function () {
var flow = [{ id: "n1", type: "lower-case", name: "lower-case" }];
helper.load(lowerNode, flow, function () {
var n1 = helper.getNode("n1");
n1.should.have.property('name', 'lower-case');
done();
try {
n1.should.have.property('name', 'lower-case');
done();
} catch(err) {
done(err);
}
});
});

it('should be loaded in exported flow', function (done) {
var flow = [{"id":"3912a37a.c3818c","type":"lower-case","z":"e316ac4b.c85a2","name":"lower-case","x":240,"y":320,"wires":[[]]}];
helper.load(lowerNode, flow, function () {
var n1 = helper.getNode("3912a37a.c3818c");
n1.should.have.property('name', 'lower-case');
done();
try {
n1.should.have.property('name', 'lower-case');
done();
} catch(err) {
done(err);
}
});
});

Expand All @@ -36,8 +44,12 @@ describe('lower-case Node', function () {
var n2 = helper.getNode("n2");
var n1 = helper.getNode("n1");
n2.on("input", function (msg) {
msg.should.have.property('payload', 'uppercase');
done();
try {
msg.should.have.property('payload', 'uppercase');
done();
} catch(err) {
done(err);
}
});
n1.receive({ payload: "UpperCase" });
});
Expand Down

0 comments on commit 55d45b6

Please sign in to comment.