diff --git a/test/lib/next-test-utils.js b/test/lib/next-test-utils.js index 3cdda7813ab5512..6da31de40b668a5 100644 --- a/test/lib/next-test-utils.js +++ b/test/lib/next-test-utils.js @@ -867,3 +867,38 @@ 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) { + 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 the test path matches the glob pattern, add additional case to run the test with `--turbo` flag. + return isMatch +}