-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add @testing-library migration (#23)
This change was manually released on npm as `@angular-extensions/[email protected]` to not intervene with the current version.
- Loading branch information
1 parent
662f135
commit 7c8f02a
Showing
4 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
} |
26 changes: 26 additions & 0 deletions
26
projects/testing-library/migrations/5_1_2/rules/noAngularExtensionsImportRule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
); | ||
}); | ||
}); |