Skip to content

Commit

Permalink
feat(testing): add migration script for react native to rename .babel…
Browse files Browse the repository at this point in the history
…rc to babel.config.json
  • Loading branch information
xiongemi committed Aug 17, 2022
1 parent 6d95a71 commit 0a49d95
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
Expand Up @@ -20,7 +20,7 @@ describe('Rename jest preprocessor', () => {
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/products/jest.config.js',
jestConfig: 'apps/products/jest.config.ts',
passWithNoTests: true,
},
},
Expand All @@ -30,20 +30,20 @@ describe('Rename jest preprocessor', () => {

it(`should not remove transfrom if the code does not contain existing preprocessor`, async () => {
tree.write(
'apps/products/jest.config.js',
'apps/products/jest.config.ts',
`module.exports = {
preset: 'react-native',
};`
);
await update(tree);

const jestConfig = tree.read('apps/products/jest.config.js', 'utf-8');
const jestConfig = tree.read('apps/products/jest.config.ts', 'utf-8');
expect(jestConfig).not.toContain(`transform`);
});

it(`should remove transform if the code contains existing preprocessor`, async () => {
tree.write(
'apps/products/jest.config.js',
'apps/products/jest.config.ts',
`module.exports = {
preset: 'react-native',
testRunner: 'jest-jasmine2',
Expand All @@ -57,8 +57,53 @@ describe('Rename jest preprocessor', () => {
);
await update(tree);

const jestConfig = tree.read('apps/products/jest.config.js', 'utf-8');
const jestConfig = tree.read('apps/products/jest.config.ts', 'utf-8');
expect(jestConfig).not.toContain('transfrom: {');
expect(jestConfig).not.toContain(`testRunner: 'jest-jasmine2',`);
});

it(`should rename .babelrc to babel.config.json`, async () => {
tree.write(
'apps/products/jest.config.ts',
`module.exports = {
preset: 'react-native',
};`
);
tree.write(
'apps/products/.babelrc',
`{
"presets": ["module:metro-react-native-babel-preset"]
}`
);
await update(tree);

expect(tree.exists('apps/products/.babelrc')).toBeFalsy();
expect(tree.exists('apps/products/babel.config.json')).toBeTruthy();
const babelConfigJson = tree.read(
'apps/products/babel.config.json',
'utf-8'
);
expect(babelConfigJson).toContain(
`"presets": ["module:metro-react-native-babel-preset"]`
);
});

it(`should not rename .babelrc to babel.config.json if app is not react native`, async () => {
tree.write(
'apps/products/jest.config.ts',
`module.exports = {
preset: 'other',
};`
);
tree.write(
'apps/products/.babelrc',
`{
"presets": ["module:metro-react-native-babel-preset"]
}`
);
await update(tree);

expect(tree.exists('apps/products/.babelrc')).toBeTruthy();
expect(tree.exists('apps/products/babel.config.json')).toBeFalsy();
});
});
Expand Up @@ -7,16 +7,29 @@ import {
Tree,
} from '@nrwl/devkit';
import { removePropertyFromJestConfig } from '@nrwl/jest';
import { JestExecutorOptions } from '@nrwl/jest/src/executors/jest/schema';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
import { join } from 'path';

/**
* Remove transfrom and testRunner in jest
*/
export default async function update(tree: Tree) {
forEachExecutorOptions(tree, '@nrwl/react-native:start', (_, projectName) => {
const project = readProjectConfiguration(tree, projectName);
removeTransform(tree, project);
});
forEachExecutorOptions<JestExecutorOptions>(
tree,
'@nrwl/jest:jest',
(options, projectName) => {
if (options.jestConfig && tree.exists(options.jestConfig)) {
const jestConfig = tree.read(options.jestConfig, 'utf-8');

if (jestConfig.includes(`preset: 'react-native'`)) {
const project = readProjectConfiguration(tree, projectName);
removeTransform(tree, project);
renameBabelJson(tree, project);
}
}
}
);

await formatFiles(tree);
}
Expand All @@ -33,3 +46,23 @@ function removeTransform(host: Tree, project: ProjectConfiguration) {
);
}
}

function renameBabelJson(host: Tree, project: ProjectConfiguration) {
const babelrcPath = join(project.root, '.babelrc');
const babelJsonPath = join(project.root, 'babel.config.json');
if (!host.exists(babelrcPath)) {
return;
}
try {
const buffer = host.read(babelrcPath);
if (!buffer) {
return;
}
host.write(babelJsonPath, buffer);
host.delete(babelrcPath);
} catch {
logger.error(
stripIndents`Unable to rename from ${babelrcPath} to ${babelJsonPath} for project ${project.root}.`
);
}
}

0 comments on commit 0a49d95

Please sign in to comment.