Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 15dfda6

Browse files
committed
add more unit tests covering more interesting cjs/esm interoperability cases
1 parent 905fd2d commit 15dfda6

8 files changed

Lines changed: 139 additions & 2 deletions

File tree

e2e/__tests__/__snapshots__/nativeEsmCjsRequire.test.ts.snap

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`runs fixture where a CJS module loaded via import synchronously requires an ESM dependency 1`] = `
3+
exports[`runs ESM/CJS interop fixture (__esModule unwrapping, named exports, singleton caching) 1`] = `
44
"Test Suites: 1 passed, 1 total
5-
Tests: 1 passed, 1 total
5+
Tests: 6 passed, 6 total
6+
Snapshots: 0 total
7+
Time: <<REPLACED>>
8+
Ran all test suites matching cjs-esm-interop."
9+
`;
10+
11+
exports[`runs fixture where a CJS module loaded via import synchronously requires an ESM dependency 1`] = `
12+
"Test Suites: 2 passed, 2 total
13+
Tests: 7 passed, 7 total
614
Snapshots: 0 total
715
Time: <<REPLACED>>
816
Ran all test suites."

e2e/__tests__/nativeEsmCjsRequire.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,17 @@ test('runs fixture where a CJS module loaded via import synchronously requires a
2424
expect(summary).toMatchSnapshot();
2525
expect(exitCode).toBe(0);
2626
});
27+
28+
// Covers __esModule interop, plain-CJS default, and CJS-as-ESM caching.
29+
test('runs ESM/CJS interop fixture (__esModule unwrapping, named exports, singleton caching)', () => {
30+
const {exitCode, stderr} = runJest(
31+
DIR,
32+
['--testPathPatterns', 'cjs-esm-interop'],
33+
{nodeOptions: '--experimental-vm-modules --no-warnings'},
34+
);
35+
36+
const {summary} = extractSummary(stderr);
37+
38+
expect(summary).toMatchSnapshot();
39+
expect(exitCode).toBe(0);
40+
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import greet, {helper} from '../babel-style-default.cjs';
9+
import plainDefault, {value, multiply} from '../plain-cjs.cjs';
10+
import {increment as incA, getCount as countA} from '../importer-a.mjs';
11+
import {increment as incB, getCount as countB} from '../importer-b.mjs';
12+
13+
// ── __esModule interop ────────────────────────────────────────────────────────
14+
15+
test('default import of __esModule CJS unwraps .default, not the whole exports', () => {
16+
// greet should be the function, not {__esModule: true, default: fn, helper: fn}
17+
expect(typeof greet).toBe('function');
18+
expect(greet('World')).toBe('Hello, World!');
19+
});
20+
21+
test('named imports of __esModule CJS work alongside default', () => {
22+
expect(helper(7)).toBe(14);
23+
});
24+
25+
test('__esModule key is not exposed as a named export', async () => {
26+
const ns = await import('../babel-style-default.cjs');
27+
expect(Object.keys(ns)).not.toContain('__esModule');
28+
});
29+
30+
// ── plain CJS (no __esModule flag) ───────────────────────────────────────────
31+
32+
test('default import of plain CJS is the whole module.exports object', () => {
33+
expect(plainDefault).toEqual({value: 99, multiply: expect.any(Function)});
34+
});
35+
36+
test('named imports from plain CJS work', () => {
37+
expect(value).toBe(99);
38+
expect(multiply(6, 7)).toBe(42);
39+
});
40+
41+
// ── CJS-as-ESM caching (singleton) ───────────────────────────────────────────
42+
43+
test('two ESM importers of the same CJS module share one singleton instance', () => {
44+
// importer-a and importer-b both re-export singleton-state.cjs.
45+
// If the CJS module is cached, mutations are visible across importers.
46+
incA();
47+
incA();
48+
incB(); // operates on the same underlying CJS export object
49+
expect(countA()).toBe(3);
50+
expect(countB()).toBe(3);
51+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Simulates a CJS file emitted by Babel/Webpack from an ESM source.
9+
// The __esModule flag signals that `.default` is the real default export.
10+
'use strict';
11+
Object.defineProperty(exports, '__esModule', {value: true});
12+
exports.default = function greet(name) {
13+
return `Hello, ${name}!`;
14+
};
15+
exports.helper = function helper(x) {
16+
return x * 2;
17+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export {increment, getCount} from './singleton-state.cjs';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
export {increment, getCount} from './singleton-state.cjs';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Plain CJS without __esModule — default export should be the whole module.exports.
9+
'use strict';
10+
module.exports = {
11+
value: 99,
12+
multiply(a, b) {
13+
return a * b;
14+
},
15+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
// Stateful CJS module — used to verify that two ESM importers share the same instance.
9+
'use strict';
10+
let callCount = 0;
11+
exports.increment = function increment() {
12+
callCount += 1;
13+
};
14+
exports.getCount = function getCount() {
15+
return callCount;
16+
};

0 commit comments

Comments
 (0)