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 431934a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 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
5 changes: 5 additions & 0 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,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)
Expand Down
56 changes: 54 additions & 2 deletions test/lib/next-test-utils.js
Expand Up @@ -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),
Expand Down Expand Up @@ -867,3 +875,47 @@ 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() {
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 431934a

Please sign in to comment.