Testcafe: Using fixture.beforeEach/fixture.afterEach and test.before/test.after at the same time

Created on 20 Mar 2018  路  3Comments  路  Source: DevExpress/testcafe

Are you requesting a feature or reporting a bug?

Feature request

What is the current behavior?

If test.before or test.after is specified, it overrides the corresponding fixture.beforeEach and fixture.afterEach hook, so that the latter are not executed.

Taken from the documentation.

What is the expected behavior?

First, fixture.beforeEach is executed, then test.before is executed.
In case of teardown, test.after is executed first, then fixture.afterEach is executed.

Usecase

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 called
  • test 1:

    • fixture.beforeEach called

    • test.before called

    • test executed

    • test.after called

    • fixture.afterEach called

  • test 2:

    • fixture.beforeEach called

    • test.before called

    • test executed

    • test.after called

    • fixture.afterEach called

  • continue for test 3, 4, ...
  • fixture.after called
level 2 API enhancement

All 3 comments

I 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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

KaneMorgan picture KaneMorgan  路  3Comments

multivoltage picture multivoltage  路  3Comments

AndreyBelym picture AndreyBelym  路  3Comments

devmondo picture devmondo  路  3Comments

jvanoostveen picture jvanoostveen  路  4Comments