2022-11-09 13:52:10 +00:00
|
|
|
import expect from 'expect';
|
|
|
|
import sinon from 'sinon';
|
|
|
|
import https from 'https';
|
|
|
|
import {join} from 'path';
|
|
|
|
import {
|
|
|
|
SchematicTestRunner,
|
|
|
|
UnitTestTree,
|
|
|
|
} from '@angular-devkit/schematics/testing/schematic-test-runner';
|
|
|
|
import {JsonObject} from '@angular-devkit/core';
|
|
|
|
|
|
|
|
const WORKSPACE_OPTIONS = {
|
|
|
|
name: 'workspace',
|
|
|
|
newProjectRoot: 'projects',
|
|
|
|
version: '14.0.0',
|
|
|
|
};
|
|
|
|
|
|
|
|
const APPLICATION_OPTIONS = {
|
|
|
|
name: 'sandbox',
|
|
|
|
};
|
|
|
|
|
|
|
|
function getProjectFile(file: string): string {
|
|
|
|
return `/${WORKSPACE_OPTIONS.newProjectRoot}/${APPLICATION_OPTIONS.name}/${file}`;
|
|
|
|
}
|
|
|
|
|
2022-11-23 12:10:03 +00:00
|
|
|
function getAngularJsonScripts(
|
|
|
|
tree: UnitTestTree,
|
|
|
|
isDefault = true
|
|
|
|
): {
|
|
|
|
builder: string;
|
|
|
|
configurations: Record<string, any>;
|
|
|
|
options: Record<string, any>;
|
|
|
|
} {
|
2022-11-22 13:02:24 +00:00
|
|
|
const angularJson = tree.readJson('angular.json') as any;
|
2022-11-23 12:10:03 +00:00
|
|
|
const e2eScript = isDefault ? 'e2e' : 'puppeteer';
|
|
|
|
return angularJson['projects']?.[APPLICATION_OPTIONS.name]?.['architect'][
|
|
|
|
e2eScript
|
|
|
|
];
|
2022-11-22 13:02:24 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 13:52:10 +00:00
|
|
|
function getPackageJson(tree: UnitTestTree): {
|
|
|
|
scripts: Record<string, string>;
|
|
|
|
devDependencies: string[];
|
|
|
|
} {
|
|
|
|
const packageJson = tree.readJson('package.json') as JsonObject;
|
|
|
|
return {
|
|
|
|
scripts: packageJson['scripts'] as any,
|
|
|
|
devDependencies: Object.keys(
|
|
|
|
packageJson['devDependencies'] as Record<string, string>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function buildTestingTree(userOptions?: Record<string, any>) {
|
|
|
|
const runner = new SchematicTestRunner(
|
|
|
|
'schematics',
|
|
|
|
join(__dirname, '../../lib/schematics/collection.json')
|
|
|
|
);
|
|
|
|
const options = {
|
2022-11-23 12:10:03 +00:00
|
|
|
isDefaultTester: true,
|
2022-11-09 13:52:10 +00:00
|
|
|
exportConfig: false,
|
|
|
|
testingFramework: 'jasmine',
|
|
|
|
...userOptions,
|
|
|
|
};
|
|
|
|
let workingTree: UnitTestTree;
|
|
|
|
|
|
|
|
// Build workspace
|
|
|
|
workingTree = await runner
|
|
|
|
.runExternalSchematicAsync(
|
|
|
|
'@schematics/angular',
|
|
|
|
'workspace',
|
|
|
|
WORKSPACE_OPTIONS
|
|
|
|
)
|
|
|
|
.toPromise();
|
|
|
|
// Build dummy application
|
|
|
|
workingTree = await runner
|
|
|
|
.runExternalSchematicAsync(
|
|
|
|
'@schematics/angular',
|
|
|
|
'application',
|
|
|
|
APPLICATION_OPTIONS,
|
|
|
|
workingTree
|
|
|
|
)
|
|
|
|
.toPromise();
|
|
|
|
|
|
|
|
return await runner
|
|
|
|
.runSchematicAsync('ng-add', options, workingTree)
|
|
|
|
.toPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('@puppeteer/ng-schematics: ng-add', () => {
|
|
|
|
// Stop outgoing Request for version fetching
|
|
|
|
before(() => {
|
|
|
|
const httpsGetStub = sinon.stub(https, 'get');
|
|
|
|
httpsGetStub.returns({
|
|
|
|
on: (_: any, callback: () => void) => {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
} as any);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(() => {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create base files and update to "package.json"', async () => {
|
|
|
|
const tree = await buildTestingTree();
|
2022-11-22 13:02:24 +00:00
|
|
|
const {devDependencies, scripts} = getPackageJson(tree);
|
2022-11-23 12:10:03 +00:00
|
|
|
const {builder, configurations} = getAngularJsonScripts(tree);
|
2022-11-09 13:52:10 +00:00
|
|
|
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/tsconfig.json'));
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/tests/app.e2e.ts'));
|
|
|
|
expect(devDependencies).toContain('puppeteer');
|
2022-11-22 13:02:24 +00:00
|
|
|
expect(scripts['e2e']).toBe('ng e2e');
|
2022-11-23 12:10:03 +00:00
|
|
|
expect(builder).toBe('@puppeteer/ng-schematics:puppeteer');
|
|
|
|
expect(configurations).toEqual({
|
|
|
|
production: {
|
|
|
|
devServerTarget: 'sandbox:serve:production',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should update create proper "ng" command for non default tester', async () => {
|
|
|
|
const tree = await buildTestingTree({
|
|
|
|
isDefaultTester: false,
|
|
|
|
});
|
|
|
|
const {scripts} = getPackageJson(tree);
|
|
|
|
const {builder} = getAngularJsonScripts(tree, false);
|
|
|
|
|
|
|
|
expect(scripts['puppeteer']).toBe('ng run sandbox:puppeteer');
|
|
|
|
expect(builder).toBe('@puppeteer/ng-schematics:puppeteer');
|
2022-11-09 13:52:10 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create Puppeteer config', async () => {
|
|
|
|
const {files} = await buildTestingTree({
|
|
|
|
exportConfig: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(files).toContain(getProjectFile('.puppeteerrc.cjs'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not create Puppeteer config', async () => {
|
|
|
|
const {files} = await buildTestingTree({
|
|
|
|
exportConfig: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(files).not.toContain(getProjectFile('.puppeteerrc.cjs'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create Jasmine files and update "package.json"', async () => {
|
|
|
|
const tree = await buildTestingTree({
|
|
|
|
testingFramework: 'jasmine',
|
|
|
|
});
|
2022-11-22 13:02:24 +00:00
|
|
|
const {devDependencies} = getPackageJson(tree);
|
2022-11-23 12:10:03 +00:00
|
|
|
const {options} = getAngularJsonScripts(tree);
|
2022-11-09 13:52:10 +00:00
|
|
|
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/support/jasmine.json'));
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/helpers/babel.js'));
|
|
|
|
expect(devDependencies).toContain('jasmine');
|
|
|
|
expect(devDependencies).toContain('@babel/core');
|
|
|
|
expect(devDependencies).toContain('@babel/register');
|
2022-11-14 08:30:49 +00:00
|
|
|
expect(devDependencies).toContain('@babel/preset-typescript');
|
2022-11-23 12:10:03 +00:00
|
|
|
expect(options['commands']).toEqual([
|
2022-11-22 13:02:24 +00:00
|
|
|
[`jasmine`, '--config=./e2e/support/jasmine.json'],
|
|
|
|
]);
|
2022-11-09 13:52:10 +00:00
|
|
|
});
|
2022-11-11 13:38:52 +00:00
|
|
|
|
|
|
|
it('should create Jest files and update "package.json"', async () => {
|
|
|
|
const tree = await buildTestingTree({
|
|
|
|
testingFramework: 'jest',
|
|
|
|
});
|
2022-11-22 13:02:24 +00:00
|
|
|
const {devDependencies} = getPackageJson(tree);
|
2022-11-23 12:10:03 +00:00
|
|
|
const {options} = getAngularJsonScripts(tree);
|
2022-11-11 13:38:52 +00:00
|
|
|
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/jest.config.js'));
|
|
|
|
expect(devDependencies).toContain('jest');
|
|
|
|
expect(devDependencies).toContain('@types/jest');
|
|
|
|
expect(devDependencies).toContain('ts-jest');
|
2022-11-23 12:10:03 +00:00
|
|
|
expect(options['commands']).toEqual([[`jest`, '-c', 'e2e/jest.config.js']]);
|
2022-11-11 13:38:52 +00:00
|
|
|
});
|
2022-11-14 08:30:49 +00:00
|
|
|
|
2022-11-22 13:02:24 +00:00
|
|
|
it('should create Mocha files and update "package.json"', async () => {
|
2022-11-14 08:30:49 +00:00
|
|
|
const tree = await buildTestingTree({
|
|
|
|
testingFramework: 'mocha',
|
|
|
|
});
|
2022-11-22 13:02:24 +00:00
|
|
|
const {devDependencies} = getPackageJson(tree);
|
2022-11-23 12:10:03 +00:00
|
|
|
const {options} = getAngularJsonScripts(tree);
|
2022-11-14 08:30:49 +00:00
|
|
|
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/.mocharc.js'));
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/babel.js'));
|
|
|
|
expect(devDependencies).toContain('mocha');
|
|
|
|
expect(devDependencies).toContain('@types/mocha');
|
|
|
|
expect(devDependencies).toContain('@babel/core');
|
|
|
|
expect(devDependencies).toContain('@babel/register');
|
|
|
|
expect(devDependencies).toContain('@babel/preset-typescript');
|
2022-11-23 12:10:03 +00:00
|
|
|
expect(options['commands']).toEqual([
|
2022-11-22 13:02:24 +00:00
|
|
|
[`mocha`, '--config=./e2e/.mocharc.js'],
|
|
|
|
]);
|
2022-11-14 08:30:49 +00:00
|
|
|
});
|
2022-11-14 11:57:42 +00:00
|
|
|
|
|
|
|
it('should create Node files"', async () => {
|
|
|
|
const tree = await buildTestingTree({
|
|
|
|
testingFramework: 'node',
|
|
|
|
});
|
2022-11-23 12:10:03 +00:00
|
|
|
const {options} = getAngularJsonScripts(tree);
|
2022-11-14 11:57:42 +00:00
|
|
|
|
|
|
|
expect(tree.files).toContain(getProjectFile('e2e/.gitignore'));
|
2022-11-23 12:10:03 +00:00
|
|
|
expect(options['commands']).toEqual([
|
2022-11-22 13:02:24 +00:00
|
|
|
[`tsc`, '-p', 'e2e/tsconfig.json'],
|
|
|
|
['node', '--test', 'e2e/'],
|
|
|
|
]);
|
2022-11-14 11:57:42 +00:00
|
|
|
});
|
2022-11-09 13:52:10 +00:00
|
|
|
});
|