Skip to content

Commit 6294c9a

Browse files
committed
Change from Jest to Vitest
1 parent 56322ae commit 6294c9a

File tree

9 files changed

+9611
-13555
lines changed

9 files changed

+9611
-13555
lines changed

.eslintrc.cjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,21 @@ module.exports = {
77
'@will-stone/eslint-config-typescript',
88
'@will-stone/eslint-config-react',
99
'@will-stone/eslint-config-node',
10-
'@will-stone/eslint-config-jest',
1110
'@will-stone/eslint-config-prettier',
1211
],
1312
rules: {
1413
// Specific key order is used throughout this project, like the order of keyboard keys
1514
'sort-keys': 'off',
1615
},
16+
overrides: [
17+
{
18+
files: ['**/*.{test,spec}.{js,ts,jsx,tsx}'],
19+
extends: ['plugin:vitest/all'],
20+
rules: {
21+
'vitest/max-expects': 'off',
22+
'vitest/no-hooks': 'off',
23+
'vitest/require-top-level-describe': 'off',
24+
},
25+
},
26+
],
1727
}

jest.config.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

package-lock.json

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

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"postpack": "pinst --enable",
3131
"prepublishOnly": "npm run build",
3232
"start": "npm run build -- --watch --onSuccess \"node ./dist/cli.js\"",
33-
"test": "jest",
33+
"test": "vitest --watch=false",
3434
"typecheck": "tsc --noEmit"
3535
},
3636
"lint-staged": {
@@ -71,28 +71,27 @@
7171
"devDependencies": {
7272
"@changesets/cli": "^2.26.1",
7373
"@types/ink-gradient": "^2.0.1",
74-
"@types/jest": "^29.5.1",
7574
"@types/jsonfile": "^6.1.1",
7675
"@types/lodash": "^4.14.194",
7776
"@types/node": "^18.16.3",
7877
"@types/react": "^18.2.5",
7978
"@will-stone/eslint-config-base": "^7.0.3",
80-
"@will-stone/eslint-config-jest": "^3.0.0",
8179
"@will-stone/eslint-config-node": "^2.0.1",
8280
"@will-stone/eslint-config-prettier": "^2.0.3",
8381
"@will-stone/eslint-config-react": "^3.0.1",
8482
"@will-stone/eslint-config-typescript": "^6.0.0",
8583
"@will-stone/prettier-config": "^6.0.4",
8684
"eslint": "^8.39.0",
85+
"eslint-plugin-vitest": "^0.1.5",
8786
"husky": "^8.0.3",
8887
"ink-testing-library": "^3.0.0",
89-
"jest": "^29.5.0",
9088
"lint-staged": "^13.2.2",
9189
"pinst": "^3.0.0",
9290
"prettier": "^2.8.8",
93-
"ts-jest": "^29.1.0",
9491
"tsup": "^6.7.0",
95-
"typescript": "^5.0.4"
92+
"typescript": "^5.0.4",
93+
"vite": "^4.3.4",
94+
"vitest": "^0.31.0"
9695
},
9796
"engines": {
9897
"node": ">=18"

tests/calculate-potential-scores.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { expect, test } from 'vitest'
2+
13
import type { Dice } from '../source/calculate-potential-score.js'
24
import {
35
calculatePotentialScore,

tests/game-engine.test.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
import * as tings from 'tings'
1+
import type * as tings from 'tings'
2+
import type { SpyInstance } from 'vitest'
3+
import { afterEach, beforeAll, expect, test, vi } from 'vitest'
24

35
import { GameEngine, initialState } from '../source/game-engine.js'
6+
import type { Die } from '../source/model.js'
47
import * as utils from '../source/utils.js'
58

6-
let d6Spy: jest.SpyInstance<1 | 2 | 3 | 4 | 5 | 6 | null, [], unknown>
7-
8-
jest.useFakeTimers()
9-
jest.setSystemTime(0)
10-
11-
jest.mock('tings', () => ({ __esModule: true, ...jest.requireActual('tings') }))
9+
let d6Spy: SpyInstance<[], Die['value']>
1210

1311
// Make Tings' sleep function return immediately so tests run quicker
14-
jest.spyOn(tings, 'sleep').mockImplementation(
15-
() =>
16-
new Promise((resolve) => {
12+
vi.mock('tings', async () => ({
13+
...(await vi.importActual<typeof tings>('tings')),
14+
sleep: () =>
15+
new Promise<void>((resolve) => {
1716
resolve()
1817
}),
19-
)
20-
21-
// Overwrite biasedD6 as it will get stuck if d6 is mocked and cannot change
22-
jest.spyOn(utils, 'biasedD6').mockImplementation(() => 1)
23-
24-
beforeEach(() => {
25-
d6Spy = jest.spyOn(utils, 'd6')
18+
}))
19+
20+
beforeAll(() => {
21+
// Set all timestamps to 0
22+
vi.useFakeTimers()
23+
vi.setSystemTime(0)
24+
// Overwrite biasedD6 as it will get stuck if d6 is mocked and cannot change
25+
vi.spyOn(utils, 'biasedD6').mockImplementation(() => 1 as Die['value'])
26+
d6Spy = vi.spyOn(utils, 'd6')
2627
})
2728

2829
afterEach(() => {
@@ -31,29 +32,29 @@ afterEach(() => {
3132

3233
test('should report game as started after first roll, or a score in the scoreboard', async () => {
3334
const game1 = new GameEngine(initialState)
34-
expect(game1.isGameStart).toBe(true)
35+
expect(game1.isGameStart).toBeTruthy()
3536
await game1.roll()
36-
expect(game1.isGameStart).toBe(false)
37+
expect(game1.isGameStart).toBeFalsy()
3738

3839
const game2 = new GameEngine({
3940
...initialState,
4041
scores: { ...initialState.scores, gamble: 5 },
4142
})
4243

43-
expect(game2.isGameStart).toBe(false)
44+
expect(game2.isGameStart).toBeFalsy()
4445
})
4546

4647
test('should only allow three rolls', async () => {
4748
const game = new GameEngine(initialState)
48-
expect(game.canRoll).toBe(true)
49+
expect(game.canRoll).toBeTruthy()
4950
await game.roll()
50-
expect(game.canRoll).toBe(true)
51+
expect(game.canRoll).toBeTruthy()
5152
await game.roll()
52-
expect(game.canRoll).toBe(true)
53+
expect(game.canRoll).toBeTruthy()
5354
await game.roll()
54-
expect(game.canRoll).toBe(false)
55+
expect(game.canRoll).toBeFalsy()
5556
await game.roll()
56-
expect(game.canRoll).toBe(false)
57+
expect(game.canRoll).toBeFalsy()
5758
})
5859

5960
test('should advance turn on roll', async () => {
@@ -72,42 +73,42 @@ test('should advance turn on roll', async () => {
7273
test('should only show rolling during rolls', async () => {
7374
const game = new GameEngine(initialState)
7475
expect(game.turn).toBe(0)
75-
expect(game.isRolling).toBe(false)
76+
expect(game.isRolling).toBeFalsy()
7677

77-
expect(game.canRoll).toBe(true)
78+
expect(game.canRoll).toBeTruthy()
7879
const roll1 = game.roll()
7980
expect(game.turn).toBe(1)
80-
expect(game.isRolling).toBe(true)
81+
expect(game.isRolling).toBeTruthy()
8182
expect(game.potentialScoreboard).toStrictEqual({})
8283
await roll1
8384
expect(game.turn).toBe(1)
84-
expect(game.isRolling).toBe(false)
85+
expect(game.isRolling).toBeFalsy()
8586

86-
expect(game.canRoll).toBe(true)
87+
expect(game.canRoll).toBeTruthy()
8788
const roll2 = game.roll()
8889
expect(game.turn).toBe(2)
89-
expect(game.isRolling).toBe(true)
90+
expect(game.isRolling).toBeTruthy()
9091
await roll2
9192
expect(game.turn).toBe(2)
92-
expect(game.isRolling).toBe(false)
93+
expect(game.isRolling).toBeFalsy()
9394

94-
expect(game.canRoll).toBe(true)
95+
expect(game.canRoll).toBeTruthy()
9596
const roll3 = game.roll()
9697
expect(game.turn).toBe(3)
97-
expect(game.isRolling).toBe(true)
98+
expect(game.isRolling).toBeTruthy()
9899
await roll3
99100
const roll3Dice = game.dice
100101
expect(game.turn).toBe(3)
101-
expect(game.isRolling).toBe(false)
102+
expect(game.isRolling).toBeFalsy()
102103

103-
expect(game.canRoll).toBe(false)
104+
expect(game.canRoll).toBeFalsy()
104105
const roll4 = game.roll()
105106
expect(game.turn).toBe(3)
106-
expect(game.isRolling).toBe(false)
107+
expect(game.isRolling).toBeFalsy()
107108
await roll4
108109
const roll4Dice = game.dice
109110
expect(game.turn).toBe(3)
110-
expect(game.isRolling).toBe(false)
111+
expect(game.isRolling).toBeFalsy()
111112

112113
expect(roll3Dice).toStrictEqual(roll4Dice)
113114
})
@@ -351,7 +352,7 @@ test('should end game and restart', async () => {
351352
expect(game.lowerBoardSum).toBe(101)
352353
expect(game.total).toBe(131)
353354

354-
expect(game.isGameOver).toBe(false)
355+
expect(game.isGameOver).toBeFalsy()
355356
await game.roll()
356357
expect(game.potentialScoreboard).toStrictEqual({
357358
...initialState.scores,
@@ -373,7 +374,7 @@ test('should end game and restart', async () => {
373374

374375
test('should show potential jokers', async () => {
375376
const game = new GameEngine(initialState)
376-
expect(game.potentialHasJoker).toBe(false)
377+
expect(game.potentialHasJoker).toBeFalsy()
377378
expect(game.jokerCount).toBe(0)
378379
d6Spy
379380
.mockReturnValueOnce(3)
@@ -382,21 +383,21 @@ test('should show potential jokers', async () => {
382383
.mockReturnValueOnce(3)
383384
.mockReturnValueOnce(3)
384385
await game.roll()
385-
expect(game.potentialHasJoker).toBe(false)
386+
expect(game.potentialHasJoker).toBeFalsy()
386387
game.score('5Dice')
387388
expect(game.scores).toStrictEqual({
388389
...initialState.scores,
389390
'5Dice': 50,
390391
})
391-
expect(game.potentialHasJoker).toBe(false)
392+
expect(game.potentialHasJoker).toBeFalsy()
392393
d6Spy
393394
.mockReturnValueOnce(6)
394395
.mockReturnValueOnce(6)
395396
.mockReturnValueOnce(6)
396397
.mockReturnValueOnce(6)
397398
.mockReturnValueOnce(6)
398399
await game.roll()
399-
expect(game.potentialHasJoker).toBe(true)
400+
expect(game.potentialHasJoker).toBeTruthy()
400401
expect(game.jokerCount).toBe(0)
401402
game.score('5Dice')
402403
expect(game.jokerCount).toBe(1)

tests/utils.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { expect, test, vi } from 'vitest'
2+
13
import { biasedD6, d6, toKeys, toPairs } from '../source/utils.js'
24

35
test('should convert to pairs', () => {
@@ -12,7 +14,7 @@ test('should convert to keys', () => {
1214
})
1315

1416
test('should roll a d6', () => {
15-
const randomSpy = jest.spyOn(global.Math, 'random')
17+
const randomSpy = vi.spyOn(global.Math, 'random')
1618
randomSpy.mockReturnValueOnce(0.123)
1719
expect(d6()).toBe(1)
1820
randomSpy.mockReturnValueOnce(0.422)
@@ -23,7 +25,7 @@ test('should roll a d6', () => {
2325
})
2426

2527
test('should roll a biased d6', () => {
26-
const randomSpy = jest.spyOn(global.Math, 'random')
28+
const randomSpy = vi.spyOn(global.Math, 'random')
2729
randomSpy.mockReturnValueOnce(0.123)
2830
randomSpy.mockReturnValueOnce(0.152)
2931
randomSpy.mockReturnValueOnce(0.522)

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"strict": true,
2323
"stripInternal": true,
2424
"target": "ES2020",
25+
"types": ["vitest/importMeta"],
2526
"useDefineForClassFields": true
2627
},
2728
"include": ["source"]

vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
// ...
6+
},
7+
})

0 commit comments

Comments
 (0)