Skip to content

Commit

Permalink
fix(react-native): fix test-setup for react native/expo jest (#23314)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #23027
#22198
  • Loading branch information
xiongemi authored May 17, 2024
1 parent 217a349 commit a53fee8
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 18 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"types": ["node"]
},
"files": ["<%= offsetFromRoot %>node_modules/@nx/expo/typings/svg.d.ts"],
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.spec.tsx", "test-setup.ts"],
"exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.spec.tsx", "src/test-setup.ts"],
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"]
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"outDir": "<%= offsetFromRoot %>dist/out-tsc",
"types": ["node"]
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "test-setup.ts"],
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "src/test-setup.ts"],
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}
60 changes: 59 additions & 1 deletion packages/expo/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ describe('lib', () => {
});
});

describe('--unit-test-runner none', () => {
describe('--unit-test-runner', () => {
it('should not generate test configuration', async () => {
await expoLibraryGenerator(appTree, {
...defaultSchema,
Expand All @@ -220,6 +220,64 @@ describe('lib', () => {
}
`);
});

it('should generate test configuration', async () => {
await expoLibraryGenerator(appTree, {
...defaultSchema,
unitTestRunner: 'jest',
});

expect(appTree.read('my-lib/tsconfig.spec.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.test.tsx",
"src/**/*.spec.tsx",
"src/**/*.test.js",
"src/**/*.spec.js",
"src/**/*.test.jsx",
"src/**/*.spec.jsx",
"src/**/*.d.ts"
]
}
"
`);
expect(appTree.read('my-lib/jest.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"module.exports = {
displayName: 'my-lib',
resolver: '@nx/jest/plugins/resolver',
preset: 'jest-expo',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
moduleNameMapper: {
'\\\\.svg$': '@nx/expo/plugins/jest/svg-mock',
},
transform: {
'.[jt]sx?$': [
'babel-jest',
{
configFile: __dirname + '/.babelrc.js',
},
],
'^.+.(bmp|gif|jpg|jpeg|mp4|png|psd|svg|webp|ttf|otf|m4v|mov|mp4|mpeg|mpg|webm|aac|aiff|caf|m4a|mp3|wav|html|pdf|obj)$':
require.resolve('jest-expo/src/preset/assetFileTransformer.js'),
},
coverageDirectory: '../coverage/my-lib',
};
"
`);
});
});

describe('--buildable', () => {
Expand Down
9 changes: 6 additions & 3 deletions packages/expo/src/utils/add-jest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tree } from '@nx/devkit';
import { Tree, offsetFromRoot } from '@nx/devkit';
import { configurationGenerator } from '@nx/jest';

export async function addJest(
Expand All @@ -19,7 +19,7 @@ export async function addJest(
project: projectName,
supportTsx: true,
skipSerializers: true,
setupFile: 'none',
setupFile: 'react-native',
compiler: 'babel',
skipPackageJson,
skipFormat: true,
Expand All @@ -34,7 +34,7 @@ export async function addJest(
resolver: '@nx/jest/plugins/resolver',
preset: 'jest-expo',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/test-setup.${js ? 'js' : 'ts'}'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.${js ? 'js' : 'ts'}'],
moduleNameMapper: {
'\\\\.svg$': '@nx/expo/plugins/jest/svg-mock'
},
Expand All @@ -49,6 +49,9 @@ export async function addJest(
'jest-expo/src/preset/assetFileTransformer.js'
),
},
coverageDirectory: '${offsetFromRoot(
appProjectRoot
)}coverage/${appProjectRoot}'
};`;
host.write(configPath, content);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<% if(setupFile === 'react-native') { %>import '@testing-library/jest-native/extend-expect';<% } %>
2 changes: 1 addition & 1 deletion packages/jest/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface JestProjectSchema {
* @deprecated use setupFile instead
*/
skipSetupFile?: boolean;
setupFile?: 'angular' | 'web-components' | 'none';
setupFile?: 'angular' | 'web-components' | 'react-native' | 'none';
skipSerializers?: boolean;
testEnvironment?: 'node' | 'jsdom' | 'none';
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('app', () => {
preset: 'react-native',
resolver: '@nx/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/test-setup.ts'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
moduleNameMapper: {
'\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock',
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"types": ["node"]
},
"files": ["<%= offsetFromRoot %>node_modules/@nx/react-native/typings/svg.d.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "test-setup.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "src/test-setup.ts"],
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.js", "src/**/*.jsx"]
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"outDir": "<%= offsetFromRoot %>dist/out-tsc",
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "test-setup.ts"],
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "src/test-setup.ts"],
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
5 changes: 3 additions & 2 deletions packages/react-native/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('lib', () => {
});
});

describe('--unit-test-runner none', () => {
describe('--unit-test-runner', () => {
it('should not generate test configuration', async () => {
await libraryGenerator(appTree, {
...defaultSchema,
Expand All @@ -225,6 +225,7 @@ describe('lib', () => {
"module": "commonjs",
"types": ["jest", "node"]
},
"files": ["src/test-setup.ts"],
"include": [
"jest.config.ts",
"src/**/*.test.ts",
Expand All @@ -247,7 +248,7 @@ describe('lib', () => {
preset: 'react-native',
resolver: '@nx/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/test-setup.ts'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
moduleNameMapper: {
'\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock',
},
Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/src/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function addJest(
project: projectName,
supportTsx: true,
skipSerializers: true,
setupFile: 'none',
setupFile: 'react-native',
compiler: 'babel',
skipPackageJson,
skipFormat: true,
Expand All @@ -33,7 +33,7 @@ export async function addJest(
preset: 'react-native',
resolver: '@nx/jest/plugins/resolver',
moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'],
setupFilesAfterEnv: ['<rootDir>/test-setup.${js ? 'js' : 'ts'}'],
setupFilesAfterEnv: ['<rootDir>/src/test-setup.${js ? 'js' : 'ts'}'],
moduleNameMapper: {
'\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock'
},
Expand Down

0 comments on commit a53fee8

Please sign in to comment.