Skip to content

Commit

Permalink
test: Add property-based tests
Browse files Browse the repository at this point in the history
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.

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.
  • Loading branch information
dubzzz committed May 23, 2024
1 parent e02467e commit 07a58f5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
39 changes: 39 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
"eslint-config-strict": "^14.0.1",
"eslint-plugin-filenames": "^1.3.2",
"eslint-plugin-prettier": "^3.3.1",
"fast-check": "^3.19.0",
"husky": "^4.3.8",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
Expand Down
28 changes: 28 additions & 0 deletions test/property.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import inspect from '../lib/index.js'
import { expect } from 'chai'
import fc from 'fast-check'

Check failure on line 3 in test/property.js

View workflow job for this annotation

GitHub Actions / Lint

Identifier name 'fc' is too short (< 3)

describe('property', () => {
it('produces valid UTF-16 strings whatever the source', () => {
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.option(fc.nat(), { nil: undefined }),
(source, truncate) => {
expect(() => encodeURI(inspect(source, { truncate }))).not.to.throw()
}
)
)
})
})

0 comments on commit 07a58f5

Please sign in to comment.