Skip to content

Commit

Permalink
feat: add migration to add DTL as devDependency (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver authored Jun 21, 2024
1 parent 67adc5c commit ef10fbf
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 6 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 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;
}
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-dev-dependency": {
"description": "Add @testing-library/dom as a dev dependency",
"version": "17.0.0-beta.3",
"factory": "./dtl-as-dev-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 });
2 changes: 1 addition & 1 deletion projects/testing-library/tsconfig.lib.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"angularCompilerOptions": {
"compilationMode": "partial"
},
"exclude": ["jest.config.ts"]
"exclude": ["src/test-setup.ts", "**/*.spec.ts", "**/*.test.ts", "jest.config.ts"]
}
3 changes: 2 additions & 1 deletion projects/testing-library/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}

0 comments on commit ef10fbf

Please sign in to comment.