Skip to content

Commit 2ea9bea

Browse files
test: make property tests easier to read
This change uses the dedicated fast-check library for Jest, which reduces the amount of code required. The main benefit is that there's not so much indentation, so it's easier to read. Refs https://github.com/dubzzz/fast-check/tree/main/packages/jest
1 parent ff86907 commit 2ea9bea

3 files changed

Lines changed: 119 additions & 118 deletions

File tree

package-lock.json

Lines changed: 76 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"devDependencies": {
1919
"@commitlint/cli": "^16.2.3",
2020
"@commitlint/config-conventional": "^16.2.1",
21+
"@fast-check/jest": "^1.6.0",
2122
"@trivago/prettier-plugin-sort-imports": "^3.2.0",
2223
"@tsconfig/recommended": "^1.0.1",
2324
"@types/doi-regex": "^0.1.0",
@@ -31,7 +32,6 @@
3132
"eslint-import-resolver-typescript": "^2.7.1",
3233
"eslint-plugin-import": "^2.26.0",
3334
"expect-type": "^0.13.0",
34-
"fast-check": "^2.24.0",
3535
"fp-ts": "^2.11.9",
3636
"husky": "^7.0.4",
3737
"jest": "^29.4.3",

test/index.test.ts

Lines changed: 42 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,85 @@
1-
import { describe, expect, test } from '@jest/globals'
1+
import { test } from '@fast-check/jest'
2+
import { describe, expect } from '@jest/globals'
23
import * as O from 'fp-ts/Option'
34
import * as _ from '../src'
45
import * as fc from './fc'
56

67
describe('doi-ts', () => {
78
describe('destructors', () => {
8-
test('toUrl', () => {
9-
fc.assert(
10-
fc.property(fc.doi(), doi => {
11-
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`))
12-
}),
13-
)
9+
test.prop([fc.doi()])('toUrl', doi => {
10+
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`))
1411
})
1512
})
1613

1714
describe('instances', () => {
1815
describe('Eq', () => {
19-
test('with the same DOI', () => {
20-
fc.assert(
21-
fc.property(fc.doi(), doi => {
22-
expect(_.Eq.equals(doi, doi)).toBe(true)
23-
}),
24-
)
16+
test.prop([fc.doi()])('with the same DOI', doi => {
17+
expect(_.Eq.equals(doi, doi)).toBe(true)
2518
})
2619

27-
test('with the same DOI in different cases', () => {
28-
fc.assert(
29-
fc.property(fc.doi(), doi => {
30-
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true)
31-
}),
32-
)
20+
test.prop([fc.doi()])('with the same DOI in different cases', doi => {
21+
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true)
3322
})
3423

35-
test('with different DOIs', () => {
36-
fc.assert(
37-
fc.property(fc.doi(), fc.doi(), (doi1, doi2) => {
38-
expect(_.Eq.equals(doi1, doi2)).toBe(false)
39-
}),
40-
)
24+
test.prop([fc.doi(), fc.doi()])('with different DOIs', (doi1, doi2) => {
25+
expect(_.Eq.equals(doi1, doi2)).toBe(false)
4126
})
4227
})
4328
})
4429

4530
describe('refinements', () => {
4631
describe('isDoi', () => {
47-
test('with a DOI', () => {
48-
fc.assert(
49-
fc.property(fc.doi(), doi => {
50-
expect(_.isDoi(doi)).toBe(true)
51-
}),
52-
)
32+
test.prop([fc.doi()])('with a DOI', doi => {
33+
expect(_.isDoi(doi)).toBe(true)
5334
})
5435

55-
test('with a non-DOI', () => {
56-
fc.assert(
57-
fc.property(fc.anything(), value => {
58-
expect(_.isDoi(value)).toBe(false)
59-
}),
60-
)
36+
test.prop([fc.anything()])('with a non-DOI', value => {
37+
expect(_.isDoi(value)).toBe(false)
6138
})
6239
})
6340

6441
describe('hasRegistrant', () => {
65-
test('when the registrant matches', () => {
66-
fc.assert(
67-
fc.property(
68-
fc
69-
.array(fc.registrant(), { minLength: 1 })
70-
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))),
71-
([registrants, doi]) => {
72-
expect(_.hasRegistrant(...registrants)(doi)).toBe(true)
73-
},
74-
),
75-
)
42+
test.prop([
43+
fc
44+
.array(fc.registrant(), { minLength: 1 })
45+
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))),
46+
])('when the registrant matches', ([registrants, doi]) => {
47+
expect(_.hasRegistrant(...registrants)(doi)).toBe(true)
7648
})
7749

78-
test('when the registrant does not match', () => {
79-
fc.assert(
80-
fc.property(fc.array(fc.registrant()), fc.doi(), (registrants, doi) => {
81-
expect(_.hasRegistrant(...registrants)(doi)).toBe(false)
82-
}),
83-
)
50+
test.prop([fc.array(fc.registrant()), fc.doi()])('when the registrant does not match', (registrants, doi) => {
51+
expect(_.hasRegistrant(...registrants)(doi)).toBe(false)
8452
})
8553
})
8654
})
8755

8856
describe('utils', () => {
8957
describe('parse', () => {
90-
test('when it contains a DOI', () => {
91-
fc.assert(
92-
fc.property(
93-
fc
94-
.tuple(
95-
fc.doi(),
96-
fc.stringOf(fc.constant(' ')),
97-
fc.constantFrom(
98-
'doi:',
99-
'https://doi.org/',
100-
'http://doi.org/',
101-
'https://dx.doi.org/',
102-
'http://dx.doi.org/',
103-
),
104-
fc.stringOf(fc.constant(' ')),
105-
)
106-
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [
107-
doi,
108-
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`,
109-
]),
110-
([doi, input]) => {
111-
expect(_.parse(input)).toStrictEqual(O.some(doi))
112-
},
113-
),
114-
)
58+
test.prop([
59+
fc
60+
.tuple(
61+
fc.doi(),
62+
fc.stringOf(fc.constant(' ')),
63+
fc.constantFrom('doi:', 'https://doi.org/', 'http://doi.org/', 'https://dx.doi.org/', 'http://dx.doi.org/'),
64+
fc.stringOf(fc.constant(' ')),
65+
)
66+
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [
67+
doi,
68+
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`,
69+
]),
70+
])('when it contains a DOI', ([doi, input]) => {
71+
expect(_.parse(input)).toStrictEqual(O.some(doi))
11572
})
11673

117-
test('when it does not contain a DOI', () => {
118-
fc.assert(
119-
fc.property(fc.unicodeString(), input => {
120-
expect(_.parse(input)).toStrictEqual(O.none)
121-
}),
122-
)
74+
test.prop([fc.unicodeString()])('when it does not contain a DOI', input => {
75+
expect(_.parse(input)).toStrictEqual(O.none)
12376
})
12477
})
12578

126-
test('getRegistrant', () => {
127-
fc.assert(
128-
fc.property(
129-
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))),
130-
([registrant, doi]) => {
131-
expect(_.getRegistrant(doi)).toBe(registrant)
132-
},
133-
),
134-
)
79+
test.prop([
80+
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))),
81+
])('getRegistrant', ([registrant, doi]) => {
82+
expect(_.getRegistrant(doi)).toBe(registrant)
13583
})
13684
})
13785
})

0 commit comments

Comments
 (0)