From 7092b7a7ce6b558e2b58f95ad076f82383bac571 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 23 Nov 2022 18:07:09 +0100 Subject: [PATCH] fix(angular): remove karma test.ts file from generation --- .../__snapshots__/karma-project.spec.ts.snap | 85 ++----------------- .../karma-project/files/src/test.ts__tmpl__ | 22 ----- .../karma-project/files/tsconfig.spec.json | 1 - .../karma-project/karma-project.spec.ts | 23 +---- .../karma-project/lib/update-tsconfig.ts | 8 +- .../lib/update-workspace-config.ts | 4 +- .../src/generators/library/library.spec.ts | 1 - 7 files changed, 17 insertions(+), 127 deletions(-) delete mode 100644 packages/angular/src/generators/karma-project/files/src/test.ts__tmpl__ diff --git a/packages/angular/src/generators/karma-project/__snapshots__/karma-project.spec.ts.snap b/packages/angular/src/generators/karma-project/__snapshots__/karma-project.spec.ts.snap index 81199a7ac34ca..9aab0ae357096 100644 --- a/packages/angular/src/generators/karma-project/__snapshots__/karma-project.spec.ts.snap +++ b/packages/angular/src/generators/karma-project/__snapshots__/karma-project.spec.ts.snap @@ -1,55 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`karmaProject applications should create a test.ts 1`] = ` -"// This file is required by karma.conf.js and loads recursively all the .spec and framework files - -import 'zone.js/dist/zone-testing'; -import { getTestBed } from '@angular/core/testing'; -import { - BrowserDynamicTestingModule, - platformBrowserDynamicTesting -} from '@angular/platform-browser-dynamic/testing'; - -declare const require: any; - -// First, initialize the Angular testing environment. -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting() -); -// Then we find all the tests. -const context = require.context('./', true, /\\\\.spec\\\\.ts$/); -// And load the modules. -context.keys().map(context); -" -`; - -exports[`karmaProject library should create a test.ts 1`] = ` -"// This file is required by karma.conf.js and loads recursively all the .spec and framework files - -import 'zone.js/dist/zone'; - -import 'zone.js/dist/zone-testing'; -import { getTestBed } from '@angular/core/testing'; -import { - BrowserDynamicTestingModule, - platformBrowserDynamicTesting -} from '@angular/platform-browser-dynamic/testing'; - -declare const require: any; - -// First, initialize the Angular testing environment. -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting() -); -// Then we find all the tests. -const context = require.context('./', true, /\\\\.spec\\\\.ts$/); -// And load the modules. -context.keys().map(context); -" -`; - exports[`karmaProject should create a karma.conf.js 1`] = ` "// Karma configuration file, see link for more information // https://karma-runner.github.io/1.0/config/configuration-file.html @@ -75,19 +25,9 @@ exports[`karmaProject should generate files 1`] = ` \\"extends\\": \\"./tsconfig.json\\", \\"compilerOptions\\": { \\"outDir\\": \\"../../dist/out-tsc\\", - \\"types\\": [ - \\"jasmine\\", - \\"node\\" - ] + \\"types\\": [\\"jasmine\\", \\"node\\"] }, - \\"files\\": [ - \\"src/test.ts\\" - ], - \\"include\\": [ - \\"**/*.spec.ts\\", - \\"**/*.test.ts\\", - \\"**/*.d.ts\\" - ] + \\"include\\": [\\"**/*.spec.ts\\", \\"**/*.test.ts\\", \\"**/*.d.ts\\"] } " `; @@ -102,14 +42,13 @@ exports[`karmaProject should generate files and correctly add polyfills if it is \\"node\\" ] }, - \\"files\\": [ - \\"src/test.ts\\", - \\"src/polyfills.ts\\" - ], \\"include\\": [ \\"**/*.spec.ts\\", \\"**/*.test.ts\\", \\"**/*.d.ts\\" + ], + \\"files\\": [ + \\"src/polyfills.ts\\" ] } " @@ -120,19 +59,9 @@ exports[`karmaProject should generate files and not add polyfills if it is using \\"extends\\": \\"./tsconfig.json\\", \\"compilerOptions\\": { \\"outDir\\": \\"../../dist/out-tsc\\", - \\"types\\": [ - \\"jasmine\\", - \\"node\\" - ] + \\"types\\": [\\"jasmine\\", \\"node\\"] }, - \\"files\\": [ - \\"src/test.ts\\" - ], - \\"include\\": [ - \\"**/*.spec.ts\\", - \\"**/*.test.ts\\", - \\"**/*.d.ts\\" - ] + \\"include\\": [\\"**/*.spec.ts\\", \\"**/*.test.ts\\", \\"**/*.d.ts\\"] } " `; diff --git a/packages/angular/src/generators/karma-project/files/src/test.ts__tmpl__ b/packages/angular/src/generators/karma-project/files/src/test.ts__tmpl__ deleted file mode 100644 index db820813ab65d..0000000000000 --- a/packages/angular/src/generators/karma-project/files/src/test.ts__tmpl__ +++ /dev/null @@ -1,22 +0,0 @@ -// This file is required by karma.conf.js and loads recursively all the .spec and framework files -<% if (isLibrary) { %> -import 'zone.js/dist/zone'; -<% } %> -import 'zone.js/dist/zone-testing'; -import { getTestBed } from '@angular/core/testing'; -import { - BrowserDynamicTestingModule, - platformBrowserDynamicTesting -} from '@angular/platform-browser-dynamic/testing'; - -declare const require: any; - -// First, initialize the Angular testing environment. -getTestBed().initTestEnvironment( - BrowserDynamicTestingModule, - platformBrowserDynamicTesting() -); -// Then we find all the tests. -const context = require.context('./', true, /\.spec\.ts$/); -// And load the modules. -context.keys().map(context); diff --git a/packages/angular/src/generators/karma-project/files/tsconfig.spec.json b/packages/angular/src/generators/karma-project/files/tsconfig.spec.json index ff32b496a5fba..bcb94c21eebd1 100644 --- a/packages/angular/src/generators/karma-project/files/tsconfig.spec.json +++ b/packages/angular/src/generators/karma-project/files/tsconfig.spec.json @@ -4,6 +4,5 @@ "outDir": "<%= offsetFromRoot %>dist/out-tsc", "types": ["jasmine", "node"] }, - "files": ["src/test.ts"], "include": ["**/*.spec.ts", "**/*.test.ts", "**/*.d.ts"] } diff --git a/packages/angular/src/generators/karma-project/karma-project.spec.ts b/packages/angular/src/generators/karma-project/karma-project.spec.ts index 302542677185b..f7f0d8c8b32b1 100644 --- a/packages/angular/src/generators/karma-project/karma-project.spec.ts +++ b/packages/angular/src/generators/karma-project/karma-project.spec.ts @@ -53,7 +53,6 @@ describe('karmaProject', () => { expect( tree.read('/libs/lib1/tsconfig.spec.json', 'utf-8') ).toMatchSnapshot(); - expect(tree.exists('/libs/lib1/src/test.ts')).toBeTruthy(); expect(tree.exists('karma.conf.js')).toBeTruthy(); }); @@ -114,9 +113,9 @@ describe('karmaProject', () => { expect(workspaceJson.projects.lib1.architect.test).toEqual({ builder: '@angular-devkit/build-angular:karma', options: { - main: 'libs/lib1/src/test.ts', tsConfig: 'libs/lib1/tsconfig.spec.json', karmaConfig: 'libs/lib1/karma.conf.js', + polyfills: ['zone.js', 'zone.js/testing'], }, }); }); @@ -131,17 +130,9 @@ describe('karmaProject', () => { outDir: '../../dist/out-tsc', types: ['jasmine', 'node'], }, - files: ['src/test.ts'], include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'], }); }); - - it('should create a test.ts', async () => { - await karmaProjectGenerator(tree, { project: 'lib1' }); - - const testTs = tree.read('libs/lib1/src/test.ts').toString(); - expect(testTs).toMatchSnapshot(); - }); }); describe('applications', () => { @@ -152,7 +143,6 @@ describe('karmaProject', () => { expect(workspaceJson.projects.app1.architect.test).toEqual({ builder: '@angular-devkit/build-angular:karma', options: { - main: 'apps/app1/src/test.ts', polyfills: ['zone.js', 'zone.js/testing'], tsConfig: 'apps/app1/tsconfig.spec.json', karmaConfig: 'apps/app1/karma.conf.js', @@ -173,7 +163,6 @@ describe('karmaProject', () => { outDir: '../../dist/out-tsc', types: ['jasmine', 'node'], }, - files: ['src/test.ts'], include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'], }); }); @@ -190,7 +179,6 @@ describe('karmaProject', () => { expect(workspaceJson.projects.app1.architect.test).toEqual({ builder: '@angular-devkit/build-angular:karma', options: { - main: 'apps/app1/src/test.ts', polyfills: 'apps/app1/src/polyfills.ts', tsConfig: 'apps/app1/tsconfig.spec.json', karmaConfig: 'apps/app1/karma.conf.js', @@ -216,16 +204,9 @@ describe('karmaProject', () => { outDir: '../../dist/out-tsc', types: ['jasmine', 'node'], }, - files: ['src/test.ts', 'src/polyfills.ts'], + files: ['src/polyfills.ts'], include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'], }); }); - - it('should create a test.ts', async () => { - await karmaProjectGenerator(tree, { project: 'app1' }); - - const testTs = tree.read('apps/app1/src/test.ts').toString(); - expect(testTs).toMatchSnapshot(); - }); }); }); diff --git a/packages/angular/src/generators/karma-project/lib/update-tsconfig.ts b/packages/angular/src/generators/karma-project/lib/update-tsconfig.ts index 4be428577738e..541dff215d768 100644 --- a/packages/angular/src/generators/karma-project/lib/update-tsconfig.ts +++ b/packages/angular/src/generators/karma-project/lib/update-tsconfig.ts @@ -37,13 +37,17 @@ export function updateTsConfigs(tree: Tree, project: string): void { extraFiles = [polyfillsPath]; } - return updateJson( + if (!extraFiles.length) { + return; + } + + updateJson( tree, joinPathFragments(projectConfig.root, 'tsconfig.spec.json'), (json) => { return { ...json, - files: [...json.files, ...extraFiles], + files: [...(json.files ?? []), ...extraFiles], }; } ); diff --git a/packages/angular/src/generators/karma-project/lib/update-workspace-config.ts b/packages/angular/src/generators/karma-project/lib/update-workspace-config.ts index 02372dee3d106..df671656cc077 100644 --- a/packages/angular/src/generators/karma-project/lib/update-workspace-config.ts +++ b/packages/angular/src/generators/karma-project/lib/update-workspace-config.ts @@ -10,9 +10,9 @@ export function updateWorkspaceConfig(tree: Tree, project: string): void { projectConfig.targets.test = { executor: '@angular-devkit/build-angular:karma', options: { - main: joinPathFragments(projectConfig.sourceRoot, 'test.ts'), tsConfig: joinPathFragments(projectConfig.root, 'tsconfig.spec.json'), karmaConfig: joinPathFragments(projectConfig.root, 'karma.conf.js'), + polyfills: ['zone.js', 'zone.js/testing'], }, }; @@ -23,7 +23,7 @@ export function updateWorkspaceConfig(tree: Tree, project: string): void { projectConfig.targets.test.options = { ...projectConfig.targets.test.options, - polyfills: polyfillsPath ?? ['zone.js', 'zone.js/testing'], + polyfills: polyfillsPath ?? projectConfig.targets.test.options.polyfills, styles: [], scripts: [], assets: [], diff --git a/packages/angular/src/generators/library/library.spec.ts b/packages/angular/src/generators/library/library.spec.ts index fac2e3c2359c4..cea1d5ba1c355 100644 --- a/packages/angular/src/generators/library/library.spec.ts +++ b/packages/angular/src/generators/library/library.spec.ts @@ -1131,7 +1131,6 @@ describe('lib', () => { // ASSERT const workspaceJson = readJson(tree, 'workspace.json'); - expect(tree.exists('libs/my-lib/src/test.ts')).toBeTruthy(); expect(tree.exists('libs/my-lib/src/test-setup.ts')).toBeFalsy(); expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeTruthy(); expect(tree.exists('libs/my-lib/karma.conf.js')).toBeTruthy();