Skip to content

Commit f52ef08

Browse files
committed
docs: add fuzz testing section
1 parent ad97cd5 commit f52ef08

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

website/docs/development/testing.mdx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,39 @@ Cypress.Commands.add('login', (username, password) => {
275275
});
276276
});
277277
```
278+
279+
### Fuzz testing with fast-check
280+
281+
Fuzz testing helps find edge case bugs by generating random inputs for test data. This is very helpful since regular tests often have naive assumptions of users always inputting "expected" data.
282+
283+
Fuzz testing with fast-check is very easy: it integrates seamlessly with Mocha and it doesn't require any additional libraries beyond fast-check itself.
284+
285+
Here's an example of a fuzz test section for a test file (`testCheckRepoInAuthList.test.js`):
286+
287+
```js
288+
const fc = require('fast-check');
289+
290+
// Unit tests go here
291+
292+
describe('fuzzing', () => {
293+
it('should not crash on random repo names', async () => {
294+
await fc.assert(
295+
fc.asyncProperty(
296+
fc.string(),
297+
async (repoName) => {
298+
const action = new actions.Action('123', 'type', 'get', 1234, repoName);
299+
const result = await processor.exec(null, action, authList);
300+
expect(result.error).to.be.true;
301+
}
302+
),
303+
{ numRuns: 100 }
304+
);
305+
});
306+
});
307+
```
308+
309+
Writing fuzz tests is a bit different from regular unit tests, although we do still `assert` whether a certain value is correct or not. In this example, fc.string() indicates that a random string value is being generated for the `repoName` variable. This `repoName` is then inserted in the `action` to see if the `processor.exec()` function is capable of handling these or not.
310+
311+
In this case, we expect that the `result.error` value is always true. This means that the `exec` flow always errors out, but never crashes the app entirely. You may also want to test that the app is always able to complete a flow without an error.
312+
313+
Finally, we have the `numRuns` property for `fc.assert`. This allows us to run the fuzz test multiple times with a new randomized value each time. This is important since the test may randomly fail or pass depending on the input.

0 commit comments

Comments
 (0)