Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for beforeEach/afterEach in fast-check/jest #3942

Open
hsommerlandreason opened this issue May 31, 2023 · 5 comments
Open

Support for beforeEach/afterEach in fast-check/jest #3942

hsommerlandreason opened this issue May 31, 2023 · 5 comments

Comments

@hsommerlandreason
Copy link

馃殌 Feature Request

I am having issues using sinon to mock things when running fast-check with jest, one cannot re-mock something with sinon that has already been mocked.
So after each test the mocks have to be restored, this can be done via either the afterEach hook for a specific property test or with the global hooks when not using fast-check/jest.

But when using fast-check/jest these hooks are not available (at least not what i could find) and the global hooks don't seem to be applied. The beforeEach/afterEach hooks provided by jest does not work as they are not executed on every single check which they need to be.

This also applies for the cleanup method from @testing-library/react which will also need to be executed after every test.

Motivation

This would be very appreciated as the omission of this feature from fast-check/jest makes testing several things a lot more complicated and annoying requiring either manually calling the cleanup stuff or constructing workarounds.

Example

Here is a bit of a contrived example.

const whatever = {
 bob: (n: number) => n - 10
}

test.prop([fc.nat()])('failing', (n) => {
 sinon.replace(whatever, 'bob', sinon.fake.returns(n))
 expect(whatever.bob(n)).toEqual(n)
 // sinon.restore()  /* if this is called here the tests will work.
})

test('works', () => {
 fc.assert(fc.property(fc.nat(), (n) => {
   sinon.replace(whatever, 'bob', sinon.fake.returns(n))
   expect(whatever.bob(n)).toEqual(n)
   }).afterEach(() => sinon.restore())
 )
})

This will fail with:

 Property failed after 2 tests
   { seed: 1576879931, path: "1:0", endOnFailure: true }
   Counterexample: [0]
   Shrunk 1 time(s)
   Got TypeError: Attempted to replace bob which is already replaced
@dubzzz
Copy link
Owner

dubzzz commented Jun 9, 2023

Definitely something worth adding. It would require te have a way to either hook into the beforeEach defined for Jest or expose one via fast-check and fill the one of Jest with it.

@dmurvihill
Copy link

+1 for this, I'm trying to use with jest-mock-axios and having trouble resetting the mock between tests.

@dubzzz
Copy link
Owner

dubzzz commented Jun 14, 2023

For now, you can use https://fast-check.dev/docs/configuration/global-settings/#shared-settings with beforeEach or beforeEachAsync

@ej-shafran
Copy link
Contributor

Hey - any updates on this? If needed, I can try implementing this and open a pull request if I manage to...

@dubzzz
Copy link
Owner

dubzzz commented Jan 29, 2024

@ej-shafran I have not worked on this issue recently. You can give it a try if you want

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants