-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add property based testing based on fast-check in Jest #8035
Comments
Something useful for the integration: Patch version should not:
A minor should not:
|
See #7938 (comment) |
Having recently discovered #8032, just wanted to note that if we integrate something like this, we should definitely do it in a way that does not impact performance for users who do not use this feature (i.e. not unconditionally |
Great idea! |
Initially, I was not planning to add Indeed In a way, But I believe that pushing the property based testing through |
I'm not totally against adding something like this and I'd like to hear what others think about this. Just wanted to note that if there's no technical reason why it should be in Jest core, we could also consider promoting it in other ways (documentation, |
Totally agree, there is no technical reason to move it inside Jest core. Promoting the approach through documentation or Jest community might also be great. I let you see what's the best option ;) |
Potentially, Jest could tell the fuzzer to do more/less work depending on how much other work there is to do, whether it's watch mode or one-shot, and probably other considerations. But that would be a nice-to-have and probably need a deeper integration than is being considered. |
I really like the idea suggested by @quasicomputational. Indeed by default, property based tends to take longer than classical units because it runs 100 tests for each property. If Syntax: If not, I don't know if there is a simple and long-term way to know whether the test is running in watch mode. |
Another feature that would be more difficult to integrate if By default, what you would want to retry in the case of property based is not the test itself but the execution of one run of the property. |
@SimenB Any opinions on this RFC? |
Another benefit to having Jest know about the property tests: being able to isolate iterations better, including resetting mocks, timers and modules. This is particularly important to get right for shrinking, because if you've managed to contaminate the environment so that the property always fails, it'll be shrunk away to nothing and you get a useless report. |
|
Today, fast-check can easily be integrated into Jest without specific tooling. test('here is a jest test using fast-check imported as fc', () => {
fc.assert(
fc.property(
fc.nat(), fc.nat(),
(a, b) => expect(a + b).toBe(b + a)
)
);
}); The benefits of adding such technology directly into Jest will be:
Concerning benefits of fast-check over others. Today I would say shrinking capabilities go further (I can provide you example in private if you need to), replay options, verbose mode, pre-condition checks... |
it('normalizes the spans to strictly positive integers', () => {
check(
property(partGen, part => {
normalize(part).forEach(span => {
expect(span).toBeGreaterThan(0)
expect(Number.isInteger(span)).toBe(true)
})
}),
)
}) The reason I bring up alternatives is because the Jest API is already quite extensive and I wonder if adding property testing to the matchers is something that should be left to third-party libraries 🤔 I think it would definitely be useful as part of the built-in matchers. The question is if it should be built from scratch with a new API considering Jest's philosophy or if a third-party library should be embedded into it. I'd love to hear what the Jest core team's thoughts are on this. |
Can you interleave properties/generation around describe/it? For example:
Sometimes I want generator to be run for a "suite" rather than for each individual test, but only sometimes. |
@dubzzz having a examples:
|
If you are interested, I am working on a package to ease integration of fast-check with jest. See https://github.com/dubzzz/jest-fast-check All suggestions are welcomed (marchers, watch options for easier and faster replays). Meanwhile, having fast-check as a first class citizen would be even better 😊 |
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
Actually already done, closing it! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
@dubzzz there's an incoming Also, I'd be happy to link to |
Whaou such a good news. If I can access it I could definitely leverage it for |
I'm thinking of just putting it on So yeah import {jest} from '@jest/globals';
console.log(jest.getSeed()); might need a better name? |
What's the format for the seed? Double value? Int32? Uint32? Or maybe no strong guarantee?
|
If link is wonky - current implementation is whatever the user passed (which we should validate to ensure it conforms to our contract) or generating one in the shape of |
Perfect 🥰 I'll draft something into |
note that it hasn't landed it, but I hope to do so in a week or two (I'm heading on vacation tomorrow, but will probably find some computer time in the week I'm gone), but exciting to hear it'll work for your use case 👍 |
You'll probably have the answer quickly: is there a way to get the currently registered beforeEach out of jest? Or even better manually trigger it? Why would I need that for fast-check? Because I need to rerun beforeEach and afterEach before and after any execution of the predicate. So being able to either access the registered one or trigger it would help. |
@SimenB Not sure it's the case in the current 'seed' implementation but do you display the seed in case of failure? When manually defined it's questionable but seems to be needed in the automatic seed case. |
I don't think so - closest might be
Not decided. My current thinking is just landing a way of setting a seed, and it's up to the thing that uses this seed to print it |
Thanks for all these answers, I'll check them as soon as the version of Jest get released in order to improve as much as possible By the way, I'm planning to change the API of Not fully made my mind about how I'll do but here is the idea: 3303 It's probably a good target to have a very close API in |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
🚀 Feature Proposal
Property based testing can be seen as some kind of fuzzing approach. It makes users able to cover a wider range of inputs with a single test.
Motivation
fast-check
proved very useful on Jest and was behind the following issues:toEqual
not symmetric for Set #7975toStrictEqual
fails to distinguish 0 from 5e-324 #7941toStrictEqual
does not consider arrays with objects having undefined values correctly #7937 (for this one, the tests offast-check
failed because of the regression so I started to dig a bit and run property based on expect of Jest to see if I would be able to find other bugs like this one)It is currently being added in the test suites of Jest itself: #8012
Example
I think the integration of
fast-check
within Jest could be something like the one I did for ava: https://github.com/dubzzz/ava-fast-check/ but I am very opened to discussions on this point.My current wrapper works as follow:
testProp
- and certainlyitProp
for JesttestProp(testName, generators, property)
or maybe:
Pitch
Jest is a very successful testing framework.
Adding property based testing directly within Jest would give the community a new way to check their code. It might help the JavaScript community to detect new bugs they even not though about before.
fast-check
has already been useful in many projects, see https://github.com/dubzzz/fast-check/blob/master/documentation/IssuesDiscovered.mdThe text was updated successfully, but these errors were encountered: