diff --git a/packages/next/build/swc/index.js b/packages/next/build/swc/index.js index 967db4bc9372..d1059c53acf8 100644 --- a/packages/next/build/swc/index.js +++ b/packages/next/build/swc/index.js @@ -410,6 +410,9 @@ function loadNative(isCustomTurbopack = false) { ...process.env, }, }) + child.on('message', (message) => { + console.log(message) + }) child.on('close', (code) => { if (code !== 0) { reject({ diff --git a/test/lib/e2e-utils.ts b/test/lib/e2e-utils.ts index df3c457da9ba..1c9ef4fbcc39 100644 --- a/test/lib/e2e-utils.ts +++ b/test/lib/e2e-utils.ts @@ -4,6 +4,7 @@ import { NextInstance, NextInstanceOpts } from './next-modes/base' import { NextDevInstance } from './next-modes/next-dev' import { NextStartInstance } from './next-modes/next-start' import { NextDeployInstance } from './next-modes/next-deploy' +import { shouldRunTurboDevTest } from './next-test-utils' // increase timeout to account for yarn install time jest.setTimeout(240 * 1000) @@ -117,6 +118,10 @@ export async function createNext( throw new Error(`createNext called without destroying previous instance`) } + if (!opts['turbo']) { + opts.turbo = shouldRunTurboDevTest() + } + if (testMode === 'dev') { // next dev nextInstance = new NextDevInstance(opts) diff --git a/test/lib/next-test-utils.js b/test/lib/next-test-utils.js index 3cdda7813ab5..e4956e2e5034 100644 --- a/test/lib/next-test-utils.js +++ b/test/lib/next-test-utils.js @@ -345,8 +345,16 @@ export function runNextCommandDev(argv, stdOut, opts = {}) { // Launch the app in dev mode. export function launchApp(dir, port, opts) { - const { turbo, ...options } = opts ?? {} - const useTurbo = !process.env.TEST_WASM && turbo + const options = opts ?? {} + let useTurbo = false + + if (!process.env.TEST_WASM) { + if (options['turbo']) { + useTurbo = options.turbo + } else { + useTurbo = shouldRunTurboDevTest() + } + } return runNextCommandDev( [useTurbo ? '--turbo' : undefined, dir, '-p', port].filter(Boolean), @@ -867,3 +875,51 @@ export function findAllTelemetryEvents(output, eventName) { ) return events.filter((e) => e.eventName === eventName).map((e) => e.payload) } + +/** + * Utility function to determine if a given test case needs to run with --turbo. + * + * This is primarily for the gradual test enablement with latest turbopack upstream changes. + * + * Note: it could be possible to dynamically create test cases itself (createDevTest(): it.each([...])), but + * it makes hard to conform with existing lint rules. Instead, starting off from manual fixture setup and + * update test cases accordingly as turbopack changes enable more test cases. + */ +export function shouldRunTurboDevTest() { + if (!!process.env.TEST_WASM) { + return false + } + + const shouldRunTurboDev = !!process.env.__INTERNAL_NEXT_DEV_TEST_TURBO_DEV + // short-circuit to run all the test with --turbo enabled skips glob matching costs + if (shouldRunTurboDev) { + console.log( + `Running tests with --turbo via custom environment variable __INTERNAL_NEXT_DEV_TEST_TURBO_DEV` + ) + return true + } + + const shouldRunTurboDevWithMatches = + !!process.env.__INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH + + // By default, we do not run any tests with `--turbo` flag. + if (!shouldRunTurboDevWithMatches) { + return false + } + + const glob = require('glob') + const matches = glob.sync( + process.env.__INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH + ) + const testPath = expect.getState().testPath + const isMatch = matches.some((match) => testPath.includes(match)) + + if (isMatch) { + console.log( + `Running tests with --turbo via custom environment variable __INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH` + ) + } + + // If the test path matches the glob pattern, add additional case to run the test with `--turbo` flag. + return isMatch +}