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.

Add fc.assertProperty function

See original GitHub issue

šŸš€ Feature Request

Add a function fc.assertProperty(...args) thatā€™s equivalent to fc.assert(fc.property(...args)). For consistency, there should perhaps also be fc.checkProperty, equivalent to fc.check(fc.property(...args)).

Motivation

Pretty much all of my tests look like fc.assert(fc.property(...args)). Itā€™d be nice to avoid the extra layer of nesting and just have an assertProperty function for these common cases. I migrated from js-verify to fast-check, and love it so far, but the lack of an analog to js-verifyā€™s assertforall has been a bit of a nuisance.

While I could easily define this function myself, itā€™s actually a little messy to define its type, since fc.property has so many overloaded signatures, and TS offers no easy way to have my wrapper function inherit all those overloads, so Iā€™d have to copy and paste them.

Example

it('should satisfy some property', () => {
  fc.assertProperty(fc.boolean(), (bool) => true);
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jdmarshallcommented, Apr 7, 2020

So first of all, Mocha suggests you not use arrow functions if you want to be able to utilize mocha internals at all, which I expect you might.

I think the interface could be even simpler than suggested above. If I work from the project description toward an implementation instead of vice versa, then I think tests would look something like this:

let fc = require("fast-check");
let all = fc.all;

describe("something()", function() {
    all("should satisfy some constraint", function(a = fc.boolean(), b = fc.boolean()) {
        let reply = something(a, b);    
        expect(reply).to.be.true;
    });
});

Thatā€™s it. All the magic is on one line. Everything else is bog-standard mocha, jest, or whatever. Nothing new to learn, no weird stack traces or unusual breakpoint stepping.

1reaction
dubzzzcommented, Dec 18, 2019

Thanks a lot for the suggestion. This is indeed a recurring question or suggestion for the lib. And I really think it will be useful to come with it quickly.

IMO one cool thing would be to directly provide test wrappers, such as it or test, with property based capabilities.

My target will be to provide some kind of itProp that could be used as follow:

import { itProp, fc } from "jest-fast-check"; // not yet published
itProp('should satisfy some property', [fc.boolean()], () => true);

It would directly wrap it, fc.assert and fc.property behind a single function.

I initially drafted this suggestion for ava but Iā€™ll soon implement it for others (see https://github.com/dubzzz/ava-fast-check/).

@ethanresnick Would it fit your need? If so Iā€™ll come back to you as soon as I get an implementation for other test runners.

Read more comments on GitHub >

github_iconTop Results From Across the Web

fast-check - Property based testing for JavaScript and TypeScript
Property based testing is another way to test programs. ... fc.assert runs the property; fc.property defines the property; fc.array(fc.integer()) definesĀ ...
Read more >
How to get started with property-based testing in JavaScript ...
In this case, we use Jest's built-in expect() function. To test our property, we pass it to fc.assert() . It does the work...
Read more >
Property Based Testing With Typescript - DEV Community ā€ ā€
fc.assert runs the property; fc.property defines the property ... For a first test, we add our indexOf test method to app.component.ts :.
Read more >
Introduction to Property Based Testing | by Nicolas Dubien
fc.assert(<property>(, parameters)) : It executes the test and checks that the property stays true for all the a, b, c strings produced by...
Read more >
fast-check.assert JavaScript and Node.js code examples
How to use. assert. function ... test('isEmpty vs nonEmpty', () => { fc.assert( fc.property(fc.anything(), value => { const option = Option.of(value) returnĀ ...
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