Skip to content

Commit

Permalink
Make property tests easier to read
Browse files Browse the repository at this point in the history
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 #388, https://github.com/dubzzz/fast-check/tree/main/packages/jest
  • Loading branch information
thewilkybarkid committed Oct 21, 2022
1 parent 5edd175 commit a6b0f11
Show file tree
Hide file tree
Showing 24 changed files with 6,852 additions and 7,068 deletions.
673 changes: 352 additions & 321 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@babel/preset-env": "^7.19.4",
"@babel/preset-typescript": "^7.18.6",
"@fast-check/jest": "^1.3.0",
"@ibm/plex": "^6.1.1",
"@playwright/test": "^1.27.1",
"@prettier/plugin-xml": "^2.2.0",
Expand Down Expand Up @@ -76,7 +77,6 @@
"eslint-import-resolver-typescript": "^3.5.1",
"eslint-plugin-fp-ts": "^0.3.2",
"eslint-plugin-import": "^2.26.0",
"fast-check": "^3.2.0",
"fetch-mock": "^9.11.0",
"fp-ts-contrib": "^0.1.29",
"glob": "^8.0.3",
Expand Down
22 changes: 20 additions & 2 deletions test/fc.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fc } from '@fast-check/jest'
import { Temporal } from '@js-temporal/polyfill'
import { mod11_2 } from 'cdigit'
import { Doi, isDoi } from 'doi-ts'
import { Request, Response } from 'express'
import * as fc from 'fast-check'
import * as F from 'fetch-fp-ts'
import { isNonEmpty } from 'fp-ts/Array'
import { NonEmptyArray } from 'fp-ts/NonEmptyArray'
Expand All @@ -26,7 +26,25 @@ import {
import { NonEmptyString, isNonEmptyString } from '../src/string'
import { User } from '../src/user'

export * from 'fast-check'
export { testProp as test } from '@fast-check/jest'

export const {
anything,
array,
boolean,
constant,
constantFrom,
integer,
jsonValue,
lorem,
oneof,
option,
record,
string,
tuple,
uuid,
webUrl,
} = fc

export const error = (): fc.Arbitrary<Error> => fc.string().map(error => new Error(error))

Expand Down
123 changes: 59 additions & 64 deletions test/home.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,70 @@ import * as fc from './fc'
import { runMiddleware } from './middleware'

describe('home', () => {
test('home', async () => {
await fc.assert(
fc.asyncProperty(
fc.connection({ method: fc.requestMethod().filter(method => method !== 'POST') }),
async connection => {
const actual = await runMiddleware(_.home({}), connection)()
fc.test(
'home',
[fc.connection({ method: fc.requestMethod().filter(method => method !== 'POST') })],
async connection => {
const actual = await runMiddleware(_.home({}), connection)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.OK },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
},
),
)
})
expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.OK },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
},
)

describe('looking up a DOI', () => {
test('with a preprint DOI', async () => {
await fc.assert(
fc.asyncProperty(
fc
.preprintDoi()
.chain(doi =>
fc.tuple(fc.constant(doi), fc.connection({ body: fc.constant({ doi }), method: fc.constant('POST') })),
),
async ([doi, connection]) => {
const actual = await runMiddleware(_.home({}), connection)()
fc.test(
'with a preprint DOI',
[
fc
.preprintDoi()
.chain(doi =>
fc.tuple(fc.constant(doi), fc.connection({ body: fc.constant({ doi }), method: fc.constant('POST') })),
),
],
async ([doi, connection]) => {
const actual = await runMiddleware(_.home({}), connection)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.SeeOther },
{
type: 'setHeader',
name: 'Location',
value: `/preprints/doi-${encodeURIComponent(
doi.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}`,
},
{ type: 'endResponse' },
]),
)
},
),
)
})
expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.SeeOther },
{
type: 'setHeader',
name: 'Location',
value: `/preprints/doi-${encodeURIComponent(
doi.toLowerCase().replaceAll('-', '+').replaceAll('/', '-'),
)}`,
},
{ type: 'endResponse' },
]),
)
},
)

test('with a non-preprint DOI', async () => {
await fc.assert(
fc.asyncProperty(
fc.connection({
body: fc.record({ doi: fc.oneof(fc.string(), fc.doi()) }, { withDeletedKeys: true }),
method: fc.constant('POST'),
}),
async connection => {
const actual = await runMiddleware(_.home({}), connection)()
fc.test(
'with a non-preprint DOI',
[
fc.connection({
body: fc.record({ doi: fc.oneof(fc.string(), fc.doi()) }, { withDeletedKeys: true }),
method: fc.constant('POST'),
}),
],
async connection => {
const actual = await runMiddleware(_.home({}), connection)()

expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.BadRequest },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
},
),
)
})
expect(actual).toStrictEqual(
E.right([
{ type: 'setStatus', status: Status.BadRequest },
{ type: 'setHeader', name: 'Content-Type', value: MediaType.textHTML },
{ type: 'setBody', body: expect.anything() },
]),
)
},
)
})
})
Loading

0 comments on commit a6b0f11

Please sign in to comment.