-
Notifications
You must be signed in to change notification settings - Fork 11.9k
test(@angular-devkit/build-angular): add application/browser test runs #28479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,21 @@ | |
*/ | ||
|
||
import { Schema } from '../schema'; | ||
import { BuilderHandlerFn } from '@angular-devkit/architect'; | ||
import { json } from '@angular-devkit/core'; | ||
import { ApplicationBuilderOptions as ApplicationSchema, buildApplication } from '@angular/build'; | ||
import * as path from 'node:path'; | ||
import { readFileSync } from 'node:fs'; | ||
|
||
import { JasmineBuilderHarness } from '../../../testing'; | ||
import { host } from '../../../testing/test-utils'; | ||
import { BuilderHarness } from '../../../testing'; | ||
import { buildWebpackBrowser } from '../../browser'; | ||
import { Schema as BrowserSchema } from '../../browser/schema'; | ||
import { | ||
BASE_OPTIONS as BROWSER_BASE_OPTIONS, | ||
BROWSER_BUILDER_INFO, | ||
} from '../../browser/tests/setup'; | ||
|
||
export { describeBuilder } from '../../../testing'; | ||
|
||
|
@@ -27,3 +42,133 @@ export const BASE_OPTIONS = Object.freeze<Schema>({ | |
progress: false, | ||
watch: false, | ||
}); | ||
|
||
const optionSchemaCache = new Map<string, json.schema.JsonSchema>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is similar to the dev-server code and could likely be refactored to be shared across builders: https://github.com/angular/angular-cli/blob/main/packages/angular_devkit/build_angular/src/builders/dev-server/tests/jasmine-helpers.ts#L18-L25 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to improve the test harness in general so that refactor could fit in there. Probably a good item for the next fixit. |
||
|
||
function getCachedSchema(options: { schemaPath: string }): json.schema.JsonSchema { | ||
let optionSchema = optionSchemaCache.get(options.schemaPath); | ||
if (optionSchema === undefined) { | ||
optionSchema = JSON.parse(readFileSync(options.schemaPath, 'utf8')) as json.schema.JsonSchema; | ||
optionSchemaCache.set(options.schemaPath, optionSchema); | ||
} | ||
return optionSchema; | ||
} | ||
|
||
/** | ||
* Adds a `build` target to a builder test harness for the browser builder with the base options | ||
* used by the browser builder tests. | ||
* | ||
* @param harness The builder harness to use when setting up the browser builder target | ||
* @param extraOptions The additional options that should be used when executing the target. | ||
*/ | ||
export function setupBrowserTarget<T>( | ||
harness: BuilderHarness<T>, | ||
extraOptions?: Partial<BrowserSchema>, | ||
): void { | ||
const browserSchema = getCachedSchema(BROWSER_BUILDER_INFO); | ||
|
||
harness.withBuilderTarget( | ||
'build', | ||
buildWebpackBrowser, | ||
{ | ||
...BROWSER_BASE_OPTIONS, | ||
...extraOptions, | ||
}, | ||
{ | ||
builderName: BROWSER_BUILDER_INFO.name, | ||
optionSchema: browserSchema, | ||
}, | ||
); | ||
} | ||
|
||
/** | ||
* Contains all required application builder fields. | ||
* Also disables progress reporting to minimize logging output. | ||
*/ | ||
export const APPLICATION_BASE_OPTIONS = Object.freeze<ApplicationSchema>({ | ||
index: 'src/index.html', | ||
browser: 'src/main.ts', | ||
outputPath: 'dist', | ||
tsConfig: 'src/tsconfig.app.json', | ||
progress: false, | ||
|
||
// Disable optimizations | ||
optimization: false, | ||
|
||
// Enable polling (if a test enables watch mode). | ||
// This is a workaround for bazel isolation file watch not triggering in tests. | ||
poll: 100, | ||
}); | ||
|
||
// TODO: Remove and use import after Vite-based dev server is moved to new package | ||
export const APPLICATION_BUILDER_INFO = Object.freeze({ | ||
name: '@angular-devkit/build-angular:application', | ||
schemaPath: path.join( | ||
path.dirname(require.resolve('@angular/build/package.json')), | ||
'src/builders/application/schema.json', | ||
), | ||
}); | ||
|
||
/** | ||
* Adds a `build` target to a builder test harness for the application builder with the base options | ||
* used by the application builder tests. | ||
* | ||
* @param harness The builder harness to use when setting up the application builder target | ||
* @param extraOptions The additional options that should be used when executing the target. | ||
*/ | ||
export function setupApplicationTarget<T>( | ||
harness: BuilderHarness<T>, | ||
extraOptions?: Partial<ApplicationSchema>, | ||
): void { | ||
const applicationSchema = getCachedSchema(APPLICATION_BUILDER_INFO); | ||
|
||
harness.withBuilderTarget( | ||
'build', | ||
buildApplication, | ||
{ | ||
...APPLICATION_BASE_OPTIONS, | ||
...extraOptions, | ||
}, | ||
{ | ||
builderName: APPLICATION_BUILDER_INFO.name, | ||
optionSchema: applicationSchema, | ||
}, | ||
); | ||
} | ||
|
||
/** Runs the test against both an application- and a browser-builder context. */ | ||
export function describeKarmaBuilder<T>( | ||
builderHandler: BuilderHandlerFn<T & json.JsonObject>, | ||
options: { name?: string; schemaPath: string }, | ||
specDefinitions: (( | ||
harness: JasmineBuilderHarness<T>, | ||
setupTarget: typeof setupApplicationTarget, | ||
isApplicationTarget: true, | ||
) => void) & | ||
(( | ||
harness: JasmineBuilderHarness<T>, | ||
setupTarget: typeof setupBrowserTarget, | ||
isApplicationTarget: false, | ||
) => void), | ||
) { | ||
const optionSchema = getCachedSchema(options); | ||
const harness = new JasmineBuilderHarness<T>(builderHandler, host, { | ||
builderName: options.name, | ||
optionSchema, | ||
}); | ||
|
||
describe(options.name || builderHandler.name, () => { | ||
for (const isApplicationTarget of [true, false]) { | ||
describe(isApplicationTarget ? 'with application builder' : 'with browser builder', () => { | ||
beforeEach(() => host.initialize().toPromise()); | ||
afterEach(() => host.restore().toPromise()); | ||
|
||
if (isApplicationTarget) { | ||
specDefinitions(harness, setupApplicationTarget, true); | ||
} else { | ||
specDefinitions(harness, setupBrowserTarget, false); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theory: We're doubling the number of tests, so we can (and should) double the number of shards.