Skip to content

Commit f7f7017

Browse files
committed
feat: add migration to add DTL as devDependency
1 parent 4d02fb6 commit f7f7017

File tree

6 files changed

+79
-4
lines changed

6 files changed

+79
-4
lines changed

projects/testing-library/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"save": "devDependencies"
2727
},
2828
"ng-update": {
29-
"migrations": "./schematics/migrations/migration.json"
29+
"migrations": "./schematics/migrations/migrations.json"
3030
},
3131
"peerDependencies": {
3232
"@angular/common": ">= 17.0.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
2+
import * as path from 'path';
3+
import { EmptyTree } from '@angular-devkit/schematics';
4+
5+
test('adds DTL to peerDependencies', async () => {
6+
const tree = await setup({});
7+
const pkg = tree.readContent('package.json');
8+
9+
expect(pkg).toMatchInlineSnapshot(`
10+
"{
11+
\\"devDependencies\\": {
12+
\\"@testing-library/dom\\": \\"^10.0.0\\"
13+
}
14+
}"
15+
`);
16+
});
17+
18+
test('ignores if DTL is already listed as a dev dependency', async () => {
19+
// eslint-disable-next-line @typescript-eslint/naming-convention
20+
const tree = await setup({ devDependencies: { '@testing-library/dom': '^9.0.0' } });
21+
const pkg = tree.readContent('package.json');
22+
23+
expect(pkg).toMatchInlineSnapshot(`"{\\"devDependencies\\":{\\"@testing-library/dom\\":\\"^9.0.0\\"}}"`);
24+
});
25+
26+
test('ignores if DTL is already listed as a dependency', async () => {
27+
// eslint-disable-next-line @typescript-eslint/naming-convention
28+
const tree = await setup({ dependencies: { '@testing-library/dom': '^11.0.0' } });
29+
const pkg = tree.readContent('package.json');
30+
31+
expect(pkg).toMatchInlineSnapshot(`"{\\"dependencies\\":{\\"@testing-library/dom\\":\\"^11.0.0\\"}}"`);
32+
});
33+
34+
async function setup(packageJson: object) {
35+
const collectionPath = path.join(__dirname, '../migrations.json');
36+
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);
37+
38+
const tree = new UnitTestTree(new EmptyTree());
39+
tree.create('package.json', JSON.stringify(packageJson));
40+
41+
await schematicRunner.runSchematic(`atl-add-dtl-as-peer-dependency`, {}, tree);
42+
43+
return tree;
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
2+
import {
3+
addPackageJsonDependency,
4+
getPackageJsonDependency,
5+
NodeDependencyType,
6+
} from '@schematics/angular/utility/dependencies';
7+
8+
const dtl = '@testing-library/dom';
9+
10+
export default function (): Rule {
11+
return async (tree: Tree, context: SchematicContext) => {
12+
const dtlDep = getPackageJsonDependency(tree, dtl);
13+
if (dtlDep) {
14+
context.logger.info(`Skipping installation of '@testing-library/dom' because it's already installed.`);
15+
} else {
16+
context.logger.info(`Adding '@testing-library/dom' as a peer dependency.`);
17+
addPackageJsonDependency(tree, { name: dtl, type: NodeDependencyType.Dev, overwrite: false, version: '^10.0.0' });
18+
}
19+
};
20+
}

projects/testing-library/schematics/migrations/migration.json

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
3+
"schematics": {
4+
"atl-add-dtl-as-peer-dependency": {
5+
"description": "Add @testing-library/dom as a peer dependency",
6+
"version": "17.0.0-beta.3",
7+
"factory": "./dtl-as-peer-dependency/index"
8+
}
9+
}
10+
}
+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
import 'jest-preset-angular/setup-jest';
22
import '@testing-library/jest-dom';
3+
import { TextEncoder, TextDecoder } from 'util';
4+
5+
// eslint-disable-next-line @typescript-eslint/naming-convention
6+
Object.assign(global, { TextDecoder, TextEncoder });

0 commit comments

Comments
 (0)