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:
@ -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();
|
||||
});
|
||||
|
||||
});
|
||||
|
@ -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');
|
||||
|
Reference in New Issue
Block a user