Skip to content

Instantly share code, notes, and snippets.

@mugan86
Created December 4, 2022 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugan86/f865cb4c5da4fe968b0e0d2a5dcb4f81 to your computer and use it in GitHub Desktop.
Save mugan86/f865cb4c5da4fe968b0e0d2a5dcb4f81 to your computer and use it in GitHub Desktop.
Basic testing with Angular Schematics
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';
const collectionPath = path.join(__dirname, '../collection.json');
describe('hello', () => {
it('Execute with default args', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner
.runSchematicAsync('hello', {}, Tree.empty())
.toPromise();
expect(tree.files.length).toEqual(1);
expect(tree.files[0]).toEqual('/hello-helloanartz--1.js');
});
it('Execute with default age and name value input', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const treeNameAnartz = await runner
.runSchematicAsync('hello', {
name: 'anartz'
}, Tree.empty())
.toPromise();
expect(treeNameAnartz.files.length).toEqual(1);
expect(treeNameAnartz.files[0]).toEqual('/hello-anartz--1.js');
const treeNameMikel = await runner
.runSchematicAsync('hello', {
name: 'Mikel'
}, Tree.empty())
.toPromise();
expect(treeNameMikel.files.length).toEqual(1);
expect(treeNameMikel.files[0]).toEqual('/hello-mikel--1.js');
});
it('Execute with default name and age value input', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const treeFiftySix = await runner
.runSchematicAsync('hello', {
age: '56'
}, Tree.empty())
.toPromise();
expect(treeFiftySix.files.length).toEqual(1);
expect(treeFiftySix.files[0]).toEqual('/hello-helloanartz-56.js');
const treeNameMikel = await runner
.runSchematicAsync('hello', {
name: 'Mikel'
}, Tree.empty())
.toPromise();
expect(treeNameMikel.files.length).toEqual(1);
expect(treeNameMikel.files[0]).toEqual('/hello-mikel--1.js');
});
it('Execute and check content is correct', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);
const tree = await runner
.runSchematicAsync('hello', {}, Tree.empty())
.toPromise();
console.log(tree.files);
expect(tree.readContent(tree.files[0]))
.toContain(
"console.log('¡Hola HelloAnartz!Veo que tienes -1 años ;)')"
);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment