-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(testing): remove test runner for react native jest
- Loading branch information
Showing
4 changed files
with
116 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/react-native/src/migrations/update-14-4-0/update-jest-config.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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',`); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
packages/react-native/src/migrations/update-14-4-0/update-jest-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}.` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters