Skip to content

Commit

Permalink
feat: add migration to add DTL as devDependency
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Jun 21, 2024
1 parent 4d02fb6 commit f7f7017
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion projects/testing-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"save": "devDependencies"
},
"ng-update": {
"migrations": "./schematics/migrations/migration.json"
"migrations": "./schematics/migrations/migrations.json"
},
"peerDependencies": {
"@angular/common": ">= 17.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -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 peerDependencies', async () => {
const tree = await setup({});
const pkg = tree.readContent('package.json');

expect(pkg).toMatchInlineSnapshot(`

Check failure on line 9 in projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts

View workflow job for this annotation

GitHub Actions / build_test_release (20, ubuntu-latest)

Property 'toMatchInlineSnapshot' does not exist on type 'ArrayLikeMatchers<string>'.
"{
\\"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\\"}}"`);

Check failure on line 23 in projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts

View workflow job for this annotation

GitHub Actions / build_test_release (20, ubuntu-latest)

Property 'toMatchInlineSnapshot' does not exist on type 'ArrayLikeMatchers<string>'.
});

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\\"}}"`);

Check failure on line 31 in projects/testing-library/schematics/migrations/dtl-as-peer-dependency/index.spec.ts

View workflow job for this annotation

GitHub Actions / build_test_release (20, ubuntu-latest)

Property 'toMatchInlineSnapshot' does not exist on type 'ArrayLikeMatchers<string>'.
});

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-peer-dependency`, {}, tree);

return tree;
}
Original file line number Diff line number Diff line change
@@ -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' });
}
};
}
3 changes: 0 additions & 3 deletions projects/testing-library/schematics/migrations/migration.json

This file was deleted.

10 changes: 10 additions & 0 deletions projects/testing-library/schematics/migrations/migrations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"atl-add-dtl-as-peer-dependency": {
"description": "Add @testing-library/dom as a peer dependency",
"version": "17.0.0-beta.3",
"factory": "./dtl-as-peer-dependency/index"
}
}
}
4 changes: 4 additions & 0 deletions projects/testing-library/test-setup.ts
Original file line number Diff line number Diff line change
@@ -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 });

0 comments on commit f7f7017

Please sign in to comment.