Skip to content

Commit

Permalink
fix: make tests framework and node version agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-alford committed Sep 28, 2024
1 parent c64a8b2 commit 17a7a1d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
node-version:
- 16.x
- 18.x
- 20.x
- 22.x
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 1 addition & 1 deletion test-utils/test-suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export const getTestSuite = <I, O>(schema: Schema<I, O>): TestSuite<I, O> => {
expect(actual).toStrictEqual(expected)
})
test(`parallel`, async () => {
expect(await actualPar()).toStrictEqual(expected)
expect(await actualPar()).toEqual(expected)
})
})
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ParseBase64Json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ runStandardTestSuite(Schema, _ => ({
TC.transcodeErrors(
TC.serializationError(
'Base64',
new SyntaxError('Unexpected token j in JSON at position 0'),
// eslint-disable-next-line no-useless-escape
`{\"age\":42,\"birthday\":null,\"isAlive\":\"true\",\"name\":\"John\"}`,
expect.any(SyntaxError),
expect.stringContaining(
`{"age":42,"birthday":null,"isAlive":"true","name":"John"}`,
),
),
),
),
Expand Down
49 changes: 28 additions & 21 deletions tests/transcode-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,36 @@ const result = transcoder.decode({
baz: '{"age":42}',
})

const expectedError = `Encountered 7 transcode errors:
┌ at key quux:
└── Unexpected value: "baz"
┌ at key bar:
├── Expected Array[2,5]<[Integer<0,>, UUID version 5]> but got "Array(1)"
├── at index 0:
├─── at index 0:
├──── Expected Integer<0,> but got -1
├─── at index 1:
└──── Expected UUID version 5 but got "not-a-uuid"
┌ at key baz:
└── Expected Base64, but ran into serialization error: SyntaxError: Unexpected token j in JSON at position 0; got "{"age":42}"
┌ at key foo:
├── at union member \`boolean\`:
├─── Expected boolean but got undefined
├── at union member \`string<1,>\`:
└─── Expected string<1,> but got undefined`
const expectedErrorLines = [
'Encountered 7 transcode errors:',
'┌ at key quux:',
'└── Unexpected value: "baz"',
'┌ at key bar:',
'├── Expected Array[2,5]<[Integer<0,>, UUID version 5]> but got "Array(1)"',
'├── at index 0:',
'├─── at index 0:',
'├──── Expected Integer<0,> but got -1',
'├─── at index 1:',
'└──── Expected UUID version 5 but got "not-a-uuid"',
'┌ at key baz:',
expect.stringContaining(
'└── Expected Base64, but ran into serialization error: SyntaxError:',
),
'┌ at key foo:',
'├── at union member `boolean`:',
'├─── Expected boolean but got undefined',
'├── at union member `string<1,>`:',
'└─── Expected string<1,> but got undefined',
]

describe('transcode errors', () => {
if (E.isRight(result)) {
throw new Error('Expected a left')
}
test('drawTree', () => {
const mockError = result.left
expect(drawTree(mockError)).toBe(expectedError)

expect(drawTree(mockError).split('\n')).toStrictEqual(expectedErrorLines)
})
test('struct > array error', () => {
const result = transcoder.decode([])
Expand Down Expand Up @@ -159,13 +164,15 @@ describe('transcode errors', () => {
})
describe('introspection', () => {
test('JSON.stringify', () => {
expect(JSON.stringify(result.left)).toBe(JSON.stringify(expectedError))
expect(JSON.parse(JSON.stringify(result.left)).split('\n')).toStrictEqual(
expectedErrorLines,
)
})
test('toString', () => {
expect(result.left.toString()).toBe(expectedError)
expect(result.left.toString().split('\n')).toStrictEqual(expectedErrorLines)
})
test('inspect', () => {
expect(util.inspect(result.left)).toBe(expectedError)
expect(util.inspect(result.left).split('\n')).toStrictEqual(expectedErrorLines)
})
})
})
4 changes: 2 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"target": "ESNext",
"tsBuildInfoFile": "./build/tsconfig.build.tsbuildinfo"
},
"include": ["./src"],
"exclude": ["src/__tests__/*.ts"]
"include": ["./src/**/*.ts"],
"exclude": ["./tests/**/*.ts"]
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
"schemata-ts/*": ["./src/*"]
}
},
"include": ["./**/*.ts"],
"exclude": ["build", "dist", "node_modules"]
}

0 comments on commit 17a7a1d

Please sign in to comment.