Skip to content

Commit

Permalink
test: add proper test scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed Aug 31, 2024
1 parent 0a7e8bf commit c670360
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
99 changes: 99 additions & 0 deletions __e2e__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ function createCorruptedSetupEnvScript() {
};
}

const modifyPackageJson = (dir: string, key: string, value: string) => {
const packageJsonPath = path.join(dir, 'package.json');
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson[key] = value;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
};

beforeEach(() => {
// Clean up folder and re-create a new project
cleanup(DIR);
Expand Down Expand Up @@ -176,3 +183,95 @@ test('should read user config from react-native.config.mjs', () => {
const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
expect(stdout).toBe('test-command-esm');
});

test('should fail if if using require() in ES module in react-native.config.mjs', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.mjs': `
const packageJSON = require('./package.json');
${USER_CONFIG_ESM}
`,
});

const {stderr, stdout} = runCLI(path.join(DIR, 'TestProject'), [
'test-command-esm',
]);
expect(stderr).toMatch('error Failed to load configuration of your project');
expect(stdout).toMatch(
'ReferenceError: require is not defined in ES module scope, you can use import instead',
);
});

test('should fail if if using require() in ES module with "type": "module" in package.json', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.js': `
const packageJSON = require('./package.json');
${USER_CONFIG_ESM}
`,
});

modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');

const {stderr} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
console.log(stderr);
expect(stderr).toMatch('error Failed to load configuration of your project');
});

test('should read config if using createRequire() helper in react-native.config.js with "type": "module" in package.json', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.js': `
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const packageJSON = require('./package.json');
${USER_CONFIG_ESM}
`,
});

modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');

const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
expect(stdout).toBe('test-command-esm');
});

test('should read config if using require() in react-native.config.cjs with "type": "module" in package.json', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.cjs': `
const packageJSON = require('./package.json');
${USER_CONFIG}
`,
});

modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');

const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command']);
expect(stdout).toMatch('test-command');
});

test('should read config if using import/export in react-native.config.js with "type": "module" package.json', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.js': `
import {} from '@react-native-community/cli';
${USER_CONFIG_ESM}
`,
});

modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'module');

const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
expect(stdout).toMatch('test-command-esm');
});

test('should read config if using import/export in react-native.config.mjs with "type": "commonjs" package.json', () => {
writeFiles(path.join(DIR, 'TestProject'), {
'react-native.config.mjs': `
import {} from '@react-native-community/cli';
${USER_CONFIG_ESM}
`,
});

modifyPackageJson(path.join(DIR, 'TestProject'), 'type', 'commonjs');

const {stdout} = runCLI(path.join(DIR, 'TestProject'), ['test-command-esm']);
expect(stdout).toMatch('test-command-esm');
});
2 changes: 2 additions & 0 deletions packages/cli-config/src/readConfigFromDisk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import chalk from 'chalk';
*/
const searchPlaces = [
'react-native.config.js',
'react-native.config.cjs',
'react-native.config.mjs',
'react-native.config.ts',
'react-native.config.mjs',
];
Expand Down

0 comments on commit c670360

Please sign in to comment.