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
35import { GameEngine , initialState } from '../source/game-engine.js'
6+ import type { Die } from '../source/model.js'
47import * 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
2829afterEach ( ( ) => {
@@ -31,29 +32,29 @@ afterEach(() => {
3132
3233test ( '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
4647test ( '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
5960test ( 'should advance turn on roll' , async ( ) => {
@@ -72,42 +73,42 @@ test('should advance turn on roll', async () => {
7273test ( '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
374375test ( '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 )
0 commit comments