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.

Dynamic hook (afterThis) to enable clean-up during test.

See original GitHub issue

🚀 Feature Proposal

During (integration) testing I generally have to prepare a lot of pre-conditions for a specific test and clean them up after. If I use the afterEach hook to run my cleanup (like delete all created users) then it blocks me from running my tests in parallel since it might delete users who are still being used by other tests.

It would be nice to have a afterThis hook which I can use inside my test to trigger clean-up when this particular test completes. This would work very similarly to golang’s defer which is an amazing tool during tests.

Example

describe('UsersAPI', () => {
  it('allows only admin users access', async () => {
    const nonAdminUser = createUser({admin: false}); // custom test utility
    afterThis(() => deleteUser(nonAdminUser.id)); // example of hook usage

    const adminUser = createUser({admin: true});
    afterThis(() => deleteUser(adminUser.id));

    // todo: do the actual tests and assertions
  });
});

Pitch

Since this functionality would be part of the core I don’t really see how someone could add this as elegantly as an external library/plugin. However, having this hook in jest would allow us to elegantly write tests which can embrace the parallel power of nodejs. Having a lot of tests is good, but running them faster is better!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ChappIOcommented, Dec 14, 2019

Why not put cleanup in the end of it?

For 3 reasons:

  1. Clarity. By declaring the clean-up in the same place the resource is created you will know for sure you did not forget to remove the resource.
  2. Failing tests. When 1 test fails it will not clean up its resources which may cause other tests to fail in turn. (Unless you add try/catch boilerplate to all your tests.)
  3. Helper functions. For brevity I didn’t do this in my example, but if I could move the afterThis hook into the createUser helper function, then I would never even have to think about cleanup in my test. Any helper function which creates resources will be responsible for cleaning them up too.
3reactions
SimenBcommented, Feb 28, 2022

Yeah, something like this would be great, but we would need some API to bind it to a single test. Need to think about this, but some sort of test.modern, test.strict, test.boundor some such is high on my list of wanted features. Might spend some time once v28 is released trying to design something and gather feedback. In addition to bound setups and cleanups I'd like to add a boundexpect` so we can properly count assertions and tell you how far you got

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic scenario freezes when called using afterFeature hook
For now, I have done the following workaround where I have added a test clean-up scenario at the end of the feature that...
Read more >
API Reference | React Hooks Testing Library
The cleanup function should be called after each test to ensure that previously rendered hooks will not have any unintended side-effects on the...
Read more >
Best Practices - Apache Airflow
You can use the Airflow CLI to purge old data with the command airflow db clean . See db clean usage for more...
Read more >
Documentation - SolidJS · Reactive Javascript Library
Such a cleanup function gets called both in between effect executions and when the ... listen to event dynamically given by eventName signal...
Read more >
3 Common Tasks — The Yocto Project ® 4.1.999 documentation
Check Existing Layers: Before creating a new layer, you should be sure someone ... in the filesystem is suitable for environments that use...
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