Skip to content

Commit

Permalink
test(integration): allow to run --turbo dev server tests dynamically (
Browse files Browse the repository at this point in the history
#42967)

This PR is companion to #42656.

Previous PR #41908 allowed to set up integration / e2e tests spawn `next dev` with `--turbo` as needed. This PR leverages those, change all the tests suites to run with `--turbo`. One additional change is it can be configured dynamically via env variable. Setting env `__INTERNAL_NEXT_DEV_TEST_TURBO_GLOB_MATCH` to glob pattern will selectively enables tests to run with --turbo, normally it'll skip and only current devserver will be used.

These changes allow gradual integration between https://github.com/vercel/turbo to next.js. Plan is to run these tests with latest turbopack dev branch. Each time turbopack fixes / implements changes to enable certain set of tests, its CI will change its env variable without manually patching next.js's test cases. Once those change goes to upstream `next-swc` those tests can be permanantly enabled.

For those reasons, PR have somewhat verbose changes to touch individual test cases runs devserver. Changed file counts are lot, but mostly identical changes.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
kwonoj committed Nov 23, 2022
1 parent f3a728b commit 5d7e142
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 5d7e142

Please sign in to comment.