Skip to content

Commit

Permalink
fix: ESM tests on Windows. (#14804)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Aug 16, 2022
1 parent c45da3d commit 343b269
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
23 changes: 15 additions & 8 deletions jest.config.js
Expand Up @@ -12,10 +12,22 @@ module.exports = {
runner: supportsESMAndJestLightRunner ? "jest-light-runner" : "jest-runner",

collectCoverageFrom: [
"packages/*/src/**/*.{js,mjs,ts}",
"codemods/*/src/**/*.{js,mjs,ts}",
"eslint/*/src/**/*.{js,mjs,ts}",
"packages/*/src/**/*.{js,cjs,mjs,ts}",
"codemods/*/src/**/*.{js,cjs,mjs,ts}",
"eslint/*/src/**/*.{js,cjs,mjs,ts}",
],
// coveragePathIgnorePatterns Doesn't work on windows
coveragePathIgnorePatterns: [
"/node_modules/",
"<rootDir>/packages/babel-standalone/",
"/test/(fixtures|tmp|__data__)/",
".*\\.d\\.ts",
"<rootDir>/packages/babel-standalone/.*",
"<rootDir>/packages/babel-types/src/.*/generated/.*",
"<rootDir>/packages/babel-helpers/src/helpers/.*",
"<rootDir>/packages/babel-core/src/vendor/.*",
],

// The eslint/* packages use ESLint v6, which has dropped support for Node v6.
// TODO: Remove this process.version check in Babel 8.
testRegex: `./(packages|codemods${
Expand Down Expand Up @@ -48,11 +60,6 @@ module.exports = {
"/test/(fixtures|tmp|__data__)/",
"<rootDir>/(packages|codemods|eslint)/[^/]+/lib/",
],
coveragePathIgnorePatterns: [
"/node_modules/",
"<rootDir>/packages/babel-standalone/babel(\\.min)?\\.js",
"/test/(fixtures|tmp|__data__)/",
],
modulePathIgnorePatterns: [
"/test/fixtures/",
"/test/tmp/",
Expand Down
11 changes: 6 additions & 5 deletions packages/babel-core/test/config-chain.js
Expand Up @@ -1064,7 +1064,7 @@ describe("buildConfigChain", function () {
"babel.config.mjs",
])("should load %s asynchronously", async name => {
const esm = isMJS(name);
if (skipUnsupportedESM(esm, `should load ${name} asynchronously`)) {
if (esm && skipUnsupportedESM(`should load ${name} asynchronously`)) {
return;
}

Expand Down Expand Up @@ -1096,8 +1096,8 @@ describe("buildConfigChain", function () {
)("should throw if both %s and %s are used", async (name1, name2) => {
const esm = isMJS(name1) || isMJS(name2);
if (
esm &&
skipUnsupportedESM(
esm,
`should throw if both ${name1} and ${name2} are used`,
)
) {
Expand Down Expand Up @@ -1163,7 +1163,7 @@ describe("buildConfigChain", function () {
].filter(Boolean),
)("should load %s asynchronously", async name => {
const esm = isMJS(name);
if (skipUnsupportedESM(esm, `should load ${name} asynchronously`)) {
if (esm && skipUnsupportedESM(`should load ${name} asynchronously`)) {
return;
}

Expand Down Expand Up @@ -1205,8 +1205,8 @@ describe("buildConfigChain", function () {
)("should throw if both %s and %s are used", async (name1, name2) => {
const esm = isMJS(name1) || isMJS(name2);
if (
esm &&
skipUnsupportedESM(
esm,
`should throw if both ${name1} and ${name2} are used`,
)
) {
Expand Down Expand Up @@ -1249,7 +1249,8 @@ describe("buildConfigChain", function () {
async ({ config, dir, error }) => {
const esm = isMJS(config);
if (
skipUnsupportedESM(esm, `should show helpful errors for ${config}`)
esm &&
skipUnsupportedESM(`should show helpful errors for ${config}`)
) {
return;
}
Expand Down
20 changes: 5 additions & 15 deletions packages/babel-core/test/helpers/esm.js
Expand Up @@ -10,29 +10,19 @@ const require = createRequire(import.meta.url);
const dirname = path.dirname(fileURLToPath(import.meta.url));

// "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10
const nodeSupportsESM = parseInt(process.versions.node) >= 12;
const isWindows = process.platform === "win32";

export const supportsESM = nodeSupportsESM && !isWindows;
export const supportsESM = parseInt(process.versions.node) >= 12;

export const isMJS = file => path.extname(file) === ".mjs";

export const itESM = supportsESM ? it : it.skip;

export function skipUnsupportedESM(esm, name) {
if (esm && !nodeSupportsESM) {
export function skipUnsupportedESM(name) {
if (!supportsESM) {
console.warn(
`Skipping "${name}" because native ECMAScript modules are not supported.`,
);
return true;
}
// This can be removed when loadOptionsAsyncInSpawedProcess is removed.
if (esm && isWindows) {
console.warn(
`Skipping "${name}" because the ESM runner cannot be spawned on Windows.`,
);
return true;
}
return false;
}

Expand Down Expand Up @@ -60,9 +50,9 @@ export function spawnTransformSync() {
// Jest supports dynamic import(), but Node.js segfaults when using it in our tests.
async function spawn(runner, filename, cwd = process.cwd()) {
const { stdout, stderr } = await util.promisify(cp.execFile)(
require.resolve(`../fixtures/babel-${runner}.mjs`),
process.execPath,
// pass `cwd` as params as `process.cwd()` will normalize `cwd` on macOS
[filename, cwd],
[require.resolve(`../fixtures/babel-${runner}.mjs`), filename, cwd],
{ cwd, env: process.env },
);

Expand Down

0 comments on commit 343b269

Please sign in to comment.