-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
### Description of bug Mocha shows an incorrect test-title if a nested `before` hook fails. For `after` hooks the second part of the message is completely missing. ```js describe('Top-level describe', () => { it('Top-level it', () => { }); // the failing before-hook is pointing to this test describe('Nested describe', () => { before(() => { throw new Error(); }); it('Nested it', () => { }); // but it should point to this test }); }); ``` ``` Top-level describe ✓ Top-level it Nested describe 1) "before all" hook for "Top-level it" <<<incorrect ``` Root problem: see [#1638](#1638). `hook.ctx.currentTest` is set incorrectly in the hook before emitting the hook event: - `before` hook: currentTest is undefined or points to the outer test already past successfully. The runner hasn't reached the next test to come yet, so it's too early to derive currentTest from the runner. Instead the first test of hook's parent is used. - `after` hook: currentTest is undefined. The runner has already completed the latest test and runner.currentTest is deleted. Instead the last test of hook's parent is used. ### Description of the Change #### 1. Setting currentTest correctly - `before` hook: hook.ctx.currentTest is set to the first test of the parent suite - `after hook`: hook.ctx.currentTest is set to the last test of the parent suite - for nested suites just the current suite is searched for tests to come. When no tests can be found, currentTest remains undefined. #### 2. Creating title in hook failure message A correct `hook.ctx.currentTest` renders a correct title. When no currentTest can be found, the title of the parent suite is used for the hook failure message. ### Alternate Designs PR [#3333](#3333) ### Benefits This is a rather old bug back to 2015. ### Applicable issues closes [#1638](#1638) closes [#3291](#3291) closes [#2134](#2134) **EDITED by @boneskull**: added js syntax highlighting, comments
- Loading branch information
Showing
14 changed files
with
271 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
var assert = require('assert'); | ||
|
||
function getTitle(ctx) { | ||
return ctx.currentTest && ctx.currentTest.title; | ||
}; | ||
|
||
before(function () { | ||
assert.equal(getTitle(this), undefined); | ||
}); | ||
|
||
describe('suite A', () => { | ||
|
||
before(function () { | ||
assert.equal(getTitle(this), undefined); | ||
}); | ||
|
||
describe('suite B', () => { | ||
|
||
it('test1 B', () => {}); | ||
|
||
describe('suite C', function () { | ||
var lap = 0; | ||
|
||
before(function () { | ||
assert.equal(getTitle(this), 'test1 C'); | ||
}); | ||
beforeEach(function () { | ||
assert.equal(getTitle(this), ++lap === 1 ? 'test1 C' : 'test2 C'); | ||
}); | ||
|
||
it('test1 C', function () {}); | ||
it('test2 C', function () {}); | ||
|
||
afterEach(function () { | ||
assert.equal(getTitle(this), lap === 1 ? 'test1 C' : 'test2 C'); | ||
}); | ||
after(function () { | ||
assert.equal(getTitle(this), 'test2 C'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
after(function () { | ||
assert.equal(getTitle(this), undefined); | ||
}); |
13 changes: 13 additions & 0 deletions
13
test/integration/fixtures/hooks/after-hook-deepnested-error.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
describe('spec 1', function () { | ||
it('should pass', function () { }); | ||
describe('spec 2 nested - this title should be used', function () { | ||
after(function() { | ||
throw new Error('after hook nested error'); | ||
}); | ||
describe('spec 3 nested', function () { | ||
it('it nested - this title should not be used', function () { }); | ||
}); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
test/integration/fixtures/hooks/after-hook-nested-error.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict'; | ||
|
||
describe('spec 1', function () { | ||
it('should pass', function () { }); | ||
describe('spec nested', function () { | ||
after(function() { | ||
throw new Error('after hook nested error'); | ||
}); | ||
it('it nested - this title should be used', function () { }); | ||
}); | ||
describe('spec 2 nested', function () { | ||
it('it nested - not this title', function () { }); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
test/integration/fixtures/hooks/before-hook-deepnested-error.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
describe('spec 1', function () { | ||
it('should pass', function () { }); | ||
describe('spec 2 nested - this title should be used', function () { | ||
before(function() { | ||
throw new Error('before hook nested error'); | ||
}); | ||
describe('spec 3 nested', function () { | ||
it('it nested - this title should not be used', function () { }); | ||
}); | ||
}); | ||
}); |
11 changes: 11 additions & 0 deletions
11
test/integration/fixtures/hooks/before-hook-nested-error.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict'; | ||
|
||
describe('spec 1', function () { | ||
it('should pass', function () { }); | ||
describe('spec nested', function () { | ||
before(function() { | ||
throw new Error('before hook nested error'); | ||
}); | ||
it('it nested - this title should be used', function () { }); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
test/integration/fixtures/hooks/before-hook-root-error.fixture.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
before(function() { | ||
throw new Error('before hook root error'); | ||
}); | ||
|
||
describe('spec 1', function () { | ||
it('should not be called', function () { }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.