Feature request
If
test.beforeortest.afteris specified, it overrides the correspondingfixture.beforeEachandfixture.afterEachhook, so that the latter are not executed.
Taken from the documentation.
First, fixture.beforeEach is executed, then test.before is executed.
In case of teardown, test.after is executed first, then fixture.afterEach is executed.
Having multiple tests in the same common initialization logic, e.g. logging into a page.
But each individual test has it's own, specific initialization logic as well.
Maybe in the different tests, the website needs to be prepared slightly differently.
I think duplicating the code for every test.before is not en elegant solution.
In my opinion, fixture.before, fixture.beforeEach and test.before should all be called before a test starts.
I imagine it like this:
fixture.before calledfixture.beforeEach calledtest.before calledtest executedtest.after calledfixture.afterEach calledfixture.beforeEach calledtest.before calledtest executedtest.after calledfixture.afterEach calledfixture.after calledI had similar difficulty, but it seems to me that the only reason why test.before and test.after can be used is that they cancel beforeEach and afterEach. If they shouldn't cancel them then this
test(async t => { /* test */ })
.before(async t => { /* before */ })
.after(async => { /* after */ })
is basically same thing as
test(async t => {
try {
/* before */
/* test */
} finally {
/* after */
}
})
I think it's nice to have an override hook like this. With your proposed solution, the .beforeEach() hook can't be overwritten. The method for adding additional before and after using in the test itself like @bisubus mentioned looks fine to me.
I think it's nice to have an override hook like this. With your proposed solution, the
.beforeEach()hook can't be overwritten. The method for adding additional before and after using in the test itself like @bisubus mentioned looks fine to me.
I think overriding is counter intuitive.
In my case it confused my that a test's before overrides a fixture's beforeEach, eventually I found that in the docs.
If beforeEach on fixture level doesn't apply to all the fixture's tests then move it down to the tests' before hook.
Since testcafe doesn't support nested fixtures, you can't add a beforeEach to a subset of tests, which is a bummer.
The order @Lukas-Kullmann suggested is similar to jest, only I added a test.beforeEach:
(Fixture start)
fixture.before
(Test start)
fixture.beforeEach
test.beforeEach
... execute test ...
test.afterEach
fixture.afterEach
(Test end)
fixture.after
(Fixture end)