Skip to content

Commit f9955bb

Browse files
authored
fix: manualMockRoot in projects (#508)
1 parent 19184b0 commit f9955bb

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// we can also use `import`, but then
2+
// every export should be explicitly defined
3+
4+
const { fs } = require('memfs');
5+
6+
module.exports = fs;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { readFileSync } from 'node:fs';
2+
3+
export function readSomeFile(path: string) {
4+
return readFileSync(path, 'utf-8');
5+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect, it, rs } from '@rstest/core';
2+
import { fs } from 'memfs';
3+
import { readSomeFile } from '../src/readSomeFile';
4+
5+
rs.mock('node:fs');
6+
7+
it('should return correct text', () => {
8+
const path = '/hello-world.txt';
9+
fs.writeFileSync(path, 'hello world');
10+
11+
const text = readSomeFile(path);
12+
expect(text).toBe('hello world');
13+
});

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
2020
tools,
2121
performance,
2222
dev,
23+
testEnvironment,
2324
},
25+
rootPath,
2426
} = context.projects.find((p) => p.environmentName === name)!;
2527
return mergeEnvironmentConfig(
2628
config,
@@ -63,7 +65,7 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
6365
injectModulePathName: true,
6466
importMetaPathName: true,
6567
hoistMockModule: true,
66-
manualMockRoot: path.resolve(context.rootPath, '__mocks__'),
68+
manualMockRoot: path.resolve(rootPath, '__mocks__'),
6769
}),
6870
);
6971

@@ -90,7 +92,7 @@ export const pluginBasic: (context: RstestContext) => RsbuildPlugin = (
9092
config.resolve.extensionAlias['.js'] = ['.js', '.ts', '.tsx'];
9193
config.resolve.extensionAlias['.jsx'] = ['.jsx', '.tsx'];
9294

93-
if (context.normalizedConfig.testEnvironment === 'node') {
95+
if (testEnvironment === 'node') {
9496
// skip `module` field in Node.js environment.
9597
// ESM module resolved by module field is not always a native ESM module
9698
config.resolve.mainFields = config.resolve.mainFields?.filter(

0 commit comments

Comments
 (0)