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.

afterEach and beforeEach for property tests

See original GitHub issue

I’m trying to make a property based test based on actually manipulating the dom and then comparing two different dom states.

I’m using the mocha test runner. However, I’m having a problem with the state getting out of sync because the afterEach and beforeEach triggers for mocha do not trigger pre and post fast-check test as they’re separate systems. So naturally I’ve moved most of the setup and tear down code into the property test predicate. This gets pretty cluttered.

What do you think about adding hooks in the assert parameters for running setup/teardown? Something like the following.

export interface Parameters<T = void> {
    seed?: number;      // optional, initial seed of the generator: Date.now() by default
    numRuns?: number;   // optional, number of runs before success: 100 by default
    maxSkipsPerRun?: number; // optional, maximal number of skipped entries per run: 100 by default
    timeout?: number;   // optional, only taken into account for asynchronous runs (asyncProperty)
                        // specify a timeout in milliseconds, maximum time for the predicate to return its result
                        // only works for async code, will not interrupt a synchronous code: disabled by default
    path?: string;      // optional, way to replay a failing property directly with the counterexample
                        // it can be fed with the counterexamplePath returned by the failing test (requires seed too)
    logger?: (v: string) => void; // optional, log output: console.log by default
    unbiased?: boolean; // optional, force the use of unbiased arbitraries: biased by default
    verbose?: boolean;  // optional, enable verbose mode: false by default
                        // when enabling verbose mode,
                        // you will be provided the list of all failing entries encountered
                        // whenever a property fails - useful to detect patterns
    examples?: T[];     // optional, custom values added to generated ones: [] by default
                        // when set, those examples will be run against the property first
                        // followed by the values generated by the framework
                        // they do not increase the number of times the property will be launched
    afterEach?: () => void; //runs teardown code after each property test
    beforeEach?: () => void; //runs setup code before each property test begins
}

Or maybe add them to the property

interface IProperty<Ts> {
    ...
    afterEach?: () => void; //runs teardown code after each property test
    beforeEach?: () => void; //runs setup code before each property test begins
}

It isn’t a super important issue, but it might be a small quality of life feature.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
MehdiSaffarcommented, Jun 29, 2020

@dubzzz I wasn’t aware of the beforeEach and afterEach hooks of fast-check. Thanks a lot.

0reactions
dubzzzcommented, Oct 13, 2022

You can use: fc.configureGlobal({beforeEach, afterEach}). But otherwise so far, there is no such capability in @fast-check/jest. You should definitely open an issue so that it can be tracked

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Jest beforeEach to write better unit tests in JavaScript ...
Jest is one of the most popular testing frameworks in JavaScript. In this post, we will delve into using Jest beforeEach properly to...
Read more >
How I Was Completely Wrong About Setting Up/Tearing Down ...
Because of this, your describe callbacks essentially have the job of registering all of your it , beforeEach , beforeAll , afterAll ,...
Read more >
How to deal with @BeforeEach @AfterEach for all test except ...
A test class has several tests, each one depends on the @BeforeEach and is teared down by @AfterEach , except one black sheep!...
Read more >
Globals - Jest
afterEach (fn, timeout) ​ ... Runs a function after each one of the tests in this file completes. If the function returns a...
Read more >
Testing Recipes - React
A common way to do it is to use a pair of beforeEach and afterEach blocks so that they'll always run and isolate...
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