From 2d135f952bd2cd417741148ee173cc57753cd179 Mon Sep 17 00:00:00 2001 From: master96 Date: Tue, 10 Dec 2024 06:15:39 +0000 Subject: [PATCH] fix(react-native): typescript lib schema (#27955) ## Current Behavior Currently when react native generators are used. They won't exclude `.test` extension files. ## Expected Behavior React native generators should exclude `.test` files in tsconfig lib ## Related Issue(s) Fixes # Co-authored-by: vivek ajage --- packages/expo/plugins/plugin.ts | 1 - .../application/application.spec.ts | 8 ++- .../files/base/tsconfig.app.json.template | 12 +++- .../files/lib/tsconfig.lib.json.template | 12 +++- .../src/generators/library/library.spec.ts | 66 ++++++++++++++++--- .../application/application.spec.ts | 9 ++- .../files/app/tsconfig.app.json.template | 12 +++- .../files/lib/tsconfig.lib.json.template | 12 +++- .../src/generators/library/library.spec.ts | 31 +++++++++ 9 files changed, 147 insertions(+), 16 deletions(-) diff --git a/packages/expo/plugins/plugin.ts b/packages/expo/plugins/plugin.ts index 8da43105c7f8a..5cdc5342f1e3e 100644 --- a/packages/expo/plugins/plugin.ts +++ b/packages/expo/plugins/plugin.ts @@ -17,7 +17,6 @@ import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs'; import { existsSync, readdirSync } from 'fs'; import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes'; import { workspaceDataDirectory } from 'nx/src/utils/cache-directory'; -import { combineGlobPatterns } from 'nx/src/utils/globs'; import { hashObject } from 'nx/src/devkit-internals'; import { loadConfigFile } from '@nx/devkit/src/utils/config-utils'; diff --git a/packages/expo/src/generators/application/application.spec.ts b/packages/expo/src/generators/application/application.spec.ts index 3753b0a235358..24ce4173ec20e 100644 --- a/packages/expo/src/generators/application/application.spec.ts +++ b/packages/expo/src/generators/application/application.spec.ts @@ -411,10 +411,16 @@ describe('app', () => { }, "exclude": [ "dist", - "jest.config.ts", + "**/*.test.ts", "**/*.spec.ts", + "**/*.test.tsx", "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", "src/test-setup.ts", + "jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts", "eslint.config.js", diff --git a/packages/expo/src/generators/application/files/base/tsconfig.app.json.template b/packages/expo/src/generators/application/files/base/tsconfig.app.json.template index 80217c0f62c44..eada2d0dd751f 100644 --- a/packages/expo/src/generators/application/files/base/tsconfig.app.json.template +++ b/packages/expo/src/generators/application/files/base/tsconfig.app.json.template @@ -5,6 +5,16 @@ "types": ["node"] }, "files": ["<%= offsetFromRoot %>node_modules/@nx/expo/typings/svg.d.ts"], - "exclude": ["jest.config.ts", "**/*.spec.ts", "**/*.spec.tsx", "src/test-setup.ts"], + "exclude": [ + "**/*.test.ts", + "**/*.spec.ts", + "**/*.test.tsx", + "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", + "src/test-setup.ts" + ], "include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"] } diff --git a/packages/expo/src/generators/library/files/lib/tsconfig.lib.json.template b/packages/expo/src/generators/library/files/lib/tsconfig.lib.json.template index c0b1d9caa1817..1576242c6bdcc 100644 --- a/packages/expo/src/generators/library/files/lib/tsconfig.lib.json.template +++ b/packages/expo/src/generators/library/files/lib/tsconfig.lib.json.template @@ -4,6 +4,16 @@ "outDir": "<%= offsetFromRoot %>dist/out-tsc", "types": ["node"] }, - "exclude": ["**/*.spec.ts", "**/*.spec.tsx", "src/test-setup.ts"], + "exclude": [ + "**/*.test.ts", + "**/*.spec.ts", + "**/*.test.tsx", + "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", + "src/test-setup.ts" + ], "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"] } diff --git a/packages/expo/src/generators/library/library.spec.ts b/packages/expo/src/generators/library/library.spec.ts index 674abc5d2e322..3e08a40af6725 100644 --- a/packages/expo/src/generators/library/library.spec.ts +++ b/packages/expo/src/generators/library/library.spec.ts @@ -87,14 +87,64 @@ describe('lib', () => { path: './tsconfig.spec.json', }, ]); - expect( - tsconfigJson.compilerOptions.forceConsistentCasingInFileNames - ).toEqual(true); - expect(tsconfigJson.compilerOptions.strict).toEqual(true); - expect(tsconfigJson.compilerOptions.noImplicitReturns).toEqual(true); - expect(tsconfigJson.compilerOptions.noFallthroughCasesInSwitch).toEqual( - true - ); + expect(tsconfigJson).toMatchInlineSnapshot(` + { + "compilerOptions": { + "allowJs": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "jsx": "react-jsx", + "noFallthroughCasesInSwitch": true, + "noImplicitReturns": true, + "strict": true, + }, + "extends": "../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json", + }, + { + "path": "./tsconfig.spec.json", + }, + ], + } + `); + + expect(readJson(appTree, 'my-lib/tsconfig.lib.json')) + .toMatchInlineSnapshot(` + { + "compilerOptions": { + "outDir": "../dist/out-tsc", + "types": [ + "node", + ], + }, + "exclude": [ + "**/*.test.ts", + "**/*.spec.ts", + "**/*.test.tsx", + "**/*.spec.tsx", + "**/*.test.js", + "**/*.spec.js", + "**/*.test.jsx", + "**/*.spec.jsx", + "src/test-setup.ts", + "jest.config.ts", + "src/**/*.spec.ts", + "src/**/*.test.ts", + ], + "extends": "./tsconfig.json", + "include": [ + "**/*.js", + "**/*.jsx", + "**/*.ts", + "**/*.tsx", + ], + } + `); }); it('should extend the local tsconfig.json with tsconfig.spec.json', async () => { diff --git a/packages/react-native/src/generators/application/application.spec.ts b/packages/react-native/src/generators/application/application.spec.ts index 5d2381ef171d1..51f3fc653e930 100644 --- a/packages/react-native/src/generators/application/application.spec.ts +++ b/packages/react-native/src/generators/application/application.spec.ts @@ -336,11 +336,16 @@ describe('app', () => { }, "exclude": [ "dist", - "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/test-setup.ts", - "src/**/*.test.ts", + "jest.config.ts", "eslint.config.js", "eslint.config.cjs", "eslint.config.mjs", diff --git a/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template b/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template index 7b33f4033a0e3..07bde42457765 100644 --- a/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template +++ b/packages/react-native/src/generators/application/files/app/tsconfig.app.json.template @@ -5,6 +5,16 @@ "types": ["node"] }, "files": ["<%= offsetFromRoot %>node_modules/@nx/react-native/typings/svg.d.ts"], - "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "src/test-setup.ts"], + "exclude": [ + "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/test-setup.ts" + ], "include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.js", "src/**/*.jsx"] } diff --git a/packages/react-native/src/generators/library/files/lib/tsconfig.lib.json.template b/packages/react-native/src/generators/library/files/lib/tsconfig.lib.json.template index 3532cd09c1b96..106320c8aa29d 100644 --- a/packages/react-native/src/generators/library/files/lib/tsconfig.lib.json.template +++ b/packages/react-native/src/generators/library/files/lib/tsconfig.lib.json.template @@ -4,6 +4,16 @@ "outDir": "<%= offsetFromRoot %>dist/out-tsc", "types": ["node"] }, - "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.spec.tsx", "src/test-setup.ts"], + "exclude": [ + "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/test-setup.ts" + ], "include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"] } diff --git a/packages/react-native/src/generators/library/library.spec.ts b/packages/react-native/src/generators/library/library.spec.ts index 31d182ce6ca6c..1cb5a433aa2cb 100644 --- a/packages/react-native/src/generators/library/library.spec.ts +++ b/packages/react-native/src/generators/library/library.spec.ts @@ -187,6 +187,37 @@ describe('lib', () => { path: './tsconfig.spec.json', }, ]); + + expect(readJson(appTree, 'my-dir/tsconfig.lib.json')) + .toMatchInlineSnapshot(` + { + "compilerOptions": { + "outDir": "../dist/out-tsc", + "types": [ + "node", + ], + }, + "exclude": [ + "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/test-setup.ts", + "jest.config.ts", + ], + "extends": "./tsconfig.json", + "include": [ + "src/**/*.js", + "src/**/*.jsx", + "src/**/*.ts", + "src/**/*.tsx", + ], + } + `); }); it('should extend from root tsconfig.json when no tsconfig.base.json', async () => {