1
0
Fork 0
mocha-foam/test/TestOver.ts

83 lines
2.1 KiB
TypeScript
Raw Normal View History

import { expect } from 'chai';
2021-07-22 17:54:02 +00:00
import { array, defaultReportMessage, integer, lorem, tuple, uuid } from 'fast-check';
import { describe } from 'mocha';
2021-07-21 04:20:39 +00:00
import { over } from '../src/index.js';
2021-07-21 04:20:39 +00:00
const LARGE_VALUE = Math.floor(Math.random() * 1_000_000_000);
describe('example properties', () => {
over('some numbers', integer(), (it) => {
it('should be a small number', (n: number) => {
return n < LARGE_VALUE;
2021-07-21 04:54:15 +00:00
});
it('should be even', (n: number) => {
return n % 2 === 0;
2021-07-21 04:20:39 +00:00
});
2021-07-21 23:28:22 +00:00
it('should not throw', (n: number) => {
throw new Error('bad number!');
2021-07-21 23:28:22 +00:00
});
2021-07-22 17:54:02 +00:00
it('should resolve async checks', async (n: number) => {
expect(n).to.be.lessThanOrEqual(90);
});
2021-07-21 04:20:39 +00:00
});
2021-07-21 04:50:21 +00:00
over('some IDs', uuid(), (it) => {
// beforeEach hooks work normally, since the wrapped it calls through to real it
2021-07-21 23:28:22 +00:00
beforeEach(() => {
console.log('before each ID test');
});
2021-07-21 04:50:21 +00:00
it('should be a good one', (id: string) => {
return id[9] !== 'a';
});
2021-07-21 23:28:22 +00:00
it('should be long enough', (id: string) => {
expect(id).to.have.length.greaterThan(2);
2021-07-21 23:28:22 +00:00
});
}, {
// fast-check parameters are supported, like examples
examples: ['a', 'b'],
2021-07-22 18:18:45 +00:00
numRuns: 1_000,
2021-07-21 04:50:21 +00:00
});
2021-07-21 05:13:20 +00:00
over('even numbers', integer().filter(n => n % 2 === 0), (it) => {
it('should be even', (n: number) => {
return n % 2 === 0;
});
});
over('mapped properties', tuple(lorem(), integer()).map(([a, b]) => a.substr(b)), (it) => {
it('should have content', (text: string) => {
return text.length > 0;
});
}, {
// error formatting can be overridden with a custom handler, or fast-check's default
errorReporter: defaultReportMessage,
});
over('tuples', tuple(integer(), integer()), (it) => {
// tuple properties are passed as a single parameter
it('should not be equal', ([a, b]) => {
return a === b;
});
it('should be uneven', ([a, b]) => {
return a < b;
});
}, {
examples: [[1, 2]]
});
over('arrays', array(integer()), (it) => {
it('should have items', (t: Array<number>) => {
expect(t).to.have.length.lessThan(5_000);
});
}, {
numRuns: 1_000,
2021-07-21 05:13:20 +00:00
});
2021-07-21 04:20:39 +00:00
});