test: move away from deprecated testing method (#30747)

Switches all of the tests away from the `runSchematic` method which was deprecated in 8.0 (see https://github.com/angular/angular-cli/pull/14412). Along the same lines as https://github.com/angular/components/pull/16150.

PR Close #30747
This commit is contained in:
crisbeto 2019-05-30 12:43:18 +02:00 committed by Misko Hevery
parent fa354888b1
commit 99c9bcab03
6 changed files with 130 additions and 99 deletions

View File

@ -61,22 +61,38 @@ describe('ng-add schematic', () => {
new SchematicTestRunner('@angular/bazel', require.resolve('../collection.json'));
});
it('throws if package.json is not found', () => {
it('throws if package.json is not found', async() => {
expect(host.files).toContain('/package.json');
host.delete('/package.json');
expect(() => schematicRunner.runSchematic('ng-add', defaultOptions))
.toThrowError('Could not find package.json');
let message = 'No error';
try {
await schematicRunner.runSchematicAsync('ng-add', defaultOptions).toPromise();
} catch (e) {
message = e.message;
}
expect(message).toBe('Could not find package.json');
});
it('throws if angular.json is not found', () => {
it('throws if angular.json is not found', async() => {
expect(host.files).toContain('/angular.json');
host.delete('/angular.json');
expect(() => schematicRunner.runSchematic('ng-add', defaultOptions, host))
.toThrowError('Could not find angular.json');
let message = 'No error';
try {
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
} catch (e) {
message = e.message;
}
expect(message).toBe('Could not find angular.json');
});
it('should add @angular/bazel to package.json dependencies', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should add @angular/bazel to package.json dependencies', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).toContain('/package.json');
const content = host.readContent('/package.json');
@ -91,8 +107,8 @@ describe('ng-add schematic', () => {
expect(json.dependencies[core]).toBe(json.devDependencies[bazel]);
});
it('should add @bazel/* dev dependencies', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should add @bazel/* dev dependencies', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const content = host.readContent('/package.json');
const json = JSON.parse(content);
const devDeps = Object.keys(json.devDependencies);
@ -101,12 +117,12 @@ describe('ng-add schematic', () => {
expect(devDeps).toContain('@bazel/karma');
});
it('should replace an existing dev dependency', () => {
it('should replace an existing dev dependency', async() => {
expect(host.files).toContain('/package.json');
const packageJson = JSON.parse(host.readContent('/package.json'));
packageJson.devDependencies['@angular/bazel'] = '4.2.42';
host.overwrite('/package.json', JSON.stringify(packageJson));
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const content = host.readContent('/package.json');
// It is possible that a dep gets added twice if the package already exists.
expect(content.match(/@angular\/bazel/g) !.length).toEqual(1);
@ -114,29 +130,29 @@ describe('ng-add schematic', () => {
expect(json.devDependencies['@angular/bazel']).toBe('1.2.3');
});
it('should remove an existing dependency', () => {
it('should remove an existing dependency', async() => {
expect(host.files).toContain('/package.json');
const packageJson = JSON.parse(host.readContent('/package.json'));
packageJson.dependencies['@angular/bazel'] = '4.2.42';
expect(Object.keys(packageJson.dependencies)).toContain('@angular/bazel');
host.overwrite('/package.json', JSON.stringify(packageJson));
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const content = host.readContent('/package.json');
const json = JSON.parse(content);
expect(Object.keys(json.dependencies)).not.toContain('@angular/bazel');
expect(json.devDependencies['@angular/bazel']).toBe('1.2.3');
});
it('should not create Bazel workspace file', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should not create Bazel workspace file', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).not.toContain('/WORKSPACE');
expect(files).not.toContain('/BUILD.bazel');
});
it('should produce main.dev.ts and main.prod.ts for AOT', () => {
it('should produce main.dev.ts and main.prod.ts for AOT', async() => {
host.create('/src/main.ts', 'generated by CLI');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
// main.dev.ts and main.prod.ts are used by Bazel for AOT
expect(files).toContain('/src/main.dev.ts');
@ -146,9 +162,9 @@ describe('ng-add schematic', () => {
expect(files).toContain('/src/main.ts');
});
it('should not overwrite index.html with script tags', () => {
it('should not overwrite index.html with script tags', async() => {
host.create('/src/index.html', '<html>Hello World</html>');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).toContain('/src/index.html');
const content = host.readContent('/src/index.html');
@ -156,34 +172,34 @@ describe('ng-add schematic', () => {
expect(content).not.toMatch('<script src="/bundle.min.js"></script>');
});
it('should generate main.dev.ts and main.prod.ts', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should generate main.dev.ts and main.prod.ts', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).toContain('/src/main.dev.ts');
expect(files).toContain('/src/main.prod.ts');
});
it('should overwrite .gitignore for bazel-out directory', () => {
it('should overwrite .gitignore for bazel-out directory', async() => {
host.create('.gitignore', '\n# compiled output\n');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).toContain('/.gitignore');
const content = host.readContent('/.gitignore');
expect(content).toMatch('\n# compiled output\n/bazel-out\n');
});
it('should create a backup for original angular.json', () => {
it('should create a backup for original angular.json', async() => {
expect(host.files).toContain('/angular.json');
const original = host.readContent('/angular.json');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.files).toContain('/angular.json.bak');
const content = host.readContent('/angular.json.bak');
expect(content.startsWith('// This is a backup file')).toBe(true);
expect(content).toMatch(original);
});
it('should update angular.json to use Bazel builder', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should update angular.json to use Bazel builder', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
const {files} = host;
expect(files).toContain('/angular.json');
const content = host.readContent('/angular.json');
@ -203,27 +219,27 @@ describe('ng-add schematic', () => {
expect(lint.builder).toBe('@angular-devkit/build-angular:tslint');
});
it('should get defaultProject if name is not provided', () => {
it('should get defaultProject if name is not provided', async() => {
const options = {};
host = schematicRunner.runSchematic('ng-add', options, host);
host = await schematicRunner.runSchematicAsync('ng-add', options, host).toPromise();
const content = host.readContent('/angular.json');
const json = JSON.parse(content);
const builder = json.projects.demo.architect.build.builder;
expect(builder).toBe('@angular/bazel:build');
});
it('should create a backup for original tsconfig.json', () => {
it('should create a backup for original tsconfig.json', async() => {
expect(host.files).toContain('/tsconfig.json');
const original = host.readContent('/tsconfig.json');
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.files).toContain('/tsconfig.json.bak');
const content = host.readContent('/tsconfig.json.bak');
expect(content.startsWith('// This is a backup file')).toBe(true);
expect(content).toMatch(original);
});
it('should remove Bazel-controlled options from tsconfig.json', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should remove Bazel-controlled options from tsconfig.json', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.files).toContain('/tsconfig.json');
const content = host.readContent('/tsconfig.json');
expect(() => JSON.parse(content)).not.toThrow();
@ -251,7 +267,7 @@ describe('ng-add schematic', () => {
['~7.0.1', false],
];
for (const [version, upgrade] of cases) {
it(`should ${upgrade ? '' : 'not '}upgrade v${version}')`, () => {
it(`should ${upgrade ? '' : 'not '}upgrade v${version}')`, async() => {
host.overwrite('package.json', JSON.stringify({
name: 'demo',
dependencies: {
@ -262,7 +278,7 @@ describe('ng-add schematic', () => {
'typescript': '3.2.2',
},
}));
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.files).toContain('/package.json');
const content = host.readContent('/package.json');
const json = JSON.parse(content);
@ -275,15 +291,15 @@ describe('ng-add schematic', () => {
}
});
it('should add a postinstall step to package.json', () => {
host = schematicRunner.runSchematic('ng-add', defaultOptions, host);
it('should add a postinstall step to package.json', async() => {
host = await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
expect(host.files).toContain('/package.json');
const content = host.readContent('/package.json');
const json = JSON.parse(content);
expect(json.scripts.postinstall).toBe('ngc -p ./angular-metadata.tsconfig.json');
});
it('should work when run on a minimal project (without test and e2e targets)', () => {
it('should work when run on a minimal project (without test and e2e targets)', async() => {
host.overwrite('angular.json', JSON.stringify({
projects: {
'demo': {
@ -298,7 +314,15 @@ describe('ng-add schematic', () => {
},
}));
expect(() => schematicRunner.runSchematic('ng-add', defaultOptions, host)).not.toThrowError();
let error: Error|null = null;
try {
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, host).toPromise();
} catch (e) {
error = e;
}
expect(error).toBeNull();
});
});

View File

@ -16,18 +16,18 @@ describe('ng-new schematic', () => {
version: '7.0.0',
};
it('should call external @schematics/angular', () => {
it('should call external @schematics/angular', async() => {
const options = {...defaultOptions};
const host = schematicRunner.runSchematic('ng-new', options);
const host = await schematicRunner.runSchematicAsync('ng-new', options).toPromise();
const {files} = host;
// External schematic should produce workspace file angular.json
expect(files).toContain('/demo/angular.json');
expect(files).toContain('/demo/package.json');
});
it('should call ng-add to generate additional files needed by Bazel', () => {
it('should call ng-add to generate additional files needed by Bazel', async() => {
const options = {...defaultOptions};
const host = schematicRunner.runSchematic('ng-new', options);
const host = await schematicRunner.runSchematicAsync('ng-new', options).toPromise();
const {files} = host;
expect(files).toContain('/demo/src/main.dev.ts');
expect(files).toContain('/demo/src/main.prod.ts');

View File

@ -46,7 +46,7 @@ describe('injectable pipe migration', () => {
shx.rm('-r', tmpDirPath);
});
it('should add @Injectable to pipes that do not have it', () => {
it('should add @Injectable to pipes that do not have it', async() => {
writeFile('/index.ts', `
import { Pipe } from '@angular/core';
@ -55,12 +55,12 @@ describe('injectable pipe migration', () => {
}
`);
runMigration();
await runMigration();
expect(tree.readContent('/index.ts'))
.toMatch(/@Injectable\(\)\s+@Pipe\(\{ name: 'myPipe' \}\)\s+export class MyPipe/);
});
it('should add an import for Injectable to the @angular/core import declaration', () => {
it('should add an import for Injectable to the @angular/core import declaration', async() => {
writeFile('/index.ts', `
import { Pipe } from '@angular/core';
@ -69,14 +69,14 @@ describe('injectable pipe migration', () => {
}
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
expect(content).toContain('import { Pipe, Injectable } from \'@angular/core\'');
expect((content.match(/import/g) || []).length).toBe(1, 'Expected only one import statement');
});
it('should not add an import for Injectable if it is imported already', () => {
it('should not add an import for Injectable if it is imported already', async() => {
writeFile('/index.ts', `
import { Pipe, Injectable, NgModule } from '@angular/core';
@ -85,12 +85,12 @@ describe('injectable pipe migration', () => {
}
`);
runMigration();
await runMigration();
expect(tree.readContent('/index.ts'))
.toContain('import { Pipe, Injectable, NgModule } from \'@angular/core\'');
});
it('should do nothing if the pipe is marked as injectable already', () => {
it('should do nothing if the pipe is marked as injectable already', async() => {
const source = `
import { Injectable, Pipe } from '@angular/core';
@ -101,11 +101,11 @@ describe('injectable pipe migration', () => {
`;
writeFile('/index.ts', source);
runMigration();
await runMigration();
expect(tree.readContent('/index.ts')).toBe(source);
});
it('should not add @Injectable if @Pipe was not imported from @angular/core', () => {
it('should not add @Injectable if @Pipe was not imported from @angular/core', async() => {
const source = `
import { Pipe } from '@not-angular/core';
@ -115,7 +115,7 @@ describe('injectable pipe migration', () => {
`;
writeFile('/index.ts', source);
runMigration();
await runMigration();
expect(tree.readContent('/index.ts')).toBe(source);
});
@ -123,5 +123,7 @@ describe('injectable pipe migration', () => {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}
function runMigration() { runner.runSchematic('migration-injectable-pipe', {}, tree); }
function runMigration() {
runner.runSchematicAsync('migration-injectable-pipe', {}, tree).toPromise();
}
});

View File

@ -47,12 +47,12 @@ describe('move-document migration', () => {
});
describe('move-document', () => {
it('should properly apply import replacement', () => {
it('should properly apply import replacement', async() => {
writeFile('/index.ts', `
import {DOCUMENT} from '@angular/platform-browser';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
@ -60,7 +60,7 @@ describe('move-document migration', () => {
expect(content).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`);
});
it('should properly apply import replacement with existing import', () => {
it('should properly apply import replacement with existing import', async() => {
writeFile('/index.ts', `
import {DOCUMENT} from '@angular/platform-browser';
import {someImport} from '@angular/common';
@ -71,7 +71,7 @@ describe('move-document migration', () => {
import {DOCUMENT} from '@angular/platform-browser';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
const contentReverse = tree.readContent('/reverse.ts');
@ -83,7 +83,7 @@ describe('move-document migration', () => {
expect(contentReverse).not.toContain(`import {DOCUMENT} from '@angular/platform-browser';`);
});
it('should properly apply import replacement with existing import w/ comments', () => {
it('should properly apply import replacement with existing import w/ comments', async() => {
writeFile('/index.ts', `
/**
* this is a comment
@ -92,7 +92,7 @@ describe('move-document migration', () => {
import {DOCUMENT} from '@angular/platform-browser';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
@ -102,14 +102,14 @@ describe('move-document migration', () => {
expect(content).toMatch(/.*this is a comment.*/);
});
it('should properly apply import replacement with existing and redundant imports', () => {
it('should properly apply import replacement with existing and redundant imports', async() => {
writeFile('/index.ts', `
import {DOCUMENT} from '@angular/platform-browser';
import {anotherImport} from '@angular/platform-browser-dynamic';
import {someImport} from '@angular/common';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
@ -118,13 +118,13 @@ describe('move-document migration', () => {
});
it('should properly apply import replacement with existing import and leave original import',
() => {
async() => {
writeFile('/index.ts', `
import {DOCUMENT, anotherImport} from '@angular/platform-browser';
import {someImport} from '@angular/common';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
@ -132,13 +132,13 @@ describe('move-document migration', () => {
expect(content).toContain(`import { anotherImport } from '@angular/platform-browser';`);
});
it('should properly apply import replacement with existing import and alias', () => {
it('should properly apply import replacement with existing import and alias', async() => {
writeFile('/index.ts', `
import {DOCUMENT as doc, anotherImport} from '@angular/platform-browser';
import {someImport} from '@angular/common';
`);
runMigration();
await runMigration();
const content = tree.readContent('/index.ts');
@ -151,5 +151,7 @@ describe('move-document migration', () => {
host.sync.write(normalize(filePath), virtualFs.stringToFileBuffer(contents));
}
function runMigration() { runner.runSchematic('migration-v8-move-document', {}, tree); }
function runMigration() {
runner.runSchematicAsync('migration-v8-move-document', {}, tree).toPromise();
}
});

View File

@ -59,10 +59,10 @@ describe('template variable assignment migration', () => {
}
function runMigration() {
runner.runSchematic('migration-v8-template-local-variables', {}, tree);
return runner.runSchematicAsync('migration-v8-template-local-variables', {}, tree).toPromise();
}
it('should warn for two-way data binding variable assignment', () => {
it('should warn for two-way data binding variable assignment', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -72,13 +72,13 @@ describe('template variable assignment migration', () => {
export class MyComp {}
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(1);
expect(warnOutput[0]).toMatch(/^⮑ {3}index.ts@5:69: Found assignment/);
});
it('should warn for two-way data binding assigning to "as" variable', () => {
it('should warn for two-way data binding assigning to "as" variable', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -94,13 +94,13 @@ describe('template variable assignment migration', () => {
</div>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(1);
expect(warnOutput).toMatch(/^⮑ {3}tmpl.html@3:31: Found assignment/);
});
it('should warn for bound event assignments to "as" variable', () => {
it('should warn for bound event assignments to "as" variable', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -117,14 +117,14 @@ describe('template variable assignment migration', () => {
</div>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(2);
expect(warnOutput[0]).toMatch(/^⮑ {3}sub_dir\/tmpl.html@3:25: Found assignment/);
expect(warnOutput[1]).toMatch(/^⮑ {3}sub_dir\/tmpl.html@4:25: Found assignment/);
});
it('should warn for bound event assignments to template "let" variables', () => {
it('should warn for bound event assignments to template "let" variables', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -141,14 +141,14 @@ describe('template variable assignment migration', () => {
</ng-template>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(2);
expect(warnOutput[0]).toMatch(/^⮑ {3}sub_dir\/tmpl.html@3:25: Found assignment/);
expect(warnOutput[1]).toMatch(/^⮑ {3}sub_dir\/tmpl.html@4:25: Found assignment/);
});
it('should not warn for bound event assignments to component property', () => {
it('should not warn for bound event assignments to component property', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -160,13 +160,14 @@ describe('template variable assignment migration', () => {
writeFile('/sub_dir/tmpl.html', `<button (click)="myProp = true"></button>`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(0);
});
it('should not warn for bound event assignments to template variable object property', () => {
writeFile('/index.ts', `
it('should not warn for bound event assignments to template variable object property',
async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@Component({
@ -175,17 +176,17 @@ describe('template variable assignment migration', () => {
export class MyComp {}
`);
writeFile('/sub_dir/tmpl.html', `
writeFile('/sub_dir/tmpl.html', `
<button *ngFor="let element of list" (click)="element.value = null">Reset</button>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(0);
});
expect(warnOutput.length).toBe(0);
});
it('should not warn for property writes with template variable name but different receiver',
() => {
async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -205,13 +206,14 @@ describe('template variable assignment migration', () => {
</button>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(0);
});
it('should not warn for property writes with template variable name but different scope', () => {
writeFile('/index.ts', `
it('should not warn for property writes with template variable name but different scope',
async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@Component({
@ -222,18 +224,18 @@ describe('template variable assignment migration', () => {
}
`);
writeFile('/sub_dir/tmpl.html', `
writeFile('/sub_dir/tmpl.html', `
<button *ngFor="let element of list">{{element}}</button>
<button (click)="element = null"></button>
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(0);
});
expect(warnOutput.length).toBe(0);
});
it('should not throw an error if a detected template fails parsing', () => {
it('should not throw an error if a detected template fails parsing', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -245,12 +247,12 @@ describe('template variable assignment migration', () => {
writeFile('/sub_dir/tmpl.html', `<x (click)="<invalid-syntax>"></x>`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(0);
});
it('should be able to report multiple templates within the same source file', () => {
it('should be able to report multiple templates within the same source file', async() => {
writeFile('/index.ts', `
import {Component} from '@angular/core';
@ -265,7 +267,7 @@ describe('template variable assignment migration', () => {
export class MyComp2 {}
`);
runMigration();
await runMigration();
expect(warnOutput.length).toBe(2);
expect(warnOutput[0]).toMatch(/^⮑ {3}index.ts@5:56: Found assignment/);

View File

@ -48,8 +48,9 @@ describe('Elements Schematics', () => {
.subscribe((tree: UnitTestTree) => appTree = tree, done.fail, done);
});
it('should run the ng-add schematic', () => {
const tree = schematicRunner.runSchematic('ng-add', defaultOptions, appTree);
it('should run the ng-add schematic', async() => {
const tree =
await schematicRunner.runSchematicAsync('ng-add', defaultOptions, appTree).toPromise();
const configText = tree.readContent('/angular.json');
const config = JSON.parse(configText);
const scripts = config.projects.elements.architect.build.options.scripts;