-
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): add preprocessor for react native jest
- Loading branch information
Showing
8 changed files
with
229 additions
and
12 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
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,89 @@ | ||
/** | ||
* From https://github.com/facebook/react-native/blob/main/jest/private/preprocessor.js | ||
* Need to change it to be compatible with jest 28 | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @format | ||
* @flow | ||
*/ | ||
|
||
/* eslint-env node */ | ||
|
||
'use strict'; | ||
|
||
const babelRegisterOnly = require('metro-babel-register'); | ||
const createCacheKeyFunction = | ||
require('@jest/create-cache-key-function').default; | ||
|
||
const { transformSync: babelTransformSync } = require('@babel/core'); | ||
const generate = require('@babel/generator').default; | ||
|
||
const nodeFiles = new RegExp( | ||
[ | ||
'/metro(?:-[^/]*)?/', // metro, metro-core, metro-source-map, metro-etc. | ||
].join('|') | ||
); | ||
const nodeOptions = babelRegisterOnly.config([nodeFiles]); | ||
|
||
babelRegisterOnly([]); | ||
|
||
const transformer = require('metro-react-native-babel-transformer'); | ||
module.exports = { | ||
process(src /*: string */, file /*: string */) /*: {code: string, ...} */ { | ||
if (nodeFiles.test(file)) { | ||
// node specific transforms only | ||
return { | ||
code: babelTransformSync(src, { | ||
filename: file, | ||
sourceType: 'script', | ||
...nodeOptions, | ||
ast: false, | ||
}), | ||
}; | ||
} | ||
|
||
const { ast } = transformer.transform({ | ||
filename: file, | ||
options: { | ||
ast: true, // needed for open source (?) https://github.com/facebook/react-native/commit/f8d6b97140cffe8d18b2558f94570c8d1b410d5c#r28647044 | ||
dev: true, | ||
enableBabelRuntime: false, | ||
experimentalImportSupport: false, | ||
globalPrefix: '', | ||
hot: false, | ||
inlineRequires: true, | ||
minify: false, | ||
platform: '', | ||
projectRoot: '', | ||
publicPath: '/assets', | ||
retainLines: true, | ||
sourceType: 'unambiguous', // b7 required. detects module vs script mode | ||
}, | ||
src, | ||
}); | ||
|
||
return generate( | ||
ast, | ||
// $FlowFixMe[prop-missing] Error found when improving flow typing for libs | ||
{ | ||
code: true, | ||
comments: false, | ||
compact: false, | ||
filename: file, | ||
retainLines: true, | ||
sourceFileName: file, | ||
sourceMaps: true, | ||
}, | ||
src | ||
); | ||
}, | ||
|
||
getCacheKey: (createCacheKeyFunction([ | ||
__filename, | ||
require.resolve('metro-react-native-babel-transformer'), | ||
require.resolve('@babel/core/package.json'), | ||
]) /*: any */), | ||
}; |
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
68 changes: 68 additions & 0 deletions
68
packages/react-native/src/migrations/update-14-4-0/rename-jest-preprocessor.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,68 @@ | ||
import { addProjectConfiguration, readJson, Tree } from '@nrwl/devkit'; | ||
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing'; | ||
import update from './rename-jest-preprocessor'; | ||
|
||
describe('Rename jest preprocessor', () => { | ||
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 preprocessor`, async () => { | ||
tree.write( | ||
'apps/products/jest.config.js', | ||
`module.exports = { | ||
preset: 'react-native', | ||
};` | ||
); | ||
await update(tree); | ||
|
||
const jestConfig = tree.read('apps/products/jest.config.js', 'utf-8'); | ||
expect(jestConfig).not.toContain( | ||
`@nrwl/react-native/plugins/jest/preprocessor.js` | ||
); | ||
}); | ||
|
||
it(`should update if the code contains existing preprocessor`, 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).toContain( | ||
`@nrwl/react-native/plugins/jest/preprocessor.js` | ||
); | ||
expect(jestConfig).not.toContain(`testRunner: 'jest-jasmine2',`); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/react-native/src/migrations/update-14-4-0/rename-jest-preprocessor.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,47 @@ | ||
import { | ||
Tree, | ||
formatFiles, | ||
getProjects, | ||
ProjectConfiguration, | ||
logger, | ||
stripIndents, | ||
} from '@nrwl/devkit'; | ||
|
||
/** | ||
* Rename reprocessor in jest to be from @nrwl/react-native | ||
* and 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( | ||
'react-native/jest/preprocessor.js', | ||
'@nrwl/react-native/plugins/jest/preprocessor.js' | ||
) | ||
.replace(`testRunner: 'jest-jasmine2',\n`, '') | ||
); | ||
} 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
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
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