From 1cc4d53890a0e8d2c6d3e365be51c402e33abc2f Mon Sep 17 00:00:00 2001 From: Gary Borton Date: Mon, 10 Apr 2017 08:05:46 -0700 Subject: [PATCH] [lifecycle hooks] Make afterAll hooks operate in the fashion as afterEach. It was discovered that afterAll hooks run in the same order that you add them, while afterEach hooks were running in reverse order. This commit makes their order consistent, and adds regression tests. Relevant issue - #3268 --- .../integration/lifecycle-hooks-test.js | 47 +++++++++++++++++++ packages/jest-jasmine2/src/jasmine/Suite.js | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/jest-jasmine2/src/__tests__/integration/lifecycle-hooks-test.js diff --git a/packages/jest-jasmine2/src/__tests__/integration/lifecycle-hooks-test.js b/packages/jest-jasmine2/src/__tests__/integration/lifecycle-hooks-test.js new file mode 100644 index 000000000000..5239e5b10439 --- /dev/null +++ b/packages/jest-jasmine2/src/__tests__/integration/lifecycle-hooks-test.js @@ -0,0 +1,47 @@ +describe('test lifecycle hooks', () => { + const actions = []; + function pushMessage(message) { + return () => { + actions.push(message); + }; + } + + // Validate in an afterAll to prevent previous hooks from firing again. + // Note that when operating correctly, this afterAll will be called last. + afterAll(() => { + const expected = [ + 'runner beforeAll1', + 'runner beforeAll2', + 'runner beforeEach1', + 'runner beforeEach2', + 'beforeEach1', + 'beforeEach2', + 'outer it 1', + 'afterEach2', + 'afterEach1', + 'runner afterEach2', + 'runner afterEach1', + 'runner afterAll2', + 'runner afterAll1' + ]; + + expect(actions).toEqual(expected); + }); + + beforeAll(pushMessage('runner beforeAll1')); + afterAll(pushMessage('runner afterAll1')); + beforeAll(pushMessage('runner beforeAll2')); + afterAll(pushMessage('runner afterAll2')); + beforeEach(pushMessage('runner beforeEach1')); + afterEach(pushMessage('runner afterEach1')); + beforeEach(pushMessage('runner beforeEach2')); + afterEach(pushMessage('runner afterEach2')); + + describe('Something', () => { + beforeEach(pushMessage('beforeEach1')); + afterEach(pushMessage('afterEach1')); + beforeEach(pushMessage('beforeEach2')); + afterEach(pushMessage('afterEach2')); + it('does it 1', pushMessage('outer it 1')); + }); +}); diff --git a/packages/jest-jasmine2/src/jasmine/Suite.js b/packages/jest-jasmine2/src/jasmine/Suite.js index 6648b4468b92..3f68f5640d11 100644 --- a/packages/jest-jasmine2/src/jasmine/Suite.js +++ b/packages/jest-jasmine2/src/jasmine/Suite.js @@ -92,7 +92,7 @@ Suite.prototype.afterEach = function(fn) { }; Suite.prototype.afterAll = function(fn) { - this.afterAllFns.push(fn); + this.afterAllFns.unshift(fn); }; Suite.prototype.addChild = function(child) {