test(@angular-devkit/build-angular): add dev-server builder deployUrl behavior tests

This change adds expanded unit tests for the dev-server builder's build deploy URL behavior using the builder test harness.
This commit is contained in:
Charles Lyding 2021-03-17 21:45:32 -04:00 committed by Charles
parent 352416fea8
commit 2041c70f76
3 changed files with 34 additions and 40 deletions

View File

@ -1,39 +0,0 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect, BuilderRun } from '@angular-devkit/architect';
import { DevServerBuilderOutput } from '@angular-devkit/build-angular';
import fetch from 'node-fetch'; // tslint:disable-line:no-implicit-dependencies
import { createArchitect, host } from '../test-utils';
describe('Dev Server Deploy Url', () => {
const target = { project: 'app', target: 'serve' };
let architect: Architect;
let runs: BuilderRun[] = [];
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
runs = [];
});
afterEach(async () => {
await host.restore().toPromise();
await Promise.all(runs.map(r => r.stop()));
});
it('works', async () => {
const run = await architect.scheduleTarget(target, { deployUrl: 'test/' });
runs.push(run);
const output = await run.result as DevServerBuilderOutput;
expect(output.success).toBe(true);
expect(output.baseUrl).toBe('http://localhost:4200/test');
const response = await fetch(`${output.baseUrl}/polyfills.js`);
expect(await response.text()).toContain('window["webpackJsonp"]');
}, 30000);
});

View File

@ -0,0 +1,33 @@
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { serveWebpackBrowser } from '../../index';
import { executeOnceAndFetch } from '../execute-fetch';
import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO, describeBuilder, setupBrowserTarget } from '../setup';
describeBuilder(serveWebpackBrowser, DEV_SERVER_BUILDER_INFO, (harness) => {
describe('Behavior: "browser builder deployUrl"', () => {
beforeEach(() => {
setupBrowserTarget(harness, {
deployUrl: 'test/',
});
});
it('uses the deploy URL defined in the "browserTarget" options as the serve path', async () => {
harness.useTarget('serve', {
...BASE_OPTIONS,
});
const { result, response } = await executeOnceAndFetch(harness, 'runtime.js');
expect(result?.success).toBeTrue();
expect(result?.baseUrl).toMatch(/\/test$/);
expect(response?.url).toMatch(/\/test\/runtime.js$/);
expect(await response?.text()).toContain('window["webpackJsonp"]');
});
});
});

View File

@ -26,7 +26,7 @@ export async function executeOnceAndFetch<T>(
mergeMap(async (executionResult) => {
let response = undefined;
if (executionResult.result?.success) {
const resolvedUrl = new URL(url, executionResult.result.baseUrl as string);
const resolvedUrl = new URL(url, `${executionResult.result.baseUrl}/`);
response = await fetch(resolvedUrl, options?.request);
}