Skip to content

Commit

Permalink
chore: add @testing-library migration (#23)
Browse files Browse the repository at this point in the history
This change was manually released on npm as `@angular-extensions/[email protected]` to not intervene with the current version.
  • Loading branch information
timdeschryver authored Jun 7, 2019
1 parent 662f135 commit 7c8f02a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 0 deletions.
53 changes: 53 additions & 0 deletions projects/testing-library/migrations/5_1_2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Rule, chain, Tree, SchematicContext } from '@angular-devkit/schematics';
import { TslintFixTask } from '@angular-devkit/schematics/tasks';
import * as path from 'path';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';

function createRule(ruleName: string): TslintFixTask {
return new TslintFixTask(
{
rulesDirectory: path.join(__dirname, 'rules'),
rules: {
[ruleName]: [true],
},
},
{
includes: ['**/*.spec.ts', '**/*.test.ts'],
silent: false,
},
);
}

function displayInformation(tree, context: SchematicContext) {
context.logger.info(stripIndents`
@angular-extensions/testing-library has moved to @testing-library/angular.
Learn more about this change here: https://github.com/testing-library/dom-testing-library/issues/260
`);

return tree;
}

function updatePackageJson(host: Tree) {
if (host.exists('package.json')) {
// tslint:disable-next-line: no-non-null-assertion
const sourceText = host.read('package.json')!.toString('utf-8');
const json = JSON.parse(sourceText);

if (json['devDependencies'] && json['devDependencies']['@angular-extensions/testing-library']) {
json['devDependencies']['@testing-library/angular'] = '^6.0.0';
delete json['devDependencies']['@angular-extensions/testing-library'];
host.overwrite('package.json', JSON.stringify(json, null, 2));
}
}

return host;
}

export default function(): Rule {
return (host, context) => {
context.addTask(createRule('no-angular-extensions-import'));

return chain([displayInformation, updatePackageJson])(host, context);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as ts from 'typescript';
import { Replacement, RuleFailure, Rules } from 'tslint';
import { tsquery } from '@phenomnomnominal/tsquery';

const IMPORT_QUERY = `ImportDeclaration StringLiteral[value="@angular-extensions/testing-library"]`;

const FAILURE_MESSAGE =
'Found the library `@angular-extensions/testing-library`, use `@testing-library/angular` instead.';

export class Rule extends Rules.AbstractRule {
public apply(ast: ts.SourceFile): Array<RuleFailure> {
const imports = this.getImports(ast);
return imports;
}

private getImports(ast: ts.SourceFile): Array<RuleFailure> {
return tsquery(ast, IMPORT_QUERY).map(result => {
// replace text between (single) quotes
const replacement = new Replacement(result.getStart() + 1, result.getWidth() - 2, '@testing-library/angular');
const start = result.getStart();
const end = result.getEnd();

return new RuleFailure(ast, start, end, FAILURE_MESSAGE, this.ruleName, replacement);
});
}
}
5 changes: 5 additions & 0 deletions projects/testing-library/migrations/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"version": "4.0.0",
"description": "Align API to *-testing-libraries",
"factory": "./4_0_0"
},
"migration-5.1.2": {
"version": "5.1.2",
"description": "Migrate to @testing-library",
"factory": "./5_1_2"
}
}
}
70 changes: 70 additions & 0 deletions projects/testing-library/tests/migration/5_1_2.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { getSystemPath, normalize, virtualFs } from '@angular-devkit/core';
import { TempScopedNodeJsSyncHost } from '@angular-devkit/core/node/testing';
import { HostTree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';

describe('Migration to version 5.1.2', () => {
const schematicRunner = new SchematicTestRunner('migrations', require.resolve('../../migrations/migration.json'));

test('it renames @angular-extensions to @testing-library (in files)', async () => {
const specPath = normalize('tests/home.spec.ts');

const host = new TempScopedNodeJsSyncHost();
const tree = new UnitTestTree(new HostTree(host));
tree.create('/package.json', JSON.stringify({}));
process.chdir(getSystemPath(host.root));
await host
.write(
specPath,
virtualFs.stringToFileBuffer(stripIndents`
import { render } from '@angular-extensions/testing-library';
import { render } from "@angular-extensions/testing-library";
`),
)
.toPromise();

await schematicRunner.runSchematicAsync('migration-5.1.2', {}, tree).toPromise();
await schematicRunner.engine.executePostTasks().toPromise();

const actual = await host
.read(specPath)
.toPromise()
.then(virtualFs.fileBufferToString);

expect(actual).toBe(stripIndents`
import { render } from '@testing-library/angular';
import { render } from "@testing-library/angular";
`);
});

test('it renames @angular-extensions to @testing-library (in package.json)', async () => {
const packageJson = normalize('/package.json');
const host = new TempScopedNodeJsSyncHost();
const tree = new UnitTestTree(new HostTree(host));

tree.create(
packageJson,
JSON.stringify({
devDependencies: {
'@angular-extensions/testing-library': '🦔',
},
}),
);

await schematicRunner.runSchematicAsync('migration-5.1.2', {}, tree).toPromise();
await schematicRunner.engine.executePostTasks().toPromise();

expect(tree.readContent(packageJson)).toBe(
JSON.stringify(
{
devDependencies: {
'@testing-library/angular': '^6.0.0',
},
},
null,
2,
),
);
});
});

0 comments on commit 7c8f02a

Please sign in to comment.