summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/ng-schematics/test/src/index.spec.ts
blob: 59636dbb38b6c4aa4412066e1dc92acef6907911 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import https from 'https';
import {join} from 'path';

import {JsonObject} from '@angular-devkit/core';
import {
  SchematicTestRunner,
  UnitTestTree,
} from '@angular-devkit/schematics/testing/schematic-test-runner';
import expect from 'expect';
import sinon from 'sinon';

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}`;
}

function getAngularJsonScripts(
  tree: UnitTestTree,
  isDefault = true
): {
  builder: string;
  configurations: Record<string, any>;
  options: Record<string, any>;
} {
  const angularJson = tree.readJson('angular.json') as any;
  const e2eScript = isDefault ? 'e2e' : 'puppeteer';
  return angularJson['projects']?.[APPLICATION_OPTIONS.name]?.['architect'][
    e2eScript
  ];
}

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 = {
    isDefaultTester: true,
    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();
    const {devDependencies, scripts} = getPackageJson(tree);
    const {builder, configurations} = getAngularJsonScripts(tree);

    expect(tree.files).toContain(getProjectFile('e2e/tsconfig.json'));
    expect(tree.files).toContain(getProjectFile('e2e/tests/app.e2e.ts'));
    expect(devDependencies).toContain('puppeteer');
    expect(scripts['e2e']).toBe('ng e2e');
    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');
  });

  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',
    });
    const {devDependencies} = getPackageJson(tree);
    const {options} = getAngularJsonScripts(tree);

    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');
    expect(devDependencies).toContain('@babel/preset-typescript');
    expect(options['commands']).toEqual([
      [`jasmine`, '--config=./e2e/support/jasmine.json'],
    ]);
  });

  it('should create Jest files and update "package.json"', async () => {
    const tree = await buildTestingTree({
      testingFramework: 'jest',
    });
    const {devDependencies} = getPackageJson(tree);
    const {options} = getAngularJsonScripts(tree);

    expect(tree.files).toContain(getProjectFile('e2e/jest.config.js'));
    expect(devDependencies).toContain('jest');
    expect(devDependencies).toContain('@types/jest');
    expect(devDependencies).toContain('ts-jest');
    expect(options['commands']).toEqual([[`jest`, '-c', 'e2e/jest.config.js']]);
  });

  it('should create Mocha files and update "package.json"', async () => {
    const tree = await buildTestingTree({
      testingFramework: 'mocha',
    });
    const {devDependencies} = getPackageJson(tree);
    const {options} = getAngularJsonScripts(tree);

    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');
    expect(options['commands']).toEqual([
      [`mocha`, '--config=./e2e/.mocharc.js'],
    ]);
  });

  it('should create Node files"', async () => {
    const tree = await buildTestingTree({
      testingFramework: 'node',
    });
    const {options} = getAngularJsonScripts(tree);

    expect(tree.files).toContain(getProjectFile('e2e/.gitignore'));
    expect(options['commands']).toEqual([
      [`tsc`, '-p', 'e2e/tsconfig.json'],
      ['node', '--test', 'e2e/'],
    ]);
  });
});