aboutsummaryrefslogtreecommitdiff
path: root/node_modules/ava/lib/test-collection.js
blob: fd34bc489300e17c5f577a4e1cf4b05bc12bfff9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict';
const EventEmitter = require('events');
const fnName = require('fn-name');
const Concurrent = require('./concurrent');
const Sequence = require('./sequence');
const Test = require('./test');

class TestCollection extends EventEmitter {
	constructor(options) {
		super();

		this.bail = options.bail;
		this.failWithoutAssertions = options.failWithoutAssertions;
		this.compareTestSnapshot = options.compareTestSnapshot;
		this.hasExclusive = false;
		this.testCount = 0;

		this.tests = {
			concurrent: [],
			serial: []
		};

		this.hooks = {
			before: [],
			beforeEach: [],
			after: [],
			afterAlways: [],
			afterEach: [],
			afterEachAlways: []
		};

		this.pendingTestInstances = new Set();

		this._emitTestResult = this._emitTestResult.bind(this);
	}

	add(test) {
		const metadata = test.metadata;
		const type = metadata.type;

		if (!type) {
			throw new Error('Test type must be specified');
		}

		if (!test.title && test.fn) {
			test.title = fnName(test.fn);
		}

		// Workaround for Babel giving anonymous functions a name
		if (test.title === 'callee$0$0') {
			test.title = null;
		}

		if (!test.title) {
			if (type === 'test') {
				test.title = '[anonymous]';
			} else {
				test.title = type;
			}
		}

		if (metadata.always && type !== 'after' && type !== 'afterEach') {
			throw new Error('"always" can only be used with after and afterEach hooks');
		}

		// Add a hook
		if (type !== 'test') {
			if (metadata.exclusive) {
				throw new Error(`"only" cannot be used with a ${type} hook`);
			}

			this.hooks[type + (metadata.always ? 'Always' : '')].push(test);
			return;
		}

		this.testCount++;

		// Add `.only()` tests if `.only()` was used previously
		if (this.hasExclusive && !metadata.exclusive) {
			return;
		}

		if (metadata.exclusive && !this.hasExclusive) {
			this.tests.concurrent = [];
			this.tests.serial = [];
			this.hasExclusive = true;
		}

		if (metadata.serial) {
			this.tests.serial.push(test);
		} else {
			this.tests.concurrent.push(test);
		}
	}

	_skippedTest(test) {
		return {
			run: () => {
				this._emitTestResult({
					passed: true,
					result: test
				});

				return true;
			}
		};
	}

	_emitTestResult(result) {
		this.pendingTestInstances.delete(result.result);
		this.emit('test', result);
	}

	_buildHooks(hooks, testTitle, context) {
		return hooks.map(hook => {
			const test = this._buildHook(hook, testTitle, context);

			if (hook.metadata.skipped || hook.metadata.todo) {
				return this._skippedTest(test);
			}

			return test;
		});
	}

	_buildHook(hook, testTitle, contextRef) {
		let title = hook.title;

		if (testTitle) {
			title += ` for ${testTitle}`;
		}

		if (!contextRef) {
			contextRef = null;
		}

		const test = new Test({
			contextRef,
			failWithoutAssertions: false,
			fn: hook.fn,
			compareTestSnapshot: this.compareTestSnapshot,
			metadata: hook.metadata,
			onResult: this._emitTestResult,
			title
		});
		this.pendingTestInstances.add(test);
		return test;
	}

	_buildTest(test, contextRef) {
		if (!contextRef) {
			contextRef = null;
		}

		test = new Test({
			contextRef,
			failWithoutAssertions: this.failWithoutAssertions,
			fn: test.fn,
			compareTestSnapshot: this.compareTestSnapshot,
			metadata: test.metadata,
			onResult: this._emitTestResult,
			title: test.title
		});
		this.pendingTestInstances.add(test);
		return test;
	}

	_buildTestWithHooks(test) {
		if (test.metadata.skipped || test.metadata.todo) {
			return new Sequence([this._skippedTest(this._buildTest(test))], true);
		}

		const context = {context: {}};

		const beforeHooks = this._buildHooks(this.hooks.beforeEach, test.title, context);
		const afterHooks = this._buildHooks(this.hooks.afterEach, test.title, context);

		let sequence = new Sequence([].concat(beforeHooks, this._buildTest(test, context), afterHooks), true);
		if (this.hooks.afterEachAlways.length > 0) {
			const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterEachAlways, test.title, context));
			sequence = new Sequence([sequence, afterAlwaysHooks], false);
		}
		return sequence;
	}

	_buildTests(tests) {
		return tests.map(test => this._buildTestWithHooks(test));
	}

	_hasUnskippedTests() {
		return this.tests.serial.concat(this.tests.concurrent)
			.some(test => {
				return !(test.metadata && test.metadata.skipped === true);
			});
	}

	build() {
		const serialTests = new Sequence(this._buildTests(this.tests.serial), this.bail);
		const concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), this.bail);
		const allTests = new Sequence([serialTests, concurrentTests]);

		let finalTests;
		// Only run before and after hooks when there are unskipped tests
		if (this._hasUnskippedTests()) {
			const beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
			const afterHooks = new Sequence(this._buildHooks(this.hooks.after));
			finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
		} else {
			finalTests = new Sequence([allTests], true);
		}

		if (this.hooks.afterAlways.length > 0) {
			const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways));
			finalTests = new Sequence([finalTests, afterAlwaysHooks], false);
		}

		return finalTests;
	}

	attributeLeakedError(err) {
		for (const test of this.pendingTestInstances) {
			if (test.attributeLeakedError(err)) {
				return true;
			}
		}
		return false;
	}
}

module.exports = TestCollection;