question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for `beforeEach` and `afterEach`

See original GitHub issue

Coming from node-tap which is way to slow for my taste I wonder if zora has support for beforeEach and afterEach patterns?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
lorenzofox3commented, Nov 22, 2019

It is tempting but I consider such hooks anti patterns when in comes to testing, especially for unit testing(so do I for snapshots but it is another story).
Such “features” will probably not make it in zora’s core but sometimes people make me change my mind 😃

However zora is quite simple to extend, you can for example create a decorator:

// beforeEach decorator
// name the function with 'zora_spec_fn' to make sure location is correct
const withBeforeEach = (spec) => function zora_spec_fn(assert) {
    const hooks = [];
    assert.beforeEach = fn => hooks.push(fn);
    const originalTest = assert.test.bind(assert);
    assert.test = function (...args) {

        const [description, specFn, ...rest] = args;

        for (const h of hooks) {
            h();
        }

        // we decorate specFn too, so beforeEach can be used recursively in nested tests as well without calling the decorator
        return originalTest(description, withBeforeEach(specFn), ...rest);
    };

    return spec(assert);
};

to be used as

//src code to test
const sum = (a, b) => a + b

test('some test', withBeforeEach(t => {

    let input = 0;

    t.beforeEach(() => input++);

    t.test(`foo`, t => {
        t.eq(sum(input, 1), 2);
    });

    t.test(`another foo`, t => {
        t.eq(sum(input, 1), 2); // fails (should be 3) to test whether "at" location is correct
    });

    t.test('nested', t => {
        let input = 0;

        t.beforeEach(() => input++);

        t.test(`foo`, t => {
            t.eq(sum(input, 1), 2);
        });

        t.test(`another foo`, t => {
            t.eq(sum(input, 1), 2); // fails (should be 3) to test whether "at" location is correct
        });
    });
}));

A compromise would be to ship such helper functions with zora or within another package

1reaction
lorenzofox3commented, Oct 2, 2020

To be honest I don’t know, I have not dug too much. The hooks I mentioned in this thread are just examples to consider as so, and are not part of zora.

I personally find the usage of such constructs a bad practice (that is why they are not part of the core library) and would rather consider each test independently.

test(`some test`, () => {
    prepare();

    // do some expectations

   teardown()
})

test(`some other test`, () => {
    prepare();

    // do some expectations

   teardown()
})

Which are more explicit and easier to maintain in the long term. I would eventually factorize with a spec decorator to follow DRY principles (like any code) but certainly not use things like beforeEach which create global context and tend to introduce coupling between the tests.

That being said. You are correct: by default zora run all the spec function concurrently. However you can explicitly maintain a shared state or a sequence between sub tests with the async/await control flow like would to for any other JS code:


test(`some test suite`, async (t) => {
   let state = 0;

   //explicitly wait for the test to complete before moving on
  await t.test(`sub test 1`,async (t) => {
       await wait();
       t.eq(state, 0);
       state++;
  });

  await t.test(`sub test 2`,async (t) => {
       await wait();
       t.eq(state, 1);
       state++;
  });

  // etc

})

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress basics: before(), beforeEach(), after() and afterEach()
I like to think of all hooks as being "squashed" together before executing a test or a describe() block. Using beforeEach block in...
Read more >
Setup and Teardown - Jest
Jest provides beforeAll and afterAll hooks to handle this situation. ... It may help to illustrate the order of execution of all hooks....
Read more >
Effective Use of beforeEach and afterEach in Angular Unit Tests
There are several reasons why using the beforeEach and afterEach methods can be beneficial when writing unit tests in Angular. First, using ...
Read more >
beforeEach and afterEach inside support/index.js not being ...
I have some initialization scripts that run a bash script with cy.exec inside beforeEach and afterEach in support/index.js file that should ...
Read more >
34. Implementation of beforeEach and afterEach methods in ...
In this video we will see how to implement beforeEach and afterEach lifecycle methods in the Test suite - Jasmine Testing Unit Testing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found