ava: afterEach and beforeEach not running?
I’ve reproduced this bug in this repo. From the README there:
The
test
directory has abug.js
and aworkaround.js
.If you run:
npm install
and thennpm t
you’ll get:~/Developer/ava-beforeEach-afterEach-bug (master) ⛄ $ npm run test -s ✔ workaround › first log ✔ workaround › second log ✔ bug › first log ✖ bug › second log t.true(console.log.calledOnce) | false 1 test failed 1. second log AssertionError: false === true Test.fn (test/bug.js:16:3)
For some reason, it appears that perhaps the cleanup function is not run between the two test runs.
It’s using the latest version of AVA. Specifically, I spotted this here. Let me know if you need more info!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 18 (17 by maintainers)
@kentcdodds They don’t require that tests run serially. It’s because
console.log
is global, so shared between the two tests. This means thatconsole.log
is referring to the same spy. So in this situation, it’s necessary to run them serially. If you are able (which is not the case here) to use thecontext
object, you don’t have to run them serially because it’s a totally new object.Shared variable
Output:
Context variable
Output:
They are, but AVA is just too fast 😃.
Look at the execution flow
As you see, the
console.log
will be the same forfirst
andsecond
, being the one created in the second call tobeforeEach
. So yes, they are shared 😃.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.
@SamVerschueren On a side note, the above would be great as a recipe. Hint hint 😉