Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve imports when needed in Jest integration #752

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions integration-test/integration-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ describe("integration tests", () => {
const testConfig = await readJSONFileContentsIfExists("./test.json");
if (testConfig?.expectedError) {
try {
await execPromise(`npx jest`);
await execPromise(`NODE_OPTIONS=--experimental-vm-modules npx jest --no-cache`);
assert.fail("Expected Jest to fail");
} catch (e) {
assert((e as {stderr: string}).stderr.includes(testConfig.expectedError));
}
} else {
// Should not crash.
await execPromise(`npx jest`);
await execPromise(`NODE_OPTIONS=--experimental-vm-modules npx jest --no-cache`);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
transform: {"\\.ts$": "@sucrase/jest-plugin"},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file isn't transpiled, so it remains ESM at runtime.
export const one = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test("addition", async () => {
// Assert that module is defined to confirm that this is a CJS file.
expect(module).toBeTruthy();

// This dynamic import should be preserved, not transformed to require.
const {one} = await import("./main");
expect(one + one).toBe(2 as number);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
transform: {"\\.ts$": "@sucrase/jest-plugin"},
extensionsToTreatAsEsm: ['.ts'],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file isn't transpiled, so it remains ESM at runtime.
export const one = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This static import should be preserved rather than transformed to require.
import {one} from "./main";

test("addition", () => {
expect(one + one).toBe(2 as number);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
20 changes: 12 additions & 8 deletions integrations/jest-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type {TransformOptions} from "@jest/transform";
import {extname} from "path";
import {Transform, transform} from "sucrase";

import type {Options} from "../../../src/Options";

function getTransforms(filename: string): Array<Transform> | null {
if (filename.endsWith(".js") || filename.endsWith(".jsx")) {
return ["flow", "jsx", "imports", "jest"];
} else if (filename.endsWith(".ts")) {
return ["typescript", "imports", "jest"];
} else if (filename.endsWith(".tsx")) {
return ["typescript", "jsx", "imports", "jest"];
function getTransforms(filename: string, supportsStaticESM: boolean): Array<Transform> | null {
const extension = extname(filename);
const maybeImports: Array<Transform> = supportsStaticESM ? [] : ["imports"];
if ([".js", ".jsx", ".mjs", ".cjs"].includes(extension)) {
return [...maybeImports, "flow", "jsx", "jest"];
} else if (extension === ".ts") {
return [...maybeImports, "typescript", "jest"];
} else if ([".tsx", ".mts", ".cts"].includes(extension)) {
return [...maybeImports, "typescript", "jsx", "jest"];
}
return null;
}
Expand All @@ -23,10 +26,11 @@ export function process(
filename: string,
options: TransformOptions<Partial<Options>>,
): {code: string; map?: RawSourceMap | string | null} {
const transforms = getTransforms(filename);
const transforms = getTransforms(filename, options.supportsStaticESM);
if (transforms !== null) {
const {code, sourceMap} = transform(src, {
transforms,
preserveDynamicImport: options.supportsDynamicImport,
...options.transformerConfig,
sourceMapOptions: {compiledFilename: filename},
filePath: filename,
Expand Down