From 8c062fea31040cb2c618702a6809ba2fb74dc36a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Tue, 3 Dec 2024 14:14:43 +0100 Subject: [PATCH] cleanup(misc): move things around and add extra test coverage --- .../src/generators/utils/add-jest.spec.ts | 54 +++++++++++++++++++ .../angular/src/generators/utils/add-jest.ts | 30 ++++------- .../configuration/configuration.spec.ts | 2 + .../files-angular/src/test-setup.ts__tmpl__ | 2 + 4 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 packages/angular/src/generators/utils/add-jest.spec.ts diff --git a/packages/angular/src/generators/utils/add-jest.spec.ts b/packages/angular/src/generators/utils/add-jest.spec.ts new file mode 100644 index 0000000000000..6066909d6f2ac --- /dev/null +++ b/packages/angular/src/generators/utils/add-jest.spec.ts @@ -0,0 +1,54 @@ +import type { Tree } from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { UnitTestRunner } from '../../utils/test-runners'; +import { addJest } from './add-jest'; +import { generateTestApplication } from './testing'; + +describe('addJest', () => { + let tree: Tree; + + beforeEach(async () => { + tree = createTreeWithEmptyWorkspace(); + await generateTestApplication(tree, { + name: 'app1', + directory: 'app1', + unitTestRunner: UnitTestRunner.None, + skipFormat: true, + }); + }); + + it('generate the test setup file', async () => { + await addJest(tree, { + name: 'app1', + projectRoot: 'app1', + skipPackageJson: false, + strict: false, + }); + + expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(` + "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv(); + " + `); + }); + + it('generate the test setup file with strict', async () => { + await addJest(tree, { + name: 'app1', + projectRoot: 'app1', + skipPackageJson: false, + strict: true, + }); + + expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(` + "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv({ + errorOnUnknownElements: true, + errorOnUnknownProperties: true + }); + " + `); + }); +}); diff --git a/packages/angular/src/generators/utils/add-jest.ts b/packages/angular/src/generators/utils/add-jest.ts index 477829a02cb65..1365ced1d5265 100644 --- a/packages/angular/src/generators/utils/add-jest.ts +++ b/packages/angular/src/generators/utils/add-jest.ts @@ -49,25 +49,17 @@ export async function addJest( 'src', 'test-setup.ts' ); - if (tree.exists(setupFile)) { + if (options.strict && tree.exists(setupFile)) { const contents = tree.read(setupFile, 'utf-8'); - if (options.strict) { - tree.write( - setupFile, - `${contents} -setupZoneTestEnv({ - errorOnUnknownElements: true, - errorOnUnknownProperties: true -}); -` - ); - } else { - tree.write( - setupFile, - `${contents} -setupZoneTestEnv(); -` - ); - } + tree.write( + setupFile, + contents.replace( + 'setupZoneTestEnv();', + `setupZoneTestEnv({ + errorOnUnknownElements: true, + errorOnUnknownProperties: true +});` + ) + ); } } diff --git a/packages/jest/src/generators/configuration/configuration.spec.ts b/packages/jest/src/generators/configuration/configuration.spec.ts index 2972e5d3a9508..449cbc7d42dd7 100644 --- a/packages/jest/src/generators/configuration/configuration.spec.ts +++ b/packages/jest/src/generators/configuration/configuration.spec.ts @@ -56,6 +56,8 @@ describe('jestProject', () => { expect(tree.read('libs/lib1/src/test-setup.ts', 'utf-8')) .toMatchInlineSnapshot(` "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv(); " `); expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy(); diff --git a/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ b/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ index 6e1a56a059718..58c511e08265c 100644 --- a/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ +++ b/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ @@ -1 +1,3 @@ import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + +setupZoneTestEnv();