|
| 1 | +/* eslint-disable no-empty-pattern */ |
| 2 | +/* eslint-disable prefer-rest-params */ |
| 3 | +import { describe, expect, expectTypeOf, test, vi } from 'vitest' |
| 4 | + |
| 5 | +const todoList: number[] = [1, 2, 3] |
| 6 | +const doneList: number[] = [] |
| 7 | +const archiveList: number[] = [] |
| 8 | + |
| 9 | +const todoFn = vi.fn().mockImplementation(async (use) => { |
| 10 | + await use(todoList) |
| 11 | + // cleanup |
| 12 | + todoFn.mockClear() |
| 13 | + todoList.length = 0 |
| 14 | + todoList.push(1, 2, 3) |
| 15 | +}) |
| 16 | + |
| 17 | +const doneFn = vi.fn().mockImplementation(async (use) => { |
| 18 | + await use(doneList) |
| 19 | + // cleanup |
| 20 | + doneFn.mockClear() |
| 21 | + doneList.length = 0 |
| 22 | +}) |
| 23 | + |
| 24 | +const myTest = test |
| 25 | + .extend<{ todoList: number[] }>({ |
| 26 | + todoList: todoFn, |
| 27 | + }) |
| 28 | + .extend<{ doneList: number[]; archiveList: number[] }>({ |
| 29 | + doneList: doneFn, |
| 30 | + archiveList, |
| 31 | + }) |
| 32 | + |
| 33 | +describe('test.extend()', () => { |
| 34 | + myTest('todoList and doneList', ({ todoList, doneList, archiveList }) => { |
| 35 | + expect(todoFn).toBeCalledTimes(1) |
| 36 | + expect(doneFn).toBeCalledTimes(1) |
| 37 | + |
| 38 | + expectTypeOf(todoList).toEqualTypeOf<number[]>() |
| 39 | + expectTypeOf(doneList).toEqualTypeOf<number[]>() |
| 40 | + expectTypeOf(doneList).toEqualTypeOf<number[]>() |
| 41 | + |
| 42 | + expect(todoList).toEqual([1, 2, 3]) |
| 43 | + expect(doneList).toEqual([]) |
| 44 | + expect(archiveList).toEqual([]) |
| 45 | + |
| 46 | + doneList.push(todoList.shift()!) |
| 47 | + expect(todoList).toEqual([2, 3]) |
| 48 | + expect(doneList).toEqual([1]) |
| 49 | + |
| 50 | + doneList.push(todoList.shift()!) |
| 51 | + expect(todoList).toEqual([3]) |
| 52 | + expect(doneList).toEqual([1, 2]) |
| 53 | + |
| 54 | + archiveList.push(todoList.shift()!) |
| 55 | + expect(todoList).toEqual([]) |
| 56 | + expect(archiveList).toEqual([3]) |
| 57 | + }) |
| 58 | + |
| 59 | + myTest('should called cleanup functions', ({ todoList, doneList, archiveList }) => { |
| 60 | + expect(todoList).toEqual([1, 2, 3]) |
| 61 | + expect(doneList).toEqual([]) |
| 62 | + expect(archiveList).toEqual([3]) |
| 63 | + }) |
| 64 | + |
| 65 | + describe('smartly init fixtures', () => { |
| 66 | + myTest('should not init any fixtures', function () { |
| 67 | + expect(todoFn).not.toBeCalled() |
| 68 | + expect(doneFn).not.toBeCalled() |
| 69 | + |
| 70 | + expectTypeOf(arguments[0].todoList).not.toEqualTypeOf<number[]>() |
| 71 | + expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>() |
| 72 | + expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>() |
| 73 | + |
| 74 | + expect(arguments[0].todoList).toBeUndefined() |
| 75 | + expect(arguments[0].doneList).toBeUndefined() |
| 76 | + expect(arguments[0].archiveList).toBeUndefined() |
| 77 | + }) |
| 78 | + |
| 79 | + myTest('should not init any fixtures', function ({}) { |
| 80 | + expect(todoFn).not.toBeCalled() |
| 81 | + expect(doneFn).not.toBeCalled() |
| 82 | + |
| 83 | + expectTypeOf(arguments[0].todoList).not.toEqualTypeOf<number[]>() |
| 84 | + expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>() |
| 85 | + expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>() |
| 86 | + |
| 87 | + expect(arguments[0].todoList).toBeUndefined() |
| 88 | + expect(arguments[0].doneList).toBeUndefined() |
| 89 | + expect(arguments[0].archiveList).toBeUndefined() |
| 90 | + }) |
| 91 | + |
| 92 | + myTest('should only init todoList', function ({ todoList }) { |
| 93 | + expect(todoFn).toBeCalledTimes(1) |
| 94 | + expect(doneFn).not.toBeCalled() |
| 95 | + |
| 96 | + expectTypeOf(todoList).toEqualTypeOf<number[]>() |
| 97 | + expectTypeOf(arguments[0].doneList).not.toEqualTypeOf<number[]>() |
| 98 | + expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>() |
| 99 | + |
| 100 | + expect(arguments[0].doneList).toBeUndefined() |
| 101 | + expect(arguments[0].archiveList).toBeUndefined() |
| 102 | + }) |
| 103 | + |
| 104 | + myTest('should only init todoList and doneList', function ({ todoList, doneList }) { |
| 105 | + expect(todoFn).toBeCalledTimes(1) |
| 106 | + expect(doneFn).toBeCalledTimes(1) |
| 107 | + |
| 108 | + expectTypeOf(todoList).toEqualTypeOf<number[]>() |
| 109 | + expectTypeOf(doneList).toEqualTypeOf<number[]>() |
| 110 | + expectTypeOf(arguments[0].archiveList).not.toEqualTypeOf<number[]>() |
| 111 | + |
| 112 | + expect(todoList).toEqual([1, 2, 3]) |
| 113 | + expect(doneList).toEqual([]) |
| 114 | + expect(arguments[0].archiveList).toBeUndefined() |
| 115 | + }) |
| 116 | + |
| 117 | + myTest('should init all fixtures', ({ todoList, ...rest }) => { |
| 118 | + expect(todoFn).toBeCalledTimes(1) |
| 119 | + expect(doneFn).toBeCalledTimes(1) |
| 120 | + |
| 121 | + expectTypeOf(todoList).toEqualTypeOf<number[]>() |
| 122 | + expectTypeOf(rest.doneList).toEqualTypeOf<number[]>() |
| 123 | + expectTypeOf(rest.archiveList).toEqualTypeOf<number[]>() |
| 124 | + |
| 125 | + expect(todoList).toEqual([1, 2, 3]) |
| 126 | + expect(rest.doneList).toEqual([]) |
| 127 | + expect(rest.archiveList).toEqual([3]) |
| 128 | + }) |
| 129 | + |
| 130 | + myTest('should init all fixtures', (context) => { |
| 131 | + expect(todoFn).toBeCalledTimes(1) |
| 132 | + expect(doneFn).toBeCalledTimes(1) |
| 133 | + |
| 134 | + expectTypeOf(context.todoList).toEqualTypeOf<number[]>() |
| 135 | + expectTypeOf(context.doneList).toEqualTypeOf<number[]>() |
| 136 | + expectTypeOf(context.archiveList).toEqualTypeOf<number[]>() |
| 137 | + |
| 138 | + expect(context.todoList).toEqual([1, 2, 3]) |
| 139 | + expect(context.doneList).toEqual([]) |
| 140 | + expect(context.archiveList).toEqual([3]) |
| 141 | + }) |
| 142 | + }) |
| 143 | +}) |
0 commit comments