Skip to content

Commit 8e3349b

Browse files
9aoyCopilot
andauthored
fix: suppress ESModulesLinkingError for exports that might be implemented in mock (#704)
Co-authored-by: Copilot <[email protected]>
1 parent e144d74 commit 8e3349b

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { describe, expect, it, rs } from '@rstest/core';
2+
// @ts-expect-error
3+
import * as serviceAPI from './services';
4+
5+
rs.mock('./services', () => ({
6+
value: 42,
7+
}));
8+
9+
describe('Mock Module EsModulesLinkingError', () => {
10+
it('should return the mocked value', () => {
11+
expect(serviceAPI.value).toBe(42);
12+
});
13+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 42;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { describe, expect, it } from '@rstest/core';
4+
import { runRstestCli } from '../../scripts';
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
describe('test EsModulesLinkingError', () => {
10+
it('should not print EsModulesLinkingError', async () => {
11+
const { cli, expectExecSuccess } = await runRstestCli({
12+
command: 'rstest',
13+
args: ['run'],
14+
options: {
15+
nodeOptions: {
16+
cwd: join(__dirname, '../fixtures/esModulesLinkingError'),
17+
},
18+
},
19+
});
20+
21+
await expectExecSuccess();
22+
23+
const logs = cli.stdout.split('\n').filter(Boolean);
24+
25+
expect(
26+
logs.find((log) => log.match(/ESModulesLinkingError: export 'value'/)),
27+
).toBeUndefined();
28+
});
29+
});

packages/core/src/core/plugins/basic.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
9191
// Keep require.resolve expressions.
9292
requireResolve: false,
9393
...(config.module.parser.javascript || {}),
94+
// suppress ESModulesLinkingError for exports that might be implemented in mock
95+
exportsPresence: 'warn',
9496
};
9597

9698
config.resolve ??= {};

packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] =
3535
"module": {
3636
"parser": {
3737
"javascript": {
38-
"exportsPresence": "error",
38+
"exportsPresence": "warn",
3939
"importDynamic": false,
4040
"inlineConst": false,
4141
"requireAsExpression": false,
@@ -555,7 +555,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = `
555555
"module": {
556556
"parser": {
557557
"javascript": {
558-
"exportsPresence": "error",
558+
"exportsPresence": "warn",
559559
"importDynamic": false,
560560
"inlineConst": false,
561561
"requireAsExpression": false,
@@ -1066,7 +1066,7 @@ exports[`prepareRsbuild > should generate rspack config correctly in watch mode
10661066
"module": {
10671067
"parser": {
10681068
"javascript": {
1069-
"exportsPresence": "error",
1069+
"exportsPresence": "warn",
10701070
"importDynamic": false,
10711071
"inlineConst": false,
10721072
"requireAsExpression": false,
@@ -1584,7 +1584,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
15841584
"module": {
15851585
"parser": {
15861586
"javascript": {
1587-
"exportsPresence": "error",
1587+
"exportsPresence": "warn",
15881588
"importDynamic": false,
15891589
"inlineConst": false,
15901590
"requireAsExpression": false,
@@ -2104,7 +2104,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects
21042104
"module": {
21052105
"parser": {
21062106
"javascript": {
2107-
"exportsPresence": "error",
2107+
"exportsPresence": "warn",
21082108
"importDynamic": false,
21092109
"inlineConst": false,
21102110
"requireAsExpression": false,

0 commit comments

Comments
 (0)