Files
next.js/test/jest-setup-after-env.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

125 lines
4.1 KiB
TypeScript

import * as matchers from 'jest-extended'
expect.extend(matchers)
// Patch jscodeshift testUtils to normalize line endings (fixes Windows CRLF issues)
// The issue: jscodeshift's printer (recast) outputs CRLF on Windows, but test fixtures use LF
// We need to patch both defineTest (which uses internal closure references) and runInlineTest
if (process.platform === 'win32') {
try {
const testUtils = require('jscodeshift/dist/testUtils')
const fs = require('fs')
const path = require('path')
// Helper to normalize line endings
// - Convert CRLF to LF
// - Remove trailing whitespace from each line (not meaningful for code)
// - Ensure exactly one trailing newline (POSIX convention for text files)
// Using \n*$ to handle case where transform output has no trailing newline
const normalizeLF = (str: string) =>
str
.replace(/\r\n/g, '\n')
.replace(/[ \t]+$/gm, '')
.replace(/\n*$/, '\n')
// Patch runInlineTest to normalize both transform output and expected
testUtils.runInlineTest = function (
module: any,
options: any,
input: any,
expectedOutput: string,
testOptions?: any
) {
// Normalize input source
const normalizedInput =
typeof input === 'object' && input.source
? { ...input, source: normalizeLF(input.source) }
: input
// Normalize expected output
const normalizedExpected = normalizeLF(expectedOutput)
// Run the transform and normalize its output for comparison
const output = testUtils.applyTransform(
module,
options,
normalizedInput,
testOptions
)
const normalizedOutput =
typeof output === 'string' ? normalizeLF(output) : output
// Do the comparison ourselves instead of letting the original do it
// eslint-disable-next-line jest/no-standalone-expect -- called from within test blocks
expect(normalizedOutput).toEqual(normalizedExpected)
return normalizedOutput
}
// Replace defineTest entirely since it uses internal closure references
// that bypass our exports patch
testUtils.defineTest = function (
dirName: string,
transformName: string,
options: any,
testFilePrefix?: string,
testOptions?: { parser?: string }
) {
const testName = testFilePrefix
? `transforms correctly using "${testFilePrefix}" data`
: 'transforms correctly'
describe(transformName, () => {
it(testName, () => {
const fixtureDir = path.join(dirName, '..', '__testfixtures__')
const prefix = testFilePrefix || transformName
const module = require(path.join(dirName, '..', transformName))
// Determine file extension based on parser option
const parser = testOptions?.parser || module.parser
const extension =
parser === 'ts' ? 'ts' : parser === 'tsx' ? 'tsx' : 'js'
const inputPath = path.join(
fixtureDir,
`${prefix}.input.${extension}`
)
const outputPath = path.join(
fixtureDir,
`${prefix}.output.${extension}`
)
const source = normalizeLF(fs.readFileSync(inputPath, 'utf8'))
const expectedOutput = normalizeLF(
fs.readFileSync(outputPath, 'utf8')
)
testUtils.runInlineTest(
module,
options,
{ path: inputPath, source },
expectedOutput,
testOptions
)
})
})
}
} catch {
// jscodeshift not available, skip patching
}
}
// A default max-timeout of 90 seconds is allowed
// per test we should aim to bring this down though
jest.setTimeout((process.platform === 'win32' ? 180 : 60) * 1000)
// Polyfill for `using` https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-2.html
if (!Symbol.dispose) {
Object.defineProperty(Symbol, 'dispose', {
value: Symbol('Symbol.dispose'),
})
}
if (!Symbol.asyncDispose) {
Object.defineProperty(Symbol, 'asyncDispose', {
value: Symbol('Symbol.asyncDispose'),
})
}