-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
afterEach and beforeEach not running? #560
Comments
What's the |
Think I found the issue. You can also make use of the context object to store your original log. import test from 'ava';
import sinon from 'sinon';
test.beforeEach(t => {
t.context.log = console.log;
console.log = sinon.spy();
});
test.afterEach(t => {
console.log = t.context.log;
});
test('first', t => {
console.log('first');
t.true(console.log.calledOnce);
});
test('second', t => {
console.log('second');
t.true(console.log.calledOnce);
}); |
Yes, from the AVA readme:
So it has to be |
@sindresorhus Cool, didn't know it was possible. Thanks :). |
Actually I'm not trying to pass the |
@kentcdodds They don't require that tests run serially. It's because import test from 'ava';
import sinon from 'sinon';
test.beforeEach(t => {
t.context.log = sinon.spy();
});
test.afterEach(t => {
t.context.log.restore();
});
test('first', t => {
t.context.log('first');
t.true(t.context.log.calledOnce);
});
test('second', t => {
t.context.log('second');
t.true(t.context.log.calledOnce);
}); |
@SamVerschueren On a side note, the above would be great as a recipe. Hint hint ;) |
That won't work for my actual use-case. And it doesn't make sense to me that the tests are using the same spy because the spy should be reset before each test. |
@kentcdodds Can you elaborate on your use-case? They're not using the same spy. The hook is run before each test and with a unique |
They are, but AVA is just too fast :). Look at the execution flow
As you see, the |
@sindresorhus He was pointing to the use case without the context object
Will do my best to not forget. Should create a repository to keep track of my todos :). |
Shared variableimport test from 'ava';
let i = 0;
test.beforeEach(t => {
i++;
});
test.afterEach(t => {
i--;
});
test('first', t => {
console.log(i);
});
test('second', t => {
console.log(i);
}); Output:
Context variableimport test from 'ava';
test.beforeEach(t => {
t.context.i = (t.context.i || 0) + 1;
});
test.afterEach(t => {
t.context.i--;
});
test('first', t => {
console.log(t.context.i);
});
test('second', t => {
console.log(t.context.i);
}); Output:
|
Ah, I see. That appears to be a bug to me.
Correct. In my actual use case, my tests are asserting that something else is calling console.log |
It's not a bug, running tests concurrently comes with a "cost", this is one of them. Take a look at the number example (2 posts above this one) again. It shows the behaviour you experience. This is not something you can have in mocha (or others) because they are running tests serially and where the execution flow is easier to follow. |
What I do if I have a similar use case is only running that particular test serially. import test from 'ava';
import fn from './';
// Do spy stuff here
test.serial('test log', t => {
fn.logger('hello world');
t.true(console.log.calledOnce);
});
test('test something else', t => {
t.true(fn.somethingElse());
}); |
Ah, I think I understand now. This is part of how AVA helps (read: forces) you isolate your tests which is good. I'll submit a PR which mentions this. |
Exactly. From the moment you have something shared (which might not seem to be shared in the first place), you will have to execute them serially. |
@kentcdodds I believe this is the same problem that I was having with the shared The workaround I came up with was to use |
I've reproduced this bug in this repo. From the README there:
It's using the latest version of AVA. Specifically, I spotted this here. Let me know if you need more info!
The text was updated successfully, but these errors were encountered: