From 3d16ed8096c3124b563fc8a45e0ed6b88acadb3b Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:50:19 +0200 Subject: [PATCH 1/2] feat: add migration to add DTL as devDependency --- projects/testing-library/package.json | 2 +- .../dtl-as-peer-dependency/index.spec.ts | 44 +++++++++++++++++++ .../dtl-as-peer-dependency/index.ts | 20 +++++++++ .../schematics/migrations/migration.json | 3 -- .../schematics/migrations/migrations.json | 10 +++++ projects/testing-library/test-setup.ts | 4 ++ 6 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts create mode 100644 projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts delete mode 100644 projects/testing-library/schematics/migrations/migration.json create mode 100644 projects/testing-library/schematics/migrations/migrations.json diff --git a/projects/testing-library/package.json b/projects/testing-library/package.json index 4aea8940..2852d027 100644 --- a/projects/testing-library/package.json +++ b/projects/testing-library/package.json @@ -26,7 +26,7 @@ "save": "devDependencies" }, "ng-update": { - "migrations": "./schematics/migrations/migration.json" + "migrations": "./schematics/migrations/migrations.json" }, "peerDependencies": { "@angular/common": ">= 17.0.0", diff --git a/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts b/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts new file mode 100644 index 00000000..a3c0fd1e --- /dev/null +++ b/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts @@ -0,0 +1,44 @@ +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; +import * as path from 'path'; +import { EmptyTree } from '@angular-devkit/schematics'; + +test('adds DTL to devDependencies', async () => { + const tree = await setup({}); + const pkg = tree.readContent('package.json'); + + expect(pkg).toMatchInlineSnapshot(` + "{ + \\"devDependencies\\": { + \\"@testing-library/dom\\": \\"^10.0.0\\" + } + }" + `); +}); + +test('ignores if DTL is already listed as a dev dependency', async () => { + // eslint-disable-next-line @typescript-eslint/naming-convention + const tree = await setup({ devDependencies: { '@testing-library/dom': '^9.0.0' } }); + const pkg = tree.readContent('package.json'); + + expect(pkg).toMatchInlineSnapshot(`"{\\"devDependencies\\":{\\"@testing-library/dom\\":\\"^9.0.0\\"}}"`); +}); + +test('ignores if DTL is already listed as a dependency', async () => { + // eslint-disable-next-line @typescript-eslint/naming-convention + const tree = await setup({ dependencies: { '@testing-library/dom': '^11.0.0' } }); + const pkg = tree.readContent('package.json'); + + expect(pkg).toMatchInlineSnapshot(`"{\\"dependencies\\":{\\"@testing-library/dom\\":\\"^11.0.0\\"}}"`); +}); + +async function setup(packageJson: object) { + const collectionPath = path.join(__dirname, '../migrations.json'); + const schematicRunner = new SchematicTestRunner('schematics', collectionPath); + + const tree = new UnitTestTree(new EmptyTree()); + tree.create('package.json', JSON.stringify(packageJson)); + + await schematicRunner.runSchematic(`atl-add-dtl-as-dev-dependency`, {}, tree); + + return tree; +} diff --git a/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts b/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts new file mode 100644 index 00000000..1c06e2f6 --- /dev/null +++ b/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts @@ -0,0 +1,20 @@ +import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; +import { + addPackageJsonDependency, + getPackageJsonDependency, + NodeDependencyType, +} from '@schematics/angular/utility/dependencies'; + +const dtl = '@testing-library/dom'; + +export default function (): Rule { + return async (tree: Tree, context: SchematicContext) => { + const dtlDep = getPackageJsonDependency(tree, dtl); + if (dtlDep) { + context.logger.info(`Skipping installation of '@testing-library/dom' because it's already installed.`); + } else { + context.logger.info(`Adding '@testing-library/dom' as a peer dependency.`); + addPackageJsonDependency(tree, { name: dtl, type: NodeDependencyType.Dev, overwrite: false, version: '^10.0.0' }); + } + }; +} diff --git a/projects/testing-library/schematics/migrations/migration.json b/projects/testing-library/schematics/migrations/migration.json deleted file mode 100644 index 63001b44..00000000 --- a/projects/testing-library/schematics/migrations/migration.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "schematics": {} -} diff --git a/projects/testing-library/schematics/migrations/migrations.json b/projects/testing-library/schematics/migrations/migrations.json new file mode 100644 index 00000000..711b7ae0 --- /dev/null +++ b/projects/testing-library/schematics/migrations/migrations.json @@ -0,0 +1,10 @@ +{ + "$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json", + "schematics": { + "atl-add-dtl-as-dev-dependency": { + "description": "Add @testing-library/dom as a dev dependency", + "version": "17.0.0-beta.3", + "factory": "./dtl-as-dev-dependency/index" + } + } +} diff --git a/projects/testing-library/test-setup.ts b/projects/testing-library/test-setup.ts index 0da94a0a..600d0857 100644 --- a/projects/testing-library/test-setup.ts +++ b/projects/testing-library/test-setup.ts @@ -1,2 +1,6 @@ import 'jest-preset-angular/setup-jest'; import '@testing-library/jest-dom'; +import { TextEncoder, TextDecoder } from 'util'; + +// eslint-disable-next-line @typescript-eslint/naming-convention +Object.assign(global, { TextDecoder, TextEncoder }); From 2282ddd4dfb6cf01252423907a47fb18f5dd087e Mon Sep 17 00:00:00 2001 From: Tim Deschryver <28659384+timdeschryver@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:56:00 +0200 Subject: [PATCH 2/2] fix: schematics build --- .../index.spec.ts | 0 .../{dtl-as-peer-dependency => dtl-as-dev-dependency}/index.ts | 0 projects/testing-library/tsconfig.lib.prod.json | 2 +- projects/testing-library/tsconfig.schematics.json | 3 ++- 4 files changed, 3 insertions(+), 2 deletions(-) rename projects/testing-library/schematics/migrations/{dtl-as-peer-dependency => dtl-as-dev-dependency}/index.spec.ts (100%) rename projects/testing-library/schematics/migrations/{dtl-as-peer-dependency => dtl-as-dev-dependency}/index.ts (100%) diff --git a/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts b/projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.spec.ts similarity index 100% rename from projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts rename to projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.spec.ts diff --git a/projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts b/projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.ts similarity index 100% rename from projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.ts rename to projects/testing-library/schematics/migrations/dtl-as-dev-dependency/index.ts diff --git a/projects/testing-library/tsconfig.lib.prod.json b/projects/testing-library/tsconfig.lib.prod.json index 1f041c94..752ed5ea 100644 --- a/projects/testing-library/tsconfig.lib.prod.json +++ b/projects/testing-library/tsconfig.lib.prod.json @@ -8,5 +8,5 @@ "angularCompilerOptions": { "compilationMode": "partial" }, - "exclude": ["jest.config.ts"] + "exclude": ["src/test-setup.ts", "**/*.spec.ts", "**/*.test.ts", "jest.config.ts"] } diff --git a/projects/testing-library/tsconfig.schematics.json b/projects/testing-library/tsconfig.schematics.json index 481a34bc..1311558d 100644 --- a/projects/testing-library/tsconfig.schematics.json +++ b/projects/testing-library/tsconfig.schematics.json @@ -13,5 +13,6 @@ "skipLibCheck": true, "sourceMap": false }, - "include": ["schematics/**/*.ts"] + "include": ["schematics/**/*.ts"], + "exclude": ["src/test-setup.ts", "**/*.spec.ts", "**/*.test.ts", "jest.config.ts"] }