Files
pnpm/__utils__/jest-config/jest.transform.js
Zoltan Kochan f79f0540ac chore: update Node.js to 26.0.0 (#11472)
* chore: update Node.js to 26.0.0

* fix(jest-config): use amaro for type stripping on Node.js 26

Node.js v26 removed the `transform` mode and `sourceMap` option from
`module.stripTypeScriptTypes`. Switch the Jest transform to call
`amaro.transformSync` directly (the same wasm transformer Node.js wraps)
so we keep inline source maps for tests.
2026-05-05 22:18:12 +02:00

53 lines
2.0 KiB
JavaScript

import { Buffer } from 'node:buffer'
import { fileURLToPath } from 'node:url'
import { transformSync } from '@babel/core'
import { transformSync as stripTypes } from 'amaro'
// This file was created referencing:
// https://github.com/jestjs/jest/issues/15443
export default {
process(sourceText, sourcePath) {
// Node.js v26 removed the `transform` mode and `sourceMap` option from
// `module.stripTypeScriptTypes`, so we call into `amaro` (the same
// wasm-backed transformer Node.js uses internally) directly. `transform`
// mode is required to keep inline source maps — without them, interactive
// debuggers can't map type-stripped test files back to their on-disk
// location. For background, see https://github.com/pnpm/pnpm/pull/11024.
const { code: stripped, map } = stripTypes(sourceText, {
mode: 'transform',
sourceMap: true,
filename: sourcePath,
})
const code = map
? `${stripped}\n//# sourceMappingURL=data:application/json;base64,${Buffer.from(map).toString('base64')}\n`
: stripped
// Using the presence of the DisposableStack global to feature detect
// whether the current Node.js runtime supports explicit resource
// management. If it exists, we don't need to do any more work.
if (typeof DisposableStack !== 'undefined') {
return { code }
}
// Node.js 24.0.0 introduces explicit resource management:
// https://nodejs.org/en/blog/release/v24.0.0#v8-136
//
// When running tests on Node.js before version 24, we'll need to transpile
// the "using" keyword. Otherwise Jest will fail to recognize this syntax
// and show confusing errors when running some tests.
//
// This can be deleted when pnpm no longer needs to support Node.js v22.
return babel(code, sourcePath)
}
}
function babel (code, sourceFileName) {
return transformSync(code, {
babelrc: false,
configFile: false,
sourceFileName,
plugins: [fileURLToPath(import.meta.resolve('@babel/plugin-transform-explicit-resource-management'))]
})
}