From 224bf131dd707c9b9b0fce973f505e7760574a0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 13 Mar 2024 13:15:55 +0100 Subject: [PATCH 1/2] refactor(test): expose test mode as re-exported variables --- test/lib/e2e-utils.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/lib/e2e-utils.ts b/test/lib/e2e-utils.ts index d0ee2b940bff1..10b185aec418a 100644 --- a/test/lib/e2e-utils.ts +++ b/test/lib/e2e-utils.ts @@ -90,6 +90,22 @@ if (testMode === 'dev') { ;(global as any).isNextStart = true } +/** + * Whether the test is running in dev mode. + * Based on `process.env.NEXT_TEST_MODE` and the test directory. + */ +export const isNextDev = testMode === 'dev' +/** + * Whether the test is running in deploy mode. + * Based on `process.env.NEXT_TEST_MODE`. + */ +export const isNextDeploy = testMode === 'deploy' +/** + * Whether the test is running in start mode. + * Default mode. `true` when both `isNextDev` and `isNextDeploy` are false. + */ +export const isNextStart = !isNextDev && !isNextDeploy + if (!testMode) { throw new Error( `No 'NEXT_TEST_MODE' set in environment, this is required for e2e-utils` From 3b50db36b314e4be1ead19542d95d3ae05cc1f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Orb=C3=A1n?= Date: Wed, 13 Mar 2024 13:19:11 +0100 Subject: [PATCH 2/2] refactor(test): don't use `global as any` in test utils --- test/lib/e2e-utils.ts | 16 ++++++++-------- test/lib/next-modes/base.ts | 15 ++++++--------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/test/lib/e2e-utils.ts b/test/lib/e2e-utils.ts index 10b185aec418a..209b2ba19fcb9 100644 --- a/test/lib/e2e-utils.ts +++ b/test/lib/e2e-utils.ts @@ -245,7 +245,7 @@ export function nextTestSetup( if (options.skipDeployment) { // When the environment is running for deployment tests. - if ((global as any).isNextDeploy) { + if (isNextDeploy) { // eslint-disable-next-line jest/no-focused-tests it.only('should skip next deploy', () => {}) // No tests are run. @@ -281,22 +281,22 @@ export function nextTestSetup( }) return { - get isNextDev(): boolean { - return Boolean((global as any).isNextDev) + get isNextDev() { + return isNextDev }, get isTurbopack(): boolean { return Boolean( - (global as any).isNextDev && + isNextDev && !process.env.TEST_WASM && (options.turbo ?? shouldRunTurboDevTest()) ) }, - get isNextDeploy(): boolean { - return Boolean((global as any).isNextDeploy) + get isNextDeploy() { + return isNextDeploy }, - get isNextStart(): boolean { - return Boolean((global as any).isNextStart) + get isNextStart() { + return isNextStart }, get next() { return nextProxy diff --git a/test/lib/next-modes/base.ts b/test/lib/next-modes/base.ts index 6e8cd0d020b55..ec50125d51d66 100644 --- a/test/lib/next-modes/base.ts +++ b/test/lib/next-modes/base.ts @@ -3,7 +3,7 @@ import path from 'path' import { existsSync, promises as fs } from 'fs' import treeKill from 'tree-kill' import type { NextConfig } from 'next' -import { FileRef } from '../e2e-utils' +import { FileRef, isNextDeploy, isNextDev } from '../e2e-utils' import { ChildProcess } from 'child_process' import { createNextInstall } from '../create-next-install' import { Span } from 'next/src/trace' @@ -75,7 +75,7 @@ export class NextInstance { require('console').log('packageJson??', this.packageJson) - if (!(global as any).isNextDeploy) { + if (!isNextDeploy) { this.env = { ...this.env, // remove node_modules/.bin repo path from env @@ -205,7 +205,7 @@ export class NextInstance { !this.dependencies && !this.installCommand && !this.packageJson && - !(global as any).isNextDeploy + !isNextDeploy ) { await fs.cp(process.env.NEXT_TEST_STARTER, this.testDir, { recursive: true, @@ -244,10 +244,7 @@ export class NextInstance { ) } - if ( - this.nextConfig || - ((global as any).isNextDeploy && !nextConfigFile) - ) { + if (this.nextConfig || (isNextDeploy && !nextConfigFile)) { const functions = [] const exportDeclare = this.packageJson?.type === 'module' @@ -281,7 +278,7 @@ export class NextInstance { ) } - if ((global as any).isNextDeploy) { + if (isNextDeploy) { const fileName = path.join( this.testDir, nextConfigFile || 'next.config.js' @@ -450,7 +447,7 @@ export class NextInstance { // TODO: replace this with an event directly from WatchPack inside // router-server for better accuracy if ( - (global as any).isNextDev && + isNextDev && (filename.startsWith('app/') || filename.startsWith('pages/')) ) { require('console').log('fs dev delay', filename)