From 2f448a804318d4beb62dc9e4ede98e5163747f93 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 1 Jul 2021 15:24:35 -0400 Subject: [PATCH 1/2] chore: run nx migrate latest --- angular.json | 76 +++++++++++++++++++++++++------------------------- jest.preset.js | 18 ------------ nx.json | 12 ++++++-- package.json | 62 ++++++++++++++++++++-------------------- 4 files changed, 79 insertions(+), 89 deletions(-) diff --git a/angular.json b/angular.json index c4792405..212ae232 100644 --- a/angular.json +++ b/angular.json @@ -181,22 +181,22 @@ } } }, - "testing-library": { - "root": "projects/testing-library", - "sourceRoot": "projects/testing-library/src", + "jest-utils": { + "root": "projects/jest-utils", + "sourceRoot": "projects/jest-utils/src", "projectType": "library", "prefix": "lib", "architect": { "build-package": { "builder": "@angular-devkit/build-angular:ng-packagr", "options": { - "tsConfig": "projects/testing-library/tsconfig.lib.json", - "project": "projects/testing-library/ng-package.json" + "tsConfig": "projects/jest-utils/tsconfig.lib.json", + "project": "projects/jest-utils/ng-package.json" }, "configurations": { "production": { - "project": "projects/testing-library/ng-package.json", - "tsConfig": "projects/testing-library/tsconfig.lib.json" + "project": "projects/jest-utils/ng-package.json", + "tsConfig": "projects/jest-utils/tsconfig.lib.json" } } }, @@ -204,11 +204,11 @@ "builder": "@nrwl/linter:eslint", "options": { "lintFilePatterns": [ - "projects/testing-library/**/*.ts", - "projects/testing-library/**/*.html", - "projects/testing-library/src/**/*.html", - "projects/testing-library/src/**/*.html", - "projects/testing-library/src/**/*.html" + "projects/jest-utils/**/*.ts", + "projects/jest-utils/**/*.html", + "projects/jest-utils/src/**/*.html", + "projects/jest-utils/src/**/*.html", + "projects/jest-utils/src/**/*.html" ] } }, @@ -218,13 +218,7 @@ "parallel": false, "commands": [ { - "command": "ng run testing-library:build-package" - }, - { - "command": "npm run build:schematics" - }, - { - "command": "cpy ./README.md ./dist/@testing-library/angular" + "command": "ng run jest-utils:build-package" } ] } @@ -232,29 +226,29 @@ "test": { "builder": "@nrwl/jest:jest", "options": { - "jestConfig": "projects/testing-library/jest.config.js", - "setupFile": "projects/testing-library/test-setup.ts" + "jestConfig": "projects/jest-utils/jest.config.js", + "setupFile": "projects/jest-utils/test-setup.ts" }, - "outputs": ["coverage/projects/testing-library"] + "outputs": ["coverage/projects/jest-utils"] } } }, - "jest-utils": { - "root": "projects/jest-utils", - "sourceRoot": "projects/jest-utils/src", + "testing-library": { + "root": "projects/testing-library", + "sourceRoot": "projects/testing-library/src", "projectType": "library", "prefix": "lib", "architect": { "build-package": { "builder": "@angular-devkit/build-angular:ng-packagr", "options": { - "tsConfig": "projects/jest-utils/tsconfig.lib.json", - "project": "projects/jest-utils/ng-package.json" + "tsConfig": "projects/testing-library/tsconfig.lib.json", + "project": "projects/testing-library/ng-package.json" }, "configurations": { "production": { - "project": "projects/jest-utils/ng-package.json", - "tsConfig": "projects/jest-utils/tsconfig.lib.json" + "project": "projects/testing-library/ng-package.json", + "tsConfig": "projects/testing-library/tsconfig.lib.json" } } }, @@ -262,11 +256,11 @@ "builder": "@nrwl/linter:eslint", "options": { "lintFilePatterns": [ - "projects/jest-utils/**/*.ts", - "projects/jest-utils/**/*.html", - "projects/jest-utils/src/**/*.html", - "projects/jest-utils/src/**/*.html", - "projects/jest-utils/src/**/*.html" + "projects/testing-library/**/*.ts", + "projects/testing-library/**/*.html", + "projects/testing-library/src/**/*.html", + "projects/testing-library/src/**/*.html", + "projects/testing-library/src/**/*.html" ] } }, @@ -276,7 +270,13 @@ "parallel": false, "commands": [ { - "command": "ng run jest-utils:build-package" + "command": "ng run testing-library:build-package" + }, + { + "command": "npm run build:schematics" + }, + { + "command": "cpy ./README.md ./dist/@testing-library/angular" } ] } @@ -284,10 +284,10 @@ "test": { "builder": "@nrwl/jest:jest", "options": { - "jestConfig": "projects/jest-utils/jest.config.js", - "setupFile": "projects/jest-utils/test-setup.ts" + "jestConfig": "projects/testing-library/jest.config.js", + "setupFile": "projects/testing-library/test-setup.ts" }, - "outputs": ["coverage/projects/jest-utils"] + "outputs": ["coverage/projects/testing-library"] } } } diff --git a/jest.preset.js b/jest.preset.js index 21c2ab11..8edf0aa5 100644 --- a/jest.preset.js +++ b/jest.preset.js @@ -1,28 +1,10 @@ const nxPreset = require('@nrwl/jest/preset'); module.exports = { ...nxPreset, - testMatch: ['**/+(*.)+(spec|test).+(ts|js)?(x)'], - transform: { - '^.+\\.(ts|js|html)$': 'ts-jest', - }, - resolver: '@nrwl/jest/plugins/resolver', - moduleFileExtensions: ['ts', 'js', 'html'], - coverageReporters: ['html'], - snapshotSerializers: [ - 'jest-preset-angular/build/serializers/no-ng-attributes', - 'jest-preset-angular/build/serializers/ng-snapshot', - 'jest-preset-angular/build/serializers/html-comment', - ], globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', stringifyContentPathRegex: '\\.(html|svg)$', - astTransformers: { - before: [ - 'jest-preset-angular/build/InlineFilesTransformer', - 'jest-preset-angular/build/StripStylesTransformer', - ], - }, }, }, }; diff --git a/nx.json b/nx.json index bae85516..70db09fe 100644 --- a/nx.json +++ b/nx.json @@ -31,11 +31,19 @@ "example-app-karma": { "tags": [] }, - "testing-library": { + "jest-utils": { "tags": [] }, - "jest-utils": { + "testing-library": { "tags": [] } + }, + "targetDependencies": { + "build": [ + { + "target": "build", + "projects": "dependencies" + } + ] } } diff --git a/package.json b/package.json index fd008e45..a0eac34a 100644 --- a/package.json +++ b/package.json @@ -28,41 +28,41 @@ "prepare": "husky install" }, "dependencies": { - "@angular/animations": "12.0.0", + "@angular/animations": "12.1.1", "@angular/cdk": "12.0.0", - "@angular/common": "12.0.0", - "@angular/compiler": "12.0.0", - "@angular/core": "12.0.0", - "@angular/forms": "12.0.0", + "@angular/common": "12.1.1", + "@angular/compiler": "12.1.1", + "@angular/core": "12.1.1", + "@angular/forms": "12.1.1", "@angular/material": "12.0.0", - "@angular/platform-browser": "12.0.0", - "@angular/platform-browser-dynamic": "12.0.0", - "@angular/router": "12.0.0", - "@ngrx/store": "12.0.0", - "@nrwl/angular": "12.0.3", - "@nrwl/nx-cloud": "11.2.0", + "@angular/platform-browser": "12.1.1", + "@angular/platform-browser-dynamic": "12.1.1", + "@angular/router": "12.1.1", + "@ngrx/store": "12.2.0", + "@nrwl/angular": "12.5.1", + "@nrwl/nx-cloud": "12.2.7", "@testing-library/dom": "7.29.4", "@testing-library/user-event": "^13.1.9", "core-js": "^3.6.5", "rxjs": "^6.5.5", "tslib": "^2.0.0", - "zone.js": "~0.11.4" + "zone.js": "0.11.4" }, "devDependencies": { - "@angular-devkit/build-angular": "12.0.0", - "@angular-eslint/eslint-plugin": "~12.0.0", - "@angular-eslint/eslint-plugin-template": "~12.0.0", - "@angular-eslint/template-parser": "~12.0.0", - "@angular/cli": "12.0.0", - "@angular/compiler-cli": "12.0.0", - "@angular/language-service": "12.0.0", - "@nrwl/cli": "12.0.3", - "@nrwl/eslint-plugin-nx": "12.0.3", - "@nrwl/jest": "12.0.3", - "@nrwl/linter": "12.0.3", - "@nrwl/node": "12.0.3", - "@nrwl/nx-plugin": "12.0.3", - "@nrwl/workspace": "12.3.1", + "@angular-devkit/build-angular": "12.1.0", + "@angular-eslint/eslint-plugin": "12.0.0", + "@angular-eslint/eslint-plugin-template": "12.0.0", + "@angular-eslint/template-parser": "12.0.0", + "@angular/cli": "12.1.0", + "@angular/compiler-cli": "12.1.1", + "@angular/language-service": "12.1.1", + "@nrwl/cli": "12.5.1", + "@nrwl/eslint-plugin-nx": "12.5.1", + "@nrwl/jest": "12.5.1", + "@nrwl/linter": "12.5.1", + "@nrwl/node": "12.5.1", + "@nrwl/nx-plugin": "12.5.1", + "@nrwl/workspace": "12.5.1", "@testing-library/jest-dom": "^5.11.10", "@types/jasmine": "~3.5.0", "@types/jest": "~26.0.3", @@ -79,18 +79,18 @@ "husky": "^6.0.0", "jasmine-core": "~3.6.0", "jasmine-spec-reporter": "~5.0.0", - "jest": "^26.1.0", - "jest-preset-angular": "8.4.0", + "jest": "27.0.3", + "jest-preset-angular": "9.0.3", "karma": "~5.0.0", "karma-chrome-launcher": "~3.1.0", "karma-jasmine": "~4.0.0", "karma-jasmine-html-reporter": "^1.5.0", "lint-staged": "^10.2.11", - "ng-packagr": "12.0.0", - "prettier": "^2.3.0", + "ng-packagr": "12.1.0", + "prettier": "2.3.2", "rimraf": "^3.0.2", "semantic-release": "^17.1.1", - "ts-jest": "26.5.4", + "ts-jest": "27.0.3", "ts-node": "9.1.1", "typescript": "4.2.4" } From a161bef2478afa97bbb348cd5fd046cc8c7c3c61 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Thu, 1 Jul 2021 16:13:33 -0400 Subject: [PATCH 2/2] chore: fix lint errors --- .eslintrc.json | 1 - projects/jest-utils/tests/create-mock.spec.ts | 5 ++-- projects/testing-library/tests/render.spec.ts | 25 ++++++------------- 3 files changed, 11 insertions(+), 20 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 5125274f..cae1bbb4 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -106,7 +106,6 @@ "files": ["*.spec.ts"], "extends": ["plugin:jest/recommended"], "rules": { - "jest/no-done-callback": "off", "jest/expect-expect": "off" } }, diff --git a/projects/jest-utils/tests/create-mock.spec.ts b/projects/jest-utils/tests/create-mock.spec.ts index 8b814fe6..e3258b2f 100644 --- a/projects/jest-utils/tests/create-mock.spec.ts +++ b/projects/jest-utils/tests/create-mock.spec.ts @@ -40,13 +40,14 @@ it('provides a mock service', async () => { }); // eslint-disable-next-line jest/expect-expect -it('is possible to write a mock implementation', async (done) => { +it('is possible to write a mock implementation', async () => { const { getByText } = await render(FixtureComponent, { providers: [provideMock(FixtureService)], }); const service = TestBed.inject(FixtureService) as Mock; - service.print.mockImplementation(() => done()); + service.print = jest.fn(); fireEvent.click(getByText('Print')); + expect(service.print).toHaveBeenCalled(); }); diff --git a/projects/testing-library/tests/render.spec.ts b/projects/testing-library/tests/render.spec.ts index aeac9789..531d5252 100644 --- a/projects/testing-library/tests/render.spec.ts +++ b/projects/testing-library/tests/render.spec.ts @@ -126,28 +126,19 @@ describe('Angular component life-cycle hooks', () => { }); }); -test('Waits for angular app initialization before rendering components', (done) => { - let resolve; +test('Waits for angular app initialization before rendering components', async () => { + const mock = jest.fn(); - const promise = new Promise((res) => { - resolve = res; - }); - - render(FixtureComponent, { + await render(FixtureComponent, { providers: [ { provide: APP_INITIALIZER, - useFactory: () => () => promise, + useFactory: () => mock, multi: true, }, ], - }) - .then(() => { - expect(TestBed.inject(ApplicationInitStatus).done).toEqual(true); - done(); - }) - .catch(done); - - // Wait a bit so the test will fail if render completes without us resolving the promise - setTimeout(resolve, 1000); + }); + + expect(TestBed.inject(ApplicationInitStatus).done).toEqual(true); + expect(mock).toHaveBeenCalled(); });