Skip to content

Commit

Permalink
test(test-utils): utility for --turbo testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 23, 2022
1 parent e079fbb commit beea6e8
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/next/build/swc/index.js
Expand Up @@ -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({
Expand Down
20 changes: 17 additions & 3 deletions test/lib/e2e-utils.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -117,15 +118,28 @@ export async function createNext(
throw new Error(`createNext called without destroying previous instance`)
}

const useTurbo = !!process.env.TEST_WASM
? false
: opts?.turbo ?? shouldRunTurboDevTest()

if (testMode === 'dev') {
// next dev
nextInstance = new NextDevInstance(opts)
nextInstance = new NextDevInstance({
...opts,
turbo: useTurbo,
})
} else if (testMode === 'deploy') {
// Vercel
nextInstance = new NextDeployInstance(opts)
nextInstance = new NextDeployInstance({
...opts,
turbo: false,
})
} else {
// next build + next start
nextInstance = new NextStartInstance(opts)
nextInstance = new NextStartInstance({
...opts,
turbo: false,
})
}

nextInstance.on('destroy', () => {
Expand Down
54 changes: 52 additions & 2 deletions test/lib/next-test-utils.js
Expand Up @@ -345,8 +345,10 @@ 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 ?? {}
const useTurbo = !!process.env.TEST_WASM
? false
: options?.turbo ?? shouldRunTurboDevTest()

return runNextCommandDev(
[useTurbo ? '--turbo' : undefined, dir, '-p', port].filter(Boolean),
Expand Down Expand Up @@ -867,3 +869,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
}

0 comments on commit beea6e8

Please sign in to comment.