Skip to content

Commit cf2c994

Browse files
committed
Moves common setup and teardown for tests into fixture
1 parent 8e520a5 commit cf2c994

File tree

3 files changed

+55
-83
lines changed

3 files changed

+55
-83
lines changed

test/01.js

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,30 @@
22

33
var test = require('tape'),
44
sinon = require('sinon');
5-
var path = require('path');
6-
var embeddedStart = require('../');
7-
8-
const REDsettings = {
9-
httpAdminRoot: false,
10-
httpNodeRoot: false,
11-
functionGlobalContext: {},
12-
disableEditor: true,
13-
userDir: path.join(__dirname, '.node-red'),
14-
logging: {
15-
console: {
16-
level: process.env.NODE_RED_LOGLEVEL || "info"
17-
}
18-
}
19-
};
5+
var embeddedStart = require('../'),
6+
fixture = require('./fixture');
207

218
var RED;
229
var REDevents;
2310
var testFlow;
2411

25-
function failAndEnd(t) {
26-
return (err) => {
27-
t.fail(err);
28-
t.end();
29-
};
30-
}
31-
3212
test.onFinish(function() {
33-
34-
function closeUp() {
35-
let prom = testFlow ? RED.nodes.removeFlow(testFlow) : Promise.resolve();
36-
if (RED) {
37-
prom = prom.then(() => RED.stop());
38-
}
39-
prom.catch((e) => {
40-
console.error("stopping Node-RED failed:");
41-
console.error(e.stack ? e.stack : e);
42-
});
43-
}
44-
45-
closeUp();
13+
fixture.closeUp(RED, testFlow);
4614
});
4715

4816
test('can create and initialize Node-RED runtime', function(t) {
4917
t.plan(3);
5018

5119
t.doesNotThrow(() => {
5220
RED = require('node-red');
53-
}, undefined, 'instantiates Node-RED runtime without error');
54-
t.doesNotThrow(RED.init.bind(RED, undefined, REDsettings),
55-
undefined,
56-
'initializes Node-RED runtime without error');
21+
}, undefined, 'loads Node-RED module without error');
22+
t.doesNotThrow(
23+
RED.init.bind(RED, undefined, fixture.settings),
24+
undefined,
25+
'initializes Node-RED runtime without error');
5726
t.doesNotThrow(() => {
5827
REDevents = require('node-red/red/runtime/events');
59-
}, undefined, 'loads RED events object without erorr');
28+
}, undefined, 'loads Node-RED events module without error');
6029
});
6130

6231
test('can be used to generate wait function', function(t) {
@@ -84,7 +53,7 @@ test('can be used to promise waiting for \'nodes-started\'', function(t) {
8453
spy();
8554
tt.pass('resolves once nodes-started event fires');
8655
tt.equal(result, REDresult, 'passes result through');
87-
}).catch(failAndEnd(tt));
56+
}).catch(fixture.failAndEnd(tt));
8857
setTimeout(() => {
8958
tt.ok(spy.notCalled, 'does not resolve before node-started event fires');
9059
REDevents.emit('nodes-started');
@@ -116,7 +85,7 @@ test('generated function promises to wait for \'nodes-started\'', function(t) {
11685
spy();
11786
t.pass('resolves once nodes-started event fires');
11887
t.equal(result, REDresult, 'passes result through');
119-
}).catch(failAndEnd(t));
88+
}).catch(fixture.failAndEnd(t));
12089
setTimeout(() => {
12190
t.ok(spy.notCalled, 'does not resolve before node-started event fires');
12291
REDevents.emit('nodes-started');
@@ -195,7 +164,7 @@ test('waiting for \'nodes-started\' results in flow API ready', function(t) {
195164
t.doesNotThrow(() => addFlow(t, true),
196165
undefined,
197166
'addFlow() now returns without error');
198-
}).catch(failAndEnd(t));
167+
}).catch(fixture.failAndEnd(t));
199168
});
200169

201170
test('generated function resolves immediately if flows API ready', function(t) {
@@ -208,7 +177,7 @@ test('generated function resolves immediately if flows API ready', function(t) {
208177
spy();
209178
t.pass('wait function resolves immediately when flows API ready');
210179
t.equal(result, REDresult, 'and passes result through');
211-
}).catch(failAndEnd(t));
180+
}).catch(fixture.failAndEnd(t));
212181
setTimeout(() => {
213182
t.ok(spy.calledOnce, 'has resolved before timeout and without event');
214183
}, 10);

test/02.js

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,20 @@
22

33
var test = require('tape'),
44
sinon = require('sinon');
5-
var path = require('path');
6-
var embeddedStart = require('../');
7-
8-
const REDsettings = {
9-
httpAdminRoot: false,
10-
httpNodeRoot: false,
11-
functionGlobalContext: {},
12-
disableEditor: true,
13-
userDir: path.join(__dirname, '.node-red'),
14-
logging: {
15-
console: {
16-
level: process.env.NODE_RED_LOGLEVEL || "info"
17-
}
18-
}
19-
};
5+
var embeddedStart = require('../'),
6+
fixture = require('./fixture');
207

218
var RED;
229
var REDstart;
2310
var testFlow;
2411

25-
function failAndEnd(t) {
26-
return (err) => {
27-
t.fail(err);
28-
t.end();
29-
};
30-
}
31-
3212
test.onFinish(function() {
33-
34-
function closeUp() {
35-
let prom = testFlow ? RED.nodes.removeFlow(testFlow) : Promise.resolve();
36-
if (RED) {
37-
prom = prom.then(() => RED.stop());
38-
}
39-
prom.catch((e) => {
40-
console.error("stopping Node-RED failed:");
41-
console.error(e.stack ? e.stack : e);
42-
});
43-
}
44-
4513
if (REDstart && RED.start !== REDstart) RED.start = REDstart;
46-
closeUp();
14+
fixture.closeUp(RED, testFlow);
4715
});
4816

4917
RED = require('node-red');
50-
RED.init(undefined, REDsettings);
18+
RED.init(undefined, fixture.settings);
5119

5220
test('can inject wait into RED.start()', function(t) {
5321
t.plan(6);
@@ -86,7 +54,7 @@ test('can inject wait into RED.start()', function(t) {
8654
undefined,
8755
'addFlow() returns without error');
8856
return prom;
89-
}).catch(failAndEnd(t));
57+
}).catch(fixture.failAndEnd(t));
9058
});
9159

9260
test('arguments with which RED.start() is called are passed on', function(t) {
@@ -107,10 +75,10 @@ test('arguments with which RED.start() is called are passed on', function(t) {
10775
let call = stub.getCall(1);
10876
RED.start = funcToRestore; // should precede last test = this is async
10977
t.deepEqual(call.args, ["one", "two", "three"],
110-
'correctly passes through multiple arguments');
78+
'correctly passes through multiple arguments');
11179
t.equal(call.thisValue, RED, '"this" is set to RED');
11280
}).catch((err) => {
11381
RED.start = funcToRestore;
114-
failAndEnd(t)(err);
82+
fixture.failAndEnd(t)(err);
11583
});
11684
});

test/fixture.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict";
2+
3+
var path = require('path');
4+
5+
module.exports = {
6+
failAndEnd: function(test) {
7+
return (err) => {
8+
if (err && err.stack) console.error(err.stack);
9+
test.fail(err);
10+
test.end();
11+
};
12+
},
13+
closeUp: function(RED, flow) {
14+
let prom = flow ? RED.nodes.removeFlow(flow) : Promise.resolve();
15+
if (RED) {
16+
prom = prom.then(() => RED.stop());
17+
}
18+
prom.catch((e) => {
19+
console.error("stopping Node-RED failed:");
20+
console.error(e.stack ? e.stack : e);
21+
});
22+
},
23+
settings: {
24+
httpAdminRoot: false,
25+
httpNodeRoot: false,
26+
functionGlobalContext: {},
27+
disableEditor: true,
28+
userDir: path.join(__dirname, '.node-red'),
29+
logging: {
30+
console: {
31+
level: process.env.NODE_RED_LOGLEVEL || "info"
32+
}
33+
}
34+
}
35+
};

0 commit comments

Comments
 (0)