Unverified Commit 514d57ae authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

add test case for esm commonjs interop (#48940)

### What?

add a test case

### Why?

Some packages expect the default export of a commonjs module to be the whole exports object when importing from ESM.

turbo PR: https://github.com/vercel/turbo/pull/4738
parent 861bc0d7
import { esm, esmPlain, loadInvalidCjs, loadInvalidEsm } from 'esm-package'
import { auto, autoPlain } from 'auto-package'
it('should have the commonjs module as default export in specified ESM', () => {
expect(esm).toMatchObject({
__esModule: true,
named: 'named',
})
expect(esmPlain).toMatchObject({
named: 'named',
})
})
it('should not have the commonjs module as default export in specified ESM', () => {
expect(auto).toMatchObject({
__esModule: true,
named: 'named',
})
expect(autoPlain).toMatchObject({
named: 'named',
})
})
it('should error for invalid esm exports', async () => {
let value = await loadInvalidEsm()
expect(value).toMatchObject({
__esModule: true,
})
})
it('should error for invalid cjs exports', async () => {
let value = await loadInvalidCjs()
expect(value).toMatchObject({
__esModule: true,
named: 'named',
})
})
Object.defineProperty(exports, "__esModule", { value: true });
exports.named = "named";
\ No newline at end of file
import m from "./commonjs.js";
import m2 from "./plain.js";
export const auto = m;
export const autoPlain = m2;
\ No newline at end of file
Object.defineProperty(exports, "__esModule", { value: true });
exports.named = "named";
\ No newline at end of file
import m from "./commonjs.cjs";
import m2 from "./plain.cjs";
export const esm = m;
export const esmPlain = m2;
export function loadInvalidEsm() {
return import("./invalid-exports.js");
}
export function loadInvalidCjs() {
return import("./invalid-exports.cjs");
}
\ No newline at end of file
PlainIssue {
severity: Error,
context: "[project]/packages/next-swc/crates/next-dev-tests/tests/integration/turbopack/basic/node-default-import/input/node_modules/esm-package/invalid-exports.cjs",
category: "module type",
title: "Specified module format (CommonJs) is not matching the module format of the source code (EcmaScript Modules)",
description: "The CommonJs module format was specified in the package.json that is affecting this source file or by using an special extension, but Ecmascript import/export syntax is used in the source code.\nThe module was automatically converted to an EcmaScript module, but that is in conflict with the specified module format. Either change the \"type\" field in the package.json or replace EcmaScript import/export syntax with CommonJs syntas in the source file.\nIn some cases EcmaScript import/export syntax is added by an transform and isn't actually part of the source code. In these cases revisit transformation options to inject the correct syntax.",
detail: "",
documentation_link: "",
source: None,
sub_issues: [],
processing_path: Some(
[],
),
}
\ No newline at end of file
PlainIssue {
severity: Warning,
context: "[project]/packages/next-swc/crates/next-dev-tests/tests/integration/turbopack/basic/node-default-import/input/node_modules/esm-package/invalid-exports.js",
category: "module type",
title: "Specified module format (EcmaScript Modules) is not matching the module format of the source code (CommonJs)",
description: "The EcmaScript module format was specified in the package.json that is affecting this source file or by using an special extension, but it looks like that CommonJs syntax is used in the source code.\nExports made by CommonJs syntax will lead to a runtime error, since the module is in EcmaScript mode. Either change the \"type\" field in the package.json or replace CommonJs syntax with EcmaScript import/export syntax in the source file.",
detail: "",
documentation_link: "",
source: None,
sub_issues: [],
processing_path: Some(
[],
),
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment