@@ -35,6 +35,13 @@ function createCorruptedSetupEnvScript() {
35
35
} ;
36
36
}
37
37
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
+
38
45
beforeEach ( ( ) => {
39
46
// Clean up folder and re-create a new project
40
47
cleanup ( DIR ) ;
@@ -176,3 +183,95 @@ test('should read user config from react-native.config.mjs', () => {
176
183
const { stdout} = runCLI ( path . join ( DIR , 'TestProject' ) , [ 'test-command-esm' ] ) ;
177
184
expect ( stdout ) . toBe ( 'test-command-esm' ) ;
178
185
} ) ;
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-native-community/cli';
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-native-community/cli';
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
+ } ) ;
0 commit comments