Skip to content

Commit

Permalink
add regression test for #346
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace authored and pi0 committed Dec 17, 2024
1 parent 7dbe81d commit e63ec80
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ Enter Identifier
Enter Literal"
`;
exports[`fixtures > require-esm > require-esm-stderr 1`] = `
"(node:11558) ExperimentalWarning: CommonJS module <cwd>/index.cjs is loading ES Module <cwd>/_dist/esm.js using require().
Support for loading ES Module in require() is an experimental feature and might change at any time
(Use \`node --trace-warnings ...\` to show where the warning was created)
broken! { mod: { fn: {}, file: {} } }"
`;
exports[`fixtures > require-esm > stdout 1`] = `""`;
exports[`fixtures > syntax > stdout 1`] = `
"Optional chaining: undefined
Nullish coalescing: 0
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ describe("fixtures", async () => {

if (name.includes("error")) {
expect(cleanUpSnap(stderr)).toMatchSnapshot("stderr");
} else if (name === "require-esm") {
expect(cleanUpSnap(stderr)).toMatchSnapshot("require-esm-stderr");
} else {
// expect no error
expect(stderr).toBe("");
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/require-esm/_dist/esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fn from "./fn.js";
import file from "./file";
export default { fn, file };
1 change: 1 addition & 0 deletions test/fixtures/require-esm/_dist/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
1 change: 1 addition & 0 deletions test/fixtures/require-esm/_dist/fn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = function () {};
18 changes: 18 additions & 0 deletions test/fixtures/require-esm/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
async function run() {
let mod;
try {
mod = require("./_dist/esm.js");
} catch {
const { createJiti } = await import("../../../lib/jiti.mjs");
const jiti = createJiti(__filename, { interopDefault: true });
mod = await jiti.import("./_dist/esm.js");
}
mod = mod.default;
if (typeof mod.fn === "function") {
console.log("Works!");
return;
}
console.error("broken!", { mod });
}

run();
3 changes: 3 additions & 0 deletions test/fixtures/require-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "-"
}

0 comments on commit e63ec80

Please sign in to comment.