diff --git a/packages/react-native/src/generators/application/application.spec.ts b/packages/react-native/src/generators/application/application.spec.ts index aa50f34e6be5b..5ae6588961856 100644 --- a/packages/react-native/src/generators/application/application.spec.ts +++ b/packages/react-native/src/generators/application/application.spec.ts @@ -60,6 +60,21 @@ describe('app', () => { expect(tsconfig.extends).toEqual('../../tsconfig.base.json'); expect(appTree.exists('apps/my-app/.eslintrc.json')).toBe(true); + expect(appTree.read('apps/my-app/jest.config.ts', 'utf-8')) + .toMatchInlineSnapshot(` + "module.exports = { + displayName: 'my-app', + preset: 'react-native', + resolver: '@nx/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'], + setupFilesAfterEnv: ['/test-setup.ts'], + moduleNameMapper: { + '\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock', + }, + coverageDirectory: '../../coverage/apps/my-app', + }; + " + `); }); it('should generate targets', async () => { @@ -71,7 +86,6 @@ describe('app', () => { install: false, }); const targets = readProjectConfiguration(appTree, 'my-app').targets; - console.log(targets.test); expect(targets.test).toBeDefined(); }); diff --git a/packages/react-native/src/generators/library/library.spec.ts b/packages/react-native/src/generators/library/library.spec.ts index 4851ef14ad040..86fadf77f0dea 100644 --- a/packages/react-native/src/generators/library/library.spec.ts +++ b/packages/react-native/src/generators/library/library.spec.ts @@ -242,6 +242,72 @@ describe('lib', () => { outputs: ['{options.outputFile}'], }); }); + + it('should generate test configuration', async () => { + await libraryGenerator(appTree, { + ...defaultSchema, + unitTestRunner: 'jest', + }); + + expect(appTree.read('libs/my-lib/tsconfig.spec.json', 'utf-8')) + .toMatchInlineSnapshot(` + "{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "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('libs/my-lib/jest.config.ts', 'utf-8')) + .toMatchInlineSnapshot(` + "module.exports = { + displayName: 'my-lib', + preset: 'react-native', + resolver: '@nx/jest/plugins/resolver', + moduleFileExtensions: ['ts', 'js', 'html', 'tsx', 'jsx'], + setupFilesAfterEnv: ['/test-setup.ts'], + moduleNameMapper: { + '\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock', + }, + coverageDirectory: '../../coverage/libs/my-lib', + }; + " + `); + const projectConfiguration = readProjectConfiguration(appTree, 'my-lib'); + expect(projectConfiguration.targets.test).toMatchInlineSnapshot(` + { + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true, + }, + }, + "executor": "@nx/jest:jest", + "options": { + "jestConfig": "libs/my-lib/jest.config.ts", + "passWithNoTests": true, + }, + "outputs": [ + "{workspaceRoot}/coverage/{projectRoot}", + ], + } + `); + }); }); describe('--buildable', () => { diff --git a/packages/react-native/src/utils/add-jest.ts b/packages/react-native/src/utils/add-jest.ts index ab38c72bb16fb..3a363006162ab 100644 --- a/packages/react-native/src/utils/add-jest.ts +++ b/packages/react-native/src/utils/add-jest.ts @@ -1,4 +1,4 @@ -import { Tree } from '@nx/devkit'; +import { Tree, offsetFromRoot } from '@nx/devkit'; import { configurationGenerator } from '@nx/jest'; export async function addJest( @@ -34,7 +34,10 @@ export async function addJest( setupFilesAfterEnv: ['/test-setup.${js ? 'js' : 'ts'}'], moduleNameMapper: { '\\\\.svg$': '@nx/react-native/plugins/jest/svg-mock' - } + }, + coverageDirectory: '${offsetFromRoot( + appProjectRoot + )}coverage/${appProjectRoot}' };`; host.write(configPath, content);