|
1 | 1 | /* eslint-disable prefer-rest-params */
|
2 | 2 | /* eslint-disable no-empty-pattern */
|
3 |
| -import { describe, expect, expectTypeOf, test, vi } from 'vitest' |
| 3 | +import { afterAll, afterEach, beforeEach, describe, expect, expectTypeOf, test, vi } from 'vitest' |
4 | 4 |
|
5 | 5 | interface Fixtures {
|
6 | 6 | todoList: number[]
|
@@ -38,39 +38,34 @@ const myTest = test
|
38 | 38 | })
|
39 | 39 |
|
40 | 40 | describe('test.extend()', () => {
|
41 |
| - myTest('todoList and doneList', ({ todoList, doneList, archiveList }) => { |
42 |
| - expect(todoFn).toBeCalledTimes(1) |
43 |
| - expect(doneFn).toBeCalledTimes(1) |
44 |
| - |
45 |
| - expectTypeOf(todoList).toEqualTypeOf<number[]>() |
46 |
| - expectTypeOf(doneList).toEqualTypeOf<number[]>() |
47 |
| - expectTypeOf(doneList).toEqualTypeOf<number[]>() |
| 41 | + describe('basic', () => { |
| 42 | + myTest('todoList and doneList', ({ todoList, doneList, archiveList }) => { |
| 43 | + expect(todoFn).toBeCalledTimes(1) |
| 44 | + expect(doneFn).toBeCalledTimes(1) |
48 | 45 |
|
49 |
| - expect(todoList).toEqual([1, 2, 3]) |
50 |
| - expect(doneList).toEqual([]) |
51 |
| - expect(archiveList).toEqual([]) |
| 46 | + expectTypeOf(todoList).toEqualTypeOf<number[]>() |
| 47 | + expectTypeOf(doneList).toEqualTypeOf<number[]>() |
| 48 | + expectTypeOf(doneList).toEqualTypeOf<number[]>() |
52 | 49 |
|
53 |
| - doneList.push(todoList.shift()!) |
54 |
| - expect(todoList).toEqual([2, 3]) |
55 |
| - expect(doneList).toEqual([1]) |
| 50 | + expect(todoList).toEqual([1, 2, 3]) |
| 51 | + expect(doneList).toEqual([]) |
| 52 | + expect(archiveList).toEqual([]) |
56 | 53 |
|
57 |
| - doneList.push(todoList.shift()!) |
58 |
| - expect(todoList).toEqual([3]) |
59 |
| - expect(doneList).toEqual([1, 2]) |
| 54 | + doneList.push(todoList.shift()!) |
| 55 | + expect(todoList).toEqual([2, 3]) |
| 56 | + expect(doneList).toEqual([1]) |
60 | 57 |
|
61 |
| - archiveList.push(todoList.shift()!) |
62 |
| - expect(todoList).toEqual([]) |
63 |
| - expect(archiveList).toEqual([3]) |
| 58 | + doneList.push(todoList.shift()!) |
| 59 | + expect(todoList).toEqual([3]) |
| 60 | + expect(doneList).toEqual([1, 2]) |
64 | 61 |
|
65 |
| - archiveList.pop() |
66 |
| - }) |
| 62 | + archiveList.push(todoList.shift()!) |
| 63 | + expect(todoList).toEqual([]) |
| 64 | + expect(archiveList).toEqual([3]) |
67 | 65 |
|
68 |
| - myTest('should called cleanup functions', ({ todoList, doneList, archiveList }) => { |
69 |
| - expect(todoList).toEqual([1, 2, 3]) |
70 |
| - expect(doneList).toEqual([]) |
71 |
| - expect(archiveList).toEqual([]) |
| 66 | + archiveList.pop() |
| 67 | + }) |
72 | 68 | })
|
73 |
| - |
74 | 69 | describe('smartly init fixtures', () => {
|
75 | 70 | myTest('should not init any fixtures', function () {
|
76 | 71 | expect(todoFn).not.toBeCalled()
|
@@ -150,4 +145,136 @@ describe('test.extend()', () => {
|
150 | 145 | expect(archive).toEqual([])
|
151 | 146 | })
|
152 | 147 | })
|
| 148 | + |
| 149 | + describe('fixture call times', () => { |
| 150 | + const apiFn = vi.fn(() => true) |
| 151 | + const serviceFn = vi.fn(() => true) |
| 152 | + const teardownFn = vi.fn() |
| 153 | + |
| 154 | + interface APIFixture { |
| 155 | + api: boolean |
| 156 | + service: boolean |
| 157 | + } |
| 158 | + |
| 159 | + const testAPI = test.extend<APIFixture>({ |
| 160 | + api: async ({}, use) => { |
| 161 | + await use(apiFn()) |
| 162 | + apiFn.mockClear() |
| 163 | + teardownFn() |
| 164 | + }, |
| 165 | + service: async ({}, use) => { |
| 166 | + await use(serviceFn()) |
| 167 | + serviceFn.mockClear() |
| 168 | + teardownFn() |
| 169 | + }, |
| 170 | + }) |
| 171 | + |
| 172 | + beforeEach<APIFixture>(({ api, service }) => { |
| 173 | + expect(api).toBe(true) |
| 174 | + expect(service).toBe(true) |
| 175 | + }) |
| 176 | + |
| 177 | + testAPI('Should init1 time', ({ api }) => { |
| 178 | + expect(api).toBe(true) |
| 179 | + expect(apiFn).toBeCalledTimes(1) |
| 180 | + }) |
| 181 | + |
| 182 | + testAPI('Should init 1 time has multiple fixture', ({ api, service }) => { |
| 183 | + expect(api).toBe(true) |
| 184 | + expect(service).toBe(true) |
| 185 | + expect(serviceFn).toBeCalledTimes(1) |
| 186 | + expect(apiFn).toBeCalledTimes(1) |
| 187 | + }) |
| 188 | + |
| 189 | + afterEach<APIFixture>(({ api, service }) => { |
| 190 | + expect(api).toBe(true) |
| 191 | + expect(service).toBe(true) |
| 192 | + expect(apiFn).toBeCalledTimes(1) |
| 193 | + expect(serviceFn).toBeCalledTimes(1) |
| 194 | + }) |
| 195 | + |
| 196 | + afterAll(() => { |
| 197 | + expect(serviceFn).toBeCalledTimes(0) |
| 198 | + expect(apiFn).toBeCalledTimes(0) |
| 199 | + expect(teardownFn).toBeCalledTimes(2) |
| 200 | + }) |
| 201 | + }) |
| 202 | + |
| 203 | + describe('fixture in nested describe', () => { |
| 204 | + interface Fixture { |
| 205 | + foo: number |
| 206 | + bar: number |
| 207 | + } |
| 208 | + |
| 209 | + const fooFn = vi.fn(() => 0) |
| 210 | + const fooCleanup = vi.fn() |
| 211 | + |
| 212 | + const barFn = vi.fn(() => 0) |
| 213 | + const barCleanup = vi.fn() |
| 214 | + |
| 215 | + const nestedTest = test.extend<Fixture>({ |
| 216 | + async foo({}, use) { |
| 217 | + await use(fooFn()) |
| 218 | + fooCleanup() |
| 219 | + }, |
| 220 | + async bar({}, use) { |
| 221 | + await use(barFn()) |
| 222 | + barCleanup() |
| 223 | + }, |
| 224 | + }) |
| 225 | + |
| 226 | + beforeEach<Fixture>(({ foo }) => { |
| 227 | + expect(foo).toBe(0) |
| 228 | + }) |
| 229 | + |
| 230 | + nestedTest('should only initialize foo', ({ foo }) => { |
| 231 | + expect(foo).toBe(0) |
| 232 | + expect(fooFn).toBeCalledTimes(1) |
| 233 | + expect(barFn).toBeCalledTimes(0) |
| 234 | + }) |
| 235 | + |
| 236 | + describe('level 2, using both foo and bar together', () => { |
| 237 | + beforeEach<Fixture>(({ foo, bar }) => { |
| 238 | + expect(foo).toBe(0) |
| 239 | + expect(bar).toBe(0) |
| 240 | + }) |
| 241 | + |
| 242 | + nestedTest('should only initialize bar', ({ foo, bar }) => { |
| 243 | + expect(foo).toBe(0) |
| 244 | + expect(bar).toBe(0) |
| 245 | + expect(fooFn).toBeCalledTimes(1) |
| 246 | + expect(barFn).toBeCalledTimes(1) |
| 247 | + }) |
| 248 | + |
| 249 | + afterEach<Fixture>(({ foo, bar }) => { |
| 250 | + expect(foo).toBe(0) |
| 251 | + expect(bar).toBe(0) |
| 252 | + }) |
| 253 | + |
| 254 | + afterAll(() => { |
| 255 | + // foo setup in outside describe |
| 256 | + // cleanup also called in outside describe |
| 257 | + expect(fooCleanup).toHaveBeenCalledTimes(0) |
| 258 | + // bar setup in inside describe |
| 259 | + // cleanup also called in inside describe |
| 260 | + expect(barCleanup).toHaveBeenCalledTimes(1) |
| 261 | + }) |
| 262 | + }) |
| 263 | + |
| 264 | + nestedTest('level 2 will not call foo cleanup', ({ foo }) => { |
| 265 | + expect(foo).toBe(0) |
| 266 | + expect(fooFn).toBeCalledTimes(1) |
| 267 | + }) |
| 268 | + |
| 269 | + afterEach<Fixture>(({ foo }) => { |
| 270 | + expect(foo).toBe(0) |
| 271 | + }) |
| 272 | + |
| 273 | + afterAll(() => { |
| 274 | + // foo setup in this describe |
| 275 | + // cleanup also called in this describe |
| 276 | + expect(fooCleanup).toHaveBeenCalledTimes(1) |
| 277 | + expect(barCleanup).toHaveBeenCalledTimes(1) |
| 278 | + }) |
| 279 | + }) |
153 | 280 | })
|
0 commit comments