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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add property-based tests #80

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dubzzz
Copy link
Contributor

@dubzzz dubzzz commented May 23, 2024

Following the discussion started on #79. I'm opening this PR as an opened question and suggestion to better cover the library and its edge-cases.

Feel free to close it 👍


First, what is property-based testing? It's a technic coming from functional world and aiming to help devs into detecting edge cases without having them to think of them too much.

Why, property-based testing? Well, in order to reduce the risks of bugs and potentially regressions on key libraries.

Could it found problems? Well, probably. I tried another property but I'm not sure whether or not the failure is considered as ok or not. As such I'm not sure of what to expect from truncate so I was not able to make a decision.

it('produces strings with at most <truncate> characters', () => {
  fc.assert(
    fc.property(
      fc.anything({
        withBigInt: true,
        withBoxedValues: true,
        withDate: true,
        withMap: true,
        withNullPrototype: true,
        withObjectString: true,
        withSet: true,
        withSparseArray: true,
        withTypedArray: true,
        withUnicodeString: true,
      }),
      fc.nat(),
      (source, truncate) => {
        const output = inspect(source, { truncate })
        expect(output).to.have.lengthOf.at.most(truncate)
      }
    )
  )
})

Who am I? I'm the author of fast-check, the library added by this PR. It's the leading property-based testing library today.

Following the discussion started on chaijs#79. I'm opening this PR as an opened question and suggestion to better cover the library and its edge-cases.

First, what is property-based testing?

It's a technic coming from functional world and aiming to help devs into detecting edge cases without having them to think of them too much.

Why, property-based testing?

Well, in order to reduce the risks of bugs and potentially regressions on key libraries.

Could it found problems?

Well, probably. I tried another property but I'm not sure whether or not the failure is considered as ok or not. As such I'm not sure of what to expect from truncate so I was not able to make a decision.

```js
it('produces strings with at most <truncate> characters', () => {
  fc.assert(
    fc.property(
      fc.anything({
        withBigInt: true,
        withBoxedValues: true,
        withDate: true,
        withMap: true,
        withNullPrototype: true,
        withObjectString: true,
        withSet: true,
        withSparseArray: true,
        withTypedArray: true,
        withUnicodeString: true,
      }),
      fc.nat(),
      (source, truncate) => {
        const output = inspect(source, { truncate })
        expect(output).to.have.lengthOf.at.most(truncate)
      }
    )
  )
})
```

Who am I?

I'm the author of fast-check, the library added by this PR. It's the leading property-based testing library today.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant