Skip to content

Commit 7f9a335

Browse files
committed
test: add proper test scenarios
1 parent 0a7e8bf commit 7f9a335

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

__e2e__/config.test.ts

+99
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ function createCorruptedSetupEnvScript() {
3535
};
3636
}
3737

38+
const modifyPackageJson = (dir: string, key: string, value: string) => {
39+
const packageJsonPath = path.join(dir, 'package.json');
40+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
41+
packageJson[key] = value;
42+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
43+
};
44+
3845
beforeEach(() => {
3946
// Clean up folder and re-create a new project
4047
cleanup(DIR);
@@ -176,3 +183,95 @@ test('should read user config from react-native.config.mjs', () => {
176183
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
177184
expect(stdout).toBe('test-command-esm');
178185
});
186+
187+
test('should fail if if using require() in ES module in react-native.config.mjs', () => {
188+
writeFiles(path.join(DIR, 'TestProject'), {
189+
'react-native.config.mjs': `
190+
const packageJSON = require('./package.json');
191+
${USER_CONFIG_ESM}
192+
`,
193+
});
194+
195+
const {stderr, stdout} = runCLI(path.join(DIR, 'TestProject'), [
196+
'test-command-esm',
197+
]);
198+
expect(stderr).toMatch('error Failed to load configuration of your project');
199+
expect(stdout).toMatch(
200+
'ReferenceError: require is not defined in ES module scope, you can use import instead',
201+
);
202+
});
203+
204+
test('should fail if if using require() in ES module with "type": "module" in package.json', () => {
205+
writeFiles(path.join(DIR, 'TestProject'), {
206+
'react-native.config.js': `
207+
const packageJSON = require('./package.json');
208+
${USER_CONFIG_ESM}
209+
`,
210+
});
211+
212+
modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');
213+
214+
const {stderr} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
215+
console.log(stderr);
216+
expect(stderr).toMatch('error Failed to load configuration of your project');
217+
});
218+
219+
test('should read config if using createRequire() helper in react-native.config.js with "type": "module" in package.json', () => {
220+
writeFiles(path.join(DIR, 'TestProject'), {
221+
'react-native.config.js': `
222+
import { createRequire } from 'node:module';
223+
const require = createRequire(import.meta.url);
224+
const packageJSON = require('./package.json');
225+
226+
${USER_CONFIG_ESM}
227+
`,
228+
});
229+
230+
modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');
231+
232+
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
233+
expect(stdout).toBe('test-command-esm');
234+
});
235+
236+
test('should read config if using require() in react-native.config.cjs with "type": "module" in package.json', () => {
237+
writeFiles(path.join(DIR, 'TestProject'), {
238+
'react-native.config.cjs': `
239+
const packageJSON = require('./package.json');
240+
${USER_CONFIG}
241+
`,
242+
});
243+
244+
modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');
245+
246+
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command']);
247+
expect(stdout).toMatch('test-command');
248+
});
249+
250+
test('should read config if using import/export in react-native.config.js with "type": "module" package.json', () => {
251+
writeFiles(path.join(DIR, 'TestProject'), {
252+
'react-native.config.js': `
253+
import {} from 'react';
254+
${USER_CONFIG_ESM}
255+
`,
256+
});
257+
258+
modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');
259+
260+
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
261+
expect(stdout).toMatch('test-command-esm');
262+
});
263+
264+
test('should read config if using import/export in react-native.config.mjs with "type": "commonjs" package.json', () => {
265+
writeFiles(path.join(DIR, 'TestProject'), {
266+
'react-native.config.mjs': `
267+
import {} from 'react';
268+
269+
${USER_CONFIG_ESM}
270+
`,
271+
});
272+
273+
modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'commonjs');
274+
275+
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
276+
expect(stdout).toMatch('test-command-esm');
277+
});

packages/cli-config/src/readConfigFromDisk.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import chalk from 'chalk';
1313
*/
1414
const searchPlaces = [
1515
'react-native.config.js',
16+
'react-native.config.cjs',
17+
'react-native.config.mjs',
1618
'react-native.config.ts',
1719
'react-native.config.mjs',
1820
];

0 commit comments

Comments
 (0)