-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parameterized Tests Can Create Multiple beforeEach Executions #4072
Comments
@josh-cain thank you for this issue and your excellent test examples. I will have a look. The order of the |
Your first example Your second example IMO the output of your examples is as expected and no bug. describe('a non-duplicating child scenario', () => {
beforeEach('before each root', () => {
console.log('before each root operation');
});
afterEach('after each root', () => {
console.log('after each root operation');
});
const testCases = [
{ description: 'test case 1', arg: 1, expected: 1 },
{ description: 'test case 2', arg: 2, expected: 4 }
];
// describe('using a namespaced suite', () => { // works with or without this suite level
beforeEach('before each child', () => {
console.log('before each child operation');
});
afterEach('after each child', () => {
console.log('after each child operation');
});
testCases.forEach((testCase) => {
it(`should square a number for ${testCase.description}`, () => {
assert.equal(square(testCase.arg), testCase.expected);
});
});
// });
}); |
@juergba - thanks for getting back! I see what you're saying, and totally agree with your evaluation. However, to someone who is new to mochajs (like me!), this has the potential to wind up as a foot-gun. How about I see about adding a little more to the existing docs on this use case? |
What is your proposal regarding docs? Adding hooks to the example? |
@juergba sure, or at least mention the algorithm used to construct hooks and the way it interacts with the parameterized for loop. I'll send a PR and we can continue discussion there? |
Prerequisites
faq
labelnode node_modules/.bin/mocha --version
(Local) andmocha --version
(Global). We recommend that you not install Mocha globally.Description
In certain cases with dynamically generated tests (like the one below), the same
beforeEach
orafterEach
hook will be fired n times for each test. I can see why, in that thesuite
is constructed by scanning/enumerating all of thebeforeEach
/afterEach
in the current scope, and since the.forEach
loop has no child scopes it simply builds the suite with a hook for every iteration.However, this feels like really strange behavior. Could this maybe be a case for a parameterized mechanism?
Steps to Reproduce
Just run this to observe:
Expected behavior:
Each
beforeEach
andafterEach
hook inside the parameterizedforEach
ran once per test.Actual behavior: [What actually happens]
Each
beforeEach
andafterEach
hook inside the parameterizedforEach
ran twice per test. Have also tried withn
items in thetestCases
array, and this will result in thebeforeEach
andafterEach
methods being runn
times per test case.Sample output:
Reproduces how often:
Every time.
Versions
Tested on 3.3.0 and 6.2.0
Additional Information
nope.
The text was updated successfully, but these errors were encountered: