Skip to content

Commit 9b2f2e1

Browse files
author
Nick Jaggars
committed
Dont run before and after hooks when all tests are skipped (avajs#1283)
1 parent b3c4090 commit 9b2f2e1

File tree

4 files changed

+124
-4
lines changed

4 files changed

+124
-4
lines changed

lib/test-collection.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,32 @@ class TestCollection extends EventEmitter {
178178
_buildTests(tests) {
179179
return tests.map(test => this._buildTestWithHooks(test));
180180
}
181+
_hasUnskippedTests() {
182+
const getUnskippedCount = tests => tests.filter(test => {
183+
return !(test.metadata && test.metadata.skipped === true);
184+
}).length;
185+
return (getUnskippedCount(this.tests.serial) + getUnskippedCount(this.tests.concurrent)) > 0;
186+
}
181187
build() {
182-
const beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
183-
const afterHooks = new Sequence(this._buildHooks(this.hooks.after));
184-
185188
const serialTests = new Sequence(this._buildTests(this.tests.serial), this.bail);
186189
const concurrentTests = new Concurrent(this._buildTests(this.tests.concurrent), this.bail);
187190
const allTests = new Sequence([serialTests, concurrentTests]);
188191

189-
let finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
192+
let finalTests;
193+
// Only run before and after hooks when there are unskipped tests
194+
if (this._hasUnskippedTests()) {
195+
const beforeHooks = new Sequence(this._buildHooks(this.hooks.before));
196+
const afterHooks = new Sequence(this._buildHooks(this.hooks.after));
197+
finalTests = new Sequence([beforeHooks, allTests, afterHooks], true);
198+
} else {
199+
finalTests = new Sequence([allTests], true);
200+
}
201+
190202
if (this.hooks.afterAlways.length > 0) {
191203
const afterAlwaysHooks = new Sequence(this._buildHooks(this.hooks.afterAlways));
192204
finalTests = new Sequence([finalTests, afterAlwaysHooks], false);
193205
}
206+
194207
return finalTests;
195208
}
196209
attributeLeakedError(err) {

test/api.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,18 @@ function generateTests(prefix, apiCreator) {
714714
});
715715
});
716716

717+
test(`${prefix} test file with only skipped tests does not run hooks`, t => {
718+
const api = apiCreator();
719+
720+
return api.run([path.join(__dirname, 'fixture/hooks-skipped.js')])
721+
.then(result => {
722+
t.is(result.tests.length, 1);
723+
t.is(result.skipCount, 1);
724+
t.is(result.passCount, 0);
725+
t.is(result.failCount, 0);
726+
});
727+
});
728+
717729
test(`${prefix} resets state before running`, t => {
718730
const api = apiCreator();
719731

test/fixture/hooks-skipped.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import test from '../..';
2+
3+
test.before(() => {
4+
throw new Error('should not run');
5+
});
6+
7+
test.after(() => {
8+
throw new Error('should not run');
9+
});
10+
11+
test.beforeEach(() => {
12+
throw new Error('should not run');
13+
});
14+
15+
test.afterEach(() => {
16+
throw new Error('should not run');
17+
});
18+
19+
test.skip('some skipped test', t => {
20+
t.fail();
21+
});

test/test-collection.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,80 @@ test('adding a bunch of different types', t => {
239239
t.end();
240240
});
241241

242+
test('skips before and after hooks when all tests are skipped', t => {
243+
t.plan(5);
244+
245+
const collection = new TestCollection({});
246+
collection.add({
247+
metadata: metadata({type: 'before'}),
248+
fn: a => a.fail()
249+
});
250+
collection.add({
251+
metadata: metadata({type: 'after'}),
252+
fn: a => a.fail()
253+
});
254+
collection.add({
255+
title: 'some serial test',
256+
metadata: metadata({skipped: true, serial: true}),
257+
fn: a => a.fail()
258+
});
259+
collection.add({
260+
title: 'some concurrent test',
261+
metadata: metadata({skipped: true}),
262+
fn: a => a.fail()
263+
});
264+
265+
const log = [];
266+
collection.on('test', result => {
267+
t.is(result.result.metadata.skipped, true);
268+
t.is(result.result.metadata.type, 'test');
269+
log.push(result.result.title);
270+
});
271+
272+
collection.build().run();
273+
274+
t.strictDeepEqual(log, [
275+
'some serial test',
276+
'some concurrent test'
277+
]);
278+
279+
t.end();
280+
});
281+
282+
test('skips beforeEach and afterEach hooks when test is skipped', t => {
283+
t.plan(3);
284+
285+
const collection = new TestCollection({});
286+
collection.add({
287+
metadata: metadata({type: 'beforeEach'}),
288+
fn: a => a.fail()
289+
});
290+
collection.add({
291+
metadata: metadata({type: 'afterEach'}),
292+
fn: a => a.fail()
293+
});
294+
collection.add({
295+
title: 'some test',
296+
metadata: metadata({skipped: true}),
297+
fn: a => a.fail()
298+
});
299+
300+
const log = [];
301+
collection.on('test', result => {
302+
t.is(result.result.metadata.skipped, true);
303+
t.is(result.result.metadata.type, 'test');
304+
log.push(result.result.title);
305+
});
306+
307+
collection.build().run();
308+
309+
t.strictDeepEqual(log, [
310+
'some test'
311+
]);
312+
313+
t.end();
314+
});
315+
242316
test('foo', t => {
243317
const collection = new TestCollection({});
244318
const log = [];

0 commit comments

Comments
 (0)