| 300 | } |
| 301 | |
| 302 | export function nextTestSetup( |
| 303 | options: Parameters<typeof createNext>[0] & { |
| 304 | skipDeployment?: boolean |
| 305 | dir?: string |
| 306 | } |
| 307 | ): { |
| 308 | isNextDev: boolean |
| 309 | isNextDeploy: boolean |
| 310 | isNextStart: boolean |
| 311 | isTurbopack: boolean |
| 312 | isRspack: boolean |
| 313 | next: NextInstance |
| 314 | skipped: boolean |
| 315 | } { |
| 316 | let skipped = false |
| 317 | |
| 318 | if (options.skipDeployment) { |
| 319 | // When the environment is running for deployment tests. |
| 320 | if (isNextDeploy) { |
| 321 | // eslint-disable-next-line jest/no-focused-tests |
| 322 | it.only('should skip next deploy', () => {}) |
| 323 | // No tests are run. |
| 324 | skipped = true |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | let next: NextInstance | undefined |
| 329 | if (!skipped) { |
| 330 | beforeAll(async () => { |
| 331 | next = await createNext(options) |
| 332 | }) |
| 333 | afterAll(async () => { |
| 334 | // Gracefully destroy the instance if `createNext` success. |
| 335 | // If next instance is not available, it's likely beforeAll hook failed and unnecessarily throws another error |
| 336 | // by attempting to destroy on undefined. |
| 337 | await next?.destroy() |
| 338 | }) |
| 339 | } |
| 340 | |
| 341 | const nextProxy = new Proxy<NextInstance>({} as NextInstance, { |
| 342 | get: function (_target, property) { |
| 343 | if (!next) { |
| 344 | throw new Error( |
| 345 | 'next instance is not initialized yet, make sure you call methods on next instance in test body.' |
| 346 | ) |
| 347 | } |
| 348 | const prop = next[property] |
| 349 | return typeof prop === 'function' ? prop.bind(next) : prop |
| 350 | }, |
| 351 | set: function (_target, key, value) { |
| 352 | if (!next) { |
| 353 | throw new Error( |
| 354 | 'next instance is not initialized yet, make sure you call methods on next instance in test body.' |
| 355 | ) |
| 356 | } |
| 357 | next[key] = value |
| 358 | return true |
| 359 | }, |