Skip to content

Ability to Apply decorator to all tests within block #3404

@mollerhoj

Description

@mollerhoj

Clear and concise description of the problem

Inspired by other test runners, I wish to have the ability to wrap multiple tests in a function.

This differes from (and is more powerful than) using beforeEach and afterEach because we can use functionality that rely on closures.

Common usecases:

  1. Wrapping tests in database transactions (for speed and consistency).
decorateEach((runTest) => {
  database.transaction(() => {
    await runTest();
  });
});
  1. Custom benchmarking of time / memory etc:
decorateEach((runTest) => {
  rbenchmark.report(() => {
    await runTest();
  });
});
  1. Freezing time by mocking Date and Date.now:
decorateEach((runTest) => {
  dateMocker.setTime("2023-01-01", () => {
    await runTest();
  });
});

I propose that the feature should be implemented such that common decorators can be applied either to all tests, or to all tests within a describe blocks.

If the team agrees the this feature is valuable, I have been allowed by my employer that I can spend work hours implementing it.

Suggested solution

I propose to use an API such as the one shown above. Suggestions for alternative naming etc are more than welcome.

Alternative

I know of no good alternative. One could use Typescripts Decorators, but that would require adding in a decorator for every test.

Additional context

This has also been requested here:
#3232

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions