Skip to content

Commit

Permalink
feat(testing): remove test runner for react native jest
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Jul 21, 2022
1 parent 33fe229 commit a532497
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 22 deletions.
15 changes: 6 additions & 9 deletions packages/react-native/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@
"cli": "nx",
"description": "Rename blacklistRE to blockList in metro.config.js",
"factory": "./src/migrations/update-14-2-1/rename-blockList-metro-config"
},
"rename-jest-preprocessor-14-4-0": {
"version": "14.4.0-beta.0",
"cli": "nx",
"description": "Remove transform in jest.config",
"factory": "./src/migrations/update-14-4-0/update-jest-config"
}
},
"packageJsonUpdates": {
Expand Down Expand Up @@ -733,15 +739,6 @@
}
}
},
"14.4.0": {
"version": "14.4.0-beta.0",
"packages": {
"jest-jasmine2": {
"version": "~28.1.1",
"alwaysAddToPackageJson": false
}
}
},
"14.4.4": {
"version": "14.4.4-beta.0",
"packages": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { addProjectConfiguration, Tree } from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import update from './update-jest-config';

describe('Update Jest Config', () => {
let tree: Tree;

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
addProjectConfiguration(tree, 'products', {
root: 'apps/products',
sourceRoot: 'apps/products/src',
targets: {
start: {
executor: '@nrwl/react-native:start',
options: {
port: 8081,
},
},
test: {
executor: '@nrwl/jest:jest',
options: {
jestConfig: 'apps/products/jest.config.js',
passWithNoTests: true,
},
},
},
});
});

it(`should not update if the code does not contain existing test runner`, async () => {
tree.write(
'apps/products/jest.config.js',
`module.exports = {
preset: 'react-native',
testRunner: 'other',
transform: {
".xyz": "xyz-transform"
}
};`
);
await update(tree);

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

it(`should update if the code contains existing test runner`, async () => {
tree.write(
'apps/products/jest.config.js',
`module.exports = {
preset: 'react-native',
testRunner: 'jest-jasmine2',
transform: {
'\\.(js|ts|tsx)$': require.resolve('react-native/jest/preprocessor.js'),
'^.+\\.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp)$': require.resolve(
'react-native/jest/assetFileTransformer.js',
}
};`
);
await update(tree);

const jestConfig = tree.read('apps/products/jest.config.js', 'utf-8');
expect(jestConfig).not.toContain(`testRunner: 'jest-jasmine2',`);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
Tree,
formatFiles,
getProjects,
ProjectConfiguration,
logger,
stripIndents,
} from '@nrwl/devkit';

/**
* Remove the testRunner
*/
export default async function update(tree: Tree) {
const projects = getProjects(tree);

projects.forEach((project) => {
if (project.targets?.start?.executor !== '@nrwl/react-native:start') return;

mockSvgInJestConfig(tree, project);
});

await formatFiles(tree);
}

function mockSvgInJestConfig(host: Tree, project: ProjectConfiguration) {
const jestConfigPath = project.targets?.test?.options?.jestConfig;
if (!jestConfigPath || !host.exists(jestConfigPath)) return;
try {
const contents = host.read(jestConfigPath, 'utf-8');
if (contents.includes('@nrwl/react-native/plugins/jest/preprocessor.js'))
return;
host.write(
jestConfigPath,
contents.replace(`testRunner: 'jest-jasmine2',\n`, '')
);
formatFiles(host);
} catch {
logger.error(
stripIndents`Unable to update ${jestConfigPath} for project ${project.root}.`
);
}
}
15 changes: 2 additions & 13 deletions packages/react-native/src/utils/add-jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { addDependenciesToPackageJson, Tree } from '@nrwl/devkit';
import { Tree } from '@nrwl/devkit';
import { jestProjectGenerator } from '@nrwl/jest';
import { jestVersion } from '@nrwl/jest/src/utils/versions';

export async function addJest(
host: Tree,
Expand All @@ -27,7 +26,6 @@ export async function addJest(
const content = `module.exports = {
displayName: '${projectName}',
preset: 'react-native',
testRunner: 'jest-jasmine2',
resolver: '@nrwl/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/test-setup.${js ? 'js' : 'ts'}'],
Expand All @@ -43,14 +41,5 @@ export async function addJest(
};`;
host.write(configPath, content);

const installTask = addDependenciesToPackageJson(
host,
{},
{ 'jest-jasmine2': jestVersion }
);

return () => {
jestTask();
installTask();
};
return jestTask;
}

0 comments on commit a532497

Please sign in to comment.