You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+16-21
Original file line number
Diff line number
Diff line change
@@ -2,20 +2,15 @@
2
2
3
3
This test-helper module makes the node test framework from the Node-RED core available for node contributors.
4
4
5
-
Using the test-helper, your tests can start the Node-RED runtime, load a flow and receive messages
6
-
to ensure your node code is correct.
5
+
Using the test-helper, your tests can start the Node-RED runtime, load a flow and receive messages to ensure your node code is correct.
7
6
8
7
## Adding to your node project dependencies
9
8
10
9
To add unit tests your node project test dependencies, add this test helper as follows:
11
10
12
11
npm install node-red-node-test-helper --save-dev
13
12
14
-
This will add this modules to your `package.json` file as a development dependency.
15
-
16
-
[Mocha](https://mochajs.org/) is a unit test framework for Javascript. [Should](https://shouldjs.github.io/) is an assertion library used in our example unit tests which will also be added to your porject. For more information on these frameworks, see their associated documentation. Depending on your testing needs, you may need to add additional dev dependencies.
17
-
18
-
Your `project.json` file should now contain something like the following:
13
+
This will add the helper module to your `package.json` file as a development dependency:
19
14
20
15
```json
21
16
...
@@ -25,9 +20,11 @@ Your `project.json` file should now contain something like the following:
25
20
...
26
21
```
27
22
23
+
Both [Mocha](https://mochajs.org/) and [Should](https://shouldjs.github.io/) will be pulled in with the test helper. Mocha is a unit test framework for Javascript; Should is an assertion library. For more information on these frameworks, see their associated documentation.
24
+
28
25
## Adding test script to `package.json`
29
26
30
-
To run your tests you can add a test script to your `package.json` file in the `scripts` section. To run all of the files with the `_spec.js` prefix in the test directory:
27
+
To run your tests you can add a test script to your `package.json` file in the `scripts` section. To run all of the files with the `_spec.js` prefix in the test directory for example:
31
28
32
29
```json
33
30
...
@@ -37,15 +34,15 @@ To run your tests you can add a test script to your `package.json` file in the `
37
34
...
38
35
```
39
36
40
-
This will allow you to use `npm test`to run your tests.
37
+
This will allow you to use `npm test`on the command line.
41
38
42
39
## Creating unit tests
43
40
44
-
We recommend putting unit test scripts in the `test/` folder of your project and using the `_spec.js` (for specification) suffix naming convention for your tests.
41
+
We recommend putting unit test scripts in the `test/` folder of your project and using the `*_spec.js` (for specification) suffix naming convention.
45
42
46
43
## Example Unit Test
47
44
48
-
Here is an example test for testing the lower-case node in the [Node-RED documentation](https://nodered.org/docs/creating-nodes/first-node). Lets name our test script `test/lower-case_spec.js`.
45
+
Here is an example test for testing the lower-case node in the [Node-RED documentation](https://nodered.org/docs/creating-nodes/first-node). Here we name our test script `test/lower-case_spec.js`.
49
46
50
47
### `test/lower-case_spec.js`:
51
48
@@ -87,31 +84,29 @@ describe('lower-case Node', function () {
87
84
});
88
85
```
89
86
90
-
In this example, we require `should` for assertions, the helper module, as well as the `lower-case` node we want to test.
87
+
In this example, we require `should` for assertions, this helper module, as well as the `lower-case` node we want to test, located in the parent directory.
91
88
92
89
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.
93
90
94
91
## Getting nodes in the runtime
95
92
96
-
The asynchronous `helper.load()` method calls the supplied function once the Node-RED server and runtime is read. We can then call helper methods to get a reference to nodes in the runtime. For more information on `helper.load()` see the API section below.
93
+
The asynchronous `helper.load()` method calls the supplied callback function once the Node-RED server and runtime is ready. We can then call the `helper.getNode(id)` method to get a reference to nodes in the runtime. For more information on these methods see the API section below.
97
94
98
95
## Receiving messages from nodes
99
96
100
-
The second test uses a `helper` node in the runtime connected to the output of our `lower-case` node under test.
97
+
The second test uses a `helper` node in the runtime connected to the output of our `lower-case` node under test. The `helper` node is a mock node with no functionality. By adding "input" event handlers as in the example, we can check the messages received by the `helper`.
101
98
102
-
The Helper node is a mock node with no functionality. By adding "input" event handlers as in the example, the helper node can receive a message from the previous `lower-case` node in the flow and check if its contents are as expected.
99
+
To send a message into the `lower-case` node `n1` under test we call `n1.receive({ payload: "UpperCase" })` on that node. We can then check that the payload is indeed lowercasein the `helper` node input event handler.
103
100
104
101
## Running your tests
105
102
106
103
To run your tests:
107
104
108
-
npm run
109
-
110
-
Producing the following output (for example):
105
+
npm test
111
106
112
-
>npm test
107
+
Producing the following output (for this example):
113
108
114
-
> [email protected] test /Users/mike/dev/work/node-red-contrib-lower-case
@@ -134,7 +129,7 @@ Loads a flow then starts the flow. This function has the following arguments:
134
129
135
130
* testNode: (object|array of objects) Module object of a node to be tested returned by require function. This node will be registered, and can be used in testFlows.
136
131
* testFlows: (array of objects) Flow data to test a node. If you want to use the flow data exported from Node-RED editor, need to covert it to JavaScript object using JSON.parse().
137
-
* testCredentials: (object) Optional node credentials. This argument is optional.
0 commit comments