Skip to content

Commit fdbba1b

Browse files
committed
One spec by matcher for property based tests
1 parent 92e6f06 commit fdbba1b

6 files changed

+221
-187
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import fc from 'fast-check';
2+
3+
// settings for anything arbitrary
4+
export const anythingSettings = {
5+
key: fc.oneof(fc.string(), fc.constantFrom('k1', 'k2', 'k3')),
6+
withBoxedValues: true,
7+
// Issue #7975 have to be fixed before enabling the generation of Map
8+
withMap: false,
9+
// Issue #7975 have to be fixed before enabling the generation of Set
10+
withSet: false,
11+
};
12+
13+
// assertion settings
14+
export const assertSettings = {}; // eg.: {numRuns: 10000}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import fc from 'fast-check';
10+
import {
11+
anythingSettings,
12+
assertSettings,
13+
} from './__arbitraries__/sharedSettings';
14+
15+
describe('toContain', () => {
16+
it('should always find the value when inside the array', () => {
17+
fc.assert(
18+
fc.property(
19+
fc.array(fc.anything(anythingSettings)),
20+
fc.array(fc.anything(anythingSettings)),
21+
fc.anything(anythingSettings).filter(v => !Number.isNaN(v)),
22+
(startValues, endValues, v) => {
23+
// Given: startValues, endValues arrays and v value (not NaN)
24+
// Assert: We expect `expect([...startValues, v, ...endValues]).toContain(v)`
25+
expect([...startValues, v, ...endValues]).toContain(v);
26+
},
27+
),
28+
assertSettings,
29+
);
30+
});
31+
32+
it('should not find the value if it has been cloned into the array', () => {
33+
fc.assert(
34+
fc.property(
35+
fc.array(fc.anything(anythingSettings)),
36+
fc.array(fc.anything(anythingSettings)),
37+
fc.dedup(fc.anything(anythingSettings), 2),
38+
(startValues, endValues, [a, b]) => {
39+
// Given: startValues, endValues arrays
40+
// and [a, b] equal, but not the same values
41+
// with `typeof a === 'object && a !== null`
42+
// Assert: We expect `expect([...startValues, a, ...endValues]).not.toContain(b)`
43+
fc.pre(typeof a === 'object' && a !== null);
44+
expect([...startValues, a, ...endValues]).not.toContain(b);
45+
},
46+
),
47+
assertSettings,
48+
);
49+
});
50+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import fc from 'fast-check';
10+
import {
11+
anythingSettings,
12+
assertSettings,
13+
} from './__arbitraries__/sharedSettings';
14+
15+
describe('toContainEqual', () => {
16+
it('should always find the value when inside the array', () => {
17+
fc.assert(
18+
fc.property(
19+
fc.array(fc.anything(anythingSettings)),
20+
fc.array(fc.anything(anythingSettings)),
21+
fc.anything(anythingSettings),
22+
(startValues, endValues, v) => {
23+
// Given: startValues, endValues arrays and v any value
24+
// Assert: We expect `expect([...startValues, v, ...endValues]).toContainEqual(v)`
25+
expect([...startValues, v, ...endValues]).toContainEqual(v);
26+
},
27+
),
28+
assertSettings,
29+
);
30+
});
31+
32+
it('should always find the value when cloned inside the array', () => {
33+
fc.assert(
34+
fc.property(
35+
fc.array(fc.anything(anythingSettings)),
36+
fc.array(fc.anything(anythingSettings)),
37+
fc.dedup(fc.anything(anythingSettings), 2),
38+
(startValues, endValues, [a, b]) => {
39+
// Given: startValues, endValues arrays
40+
// and [a, b] identical values
41+
// Assert: We expect `expect([...startValues, a, ...endValues]).toContainEqual(b)`
42+
expect([...startValues, a, ...endValues]).toContainEqual(b);
43+
},
44+
),
45+
assertSettings,
46+
);
47+
});
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import fc from 'fast-check';
10+
import {
11+
anythingSettings,
12+
assertSettings,
13+
} from './__arbitraries__/sharedSettings';
14+
15+
describe('toEqual', () => {
16+
it('should be reflexive', () => {
17+
fc.assert(
18+
fc.property(fc.dedup(fc.anything(anythingSettings), 2), ([a, b]) => {
19+
// Given: a and b identical values
20+
// Assert: We expect `expect(a).toEqual(b)`
21+
expect(a).toEqual(b);
22+
}),
23+
assertSettings,
24+
);
25+
});
26+
27+
it('should be symmetric', () => {
28+
const safeExpectEqual = (a, b) => {
29+
try {
30+
expect(a).toEqual(b);
31+
return true;
32+
} catch (err) {
33+
return false;
34+
}
35+
};
36+
fc.assert(
37+
fc.property(
38+
fc.anything(anythingSettings),
39+
fc.anything(anythingSettings),
40+
(a, b) => {
41+
// Given: a and b values
42+
// Assert: We expect `expect(a).toEqual(b)`
43+
// to be equivalent to `expect(b).toEqual(a)`
44+
expect(safeExpectEqual(a, b)).toBe(safeExpectEqual(b, a));
45+
},
46+
),
47+
{
48+
...assertSettings,
49+
examples: [
50+
[0, 5e-324], // Issue #7941
51+
// [
52+
// new Set([false, true]),
53+
// new Set([new Boolean(true), new Boolean(true)]),
54+
// ], // Issue #7975
55+
],
56+
},
57+
);
58+
});
59+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import fc from 'fast-check';
10+
import {
11+
anythingSettings,
12+
assertSettings,
13+
} from './__arbitraries__/sharedSettings';
14+
15+
describe('toStrictEqual', () => {
16+
it('should be reflexive', () => {
17+
fc.assert(
18+
fc.property(fc.dedup(fc.anything(anythingSettings), 2), ([a, b]) => {
19+
// Given: a and b identical values
20+
// Assert: We expect `expect(a).toStrictEqual(b)`
21+
expect(a).toStrictEqual(b);
22+
}),
23+
assertSettings,
24+
);
25+
});
26+
27+
it('should be symmetric', () => {
28+
const safeExpectStrictEqual = (a, b) => {
29+
try {
30+
expect(a).toEqual(b);
31+
return true;
32+
} catch (err) {
33+
return false;
34+
}
35+
};
36+
fc.assert(
37+
fc.property(
38+
fc.anything(anythingSettings),
39+
fc.anything(anythingSettings),
40+
(a, b) => {
41+
// Given: a and b values
42+
// Assert: We expect `expect(a).toStrictEqual(b)`
43+
// to be equivalent to `expect(b).toStrictEqual(a)`
44+
expect(safeExpectStrictEqual(a, b)).toBe(safeExpectStrictEqual(b, a));
45+
},
46+
),
47+
assertSettings,
48+
);
49+
});
50+
});

0 commit comments

Comments
 (0)