diff --git a/e2e/mock/fixtures/esModulesLinkingError/index.test.ts b/e2e/mock/fixtures/esModulesLinkingError/index.test.ts new file mode 100644 index 000000000..38ed548d7 --- /dev/null +++ b/e2e/mock/fixtures/esModulesLinkingError/index.test.ts @@ -0,0 +1,13 @@ +import { describe, expect, it, rs } from '@rstest/core'; +// @ts-expect-error +import * as serviceAPI from './services'; + +rs.mock('./services', () => ({ + value: 42, +})); + +describe('Mock Module EsModulesLinkingError', () => { + it('should return the mocked value', () => { + expect(serviceAPI.value).toBe(42); + }); +}); diff --git a/e2e/mock/fixtures/esModulesLinkingError/services/index.js b/e2e/mock/fixtures/esModulesLinkingError/services/index.js new file mode 100644 index 000000000..71becd3a3 --- /dev/null +++ b/e2e/mock/fixtures/esModulesLinkingError/services/index.js @@ -0,0 +1 @@ +export const a = 42; diff --git a/e2e/mock/tests/esModulesLinkingError.test.ts b/e2e/mock/tests/esModulesLinkingError.test.ts new file mode 100644 index 000000000..bd8ac7ebe --- /dev/null +++ b/e2e/mock/tests/esModulesLinkingError.test.ts @@ -0,0 +1,29 @@ +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from '@rstest/core'; +import { runRstestCli } from '../../scripts'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +describe('test EsModulesLinkingError', () => { + it('should not print EsModulesLinkingError', async () => { + const { cli, expectExecSuccess } = await runRstestCli({ + command: 'rstest', + args: ['run'], + options: { + nodeOptions: { + cwd: join(__dirname, '../fixtures/esModulesLinkingError'), + }, + }, + }); + + await expectExecSuccess(); + + const logs = cli.stdout.split('\n').filter(Boolean); + + expect( + logs.find((log) => log.match(/ESModulesLinkingError: export 'value'/)), + ).toBeUndefined(); + }); +}); diff --git a/packages/core/src/core/plugins/basic.ts b/packages/core/src/core/plugins/basic.ts index 806066967..884611273 100644 --- a/packages/core/src/core/plugins/basic.ts +++ b/packages/core/src/core/plugins/basic.ts @@ -91,6 +91,8 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = ( // Keep require.resolve expressions. requireResolve: false, ...(config.module.parser.javascript || {}), + // suppress ESModulesLinkingError for exports that might be implemented in mock + exportsPresence: 'warn', }; config.resolve ??= {}; diff --git a/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap b/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap index 1649fcf4e..76bcb1b9e 100644 --- a/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap +++ b/packages/core/tests/core/__snapshots__/rsbuild.test.ts.snap @@ -35,7 +35,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (jsdom) 1`] = "module": { "parser": { "javascript": { - "exportsPresence": "error", + "exportsPresence": "warn", "importDynamic": false, "inlineConst": false, "requireAsExpression": false, @@ -555,7 +555,7 @@ exports[`prepareRsbuild > should generate rspack config correctly (node) 1`] = ` "module": { "parser": { "javascript": { - "exportsPresence": "error", + "exportsPresence": "warn", "importDynamic": false, "inlineConst": false, "requireAsExpression": false, @@ -1066,7 +1066,7 @@ exports[`prepareRsbuild > should generate rspack config correctly in watch mode "module": { "parser": { "javascript": { - "exportsPresence": "error", + "exportsPresence": "warn", "importDynamic": false, "inlineConst": false, "requireAsExpression": false, @@ -1584,7 +1584,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects "module": { "parser": { "javascript": { - "exportsPresence": "error", + "exportsPresence": "warn", "importDynamic": false, "inlineConst": false, "requireAsExpression": false, @@ -2104,7 +2104,7 @@ exports[`prepareRsbuild > should generate rspack config correctly with projects "module": { "parser": { "javascript": { - "exportsPresence": "error", + "exportsPresence": "warn", "importDynamic": false, "inlineConst": false, "requireAsExpression": false,