|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { BuilderContext, BuilderOutput } from '@angular-devkit/architect'; |
| 10 | +import assert from 'node:assert'; |
| 11 | +import { randomUUID } from 'node:crypto'; |
| 12 | +import path from 'node:path'; |
| 13 | +import { loadEsmModule } from '../../utils/load-esm'; |
| 14 | +import { buildApplicationInternal } from '../application'; |
| 15 | +import type { |
| 16 | + ApplicationBuilderExtensions, |
| 17 | + ApplicationBuilderInternalOptions, |
| 18 | +} from '../application/options'; |
| 19 | +import { ResultKind } from '../application/results'; |
| 20 | +import { OutputHashing } from '../application/schema'; |
| 21 | +import { writeTestFiles } from '../karma/application_builder'; |
| 22 | +import { findTests, getTestEntrypoints } from '../karma/find-tests'; |
| 23 | +import { normalizeOptions } from './options'; |
| 24 | +import type { Schema as UnitTestOptions } from './schema'; |
| 25 | + |
| 26 | +export type { UnitTestOptions }; |
| 27 | + |
| 28 | +/** |
| 29 | + * @experimental Direct usage of this function is considered experimental. |
| 30 | + */ |
| 31 | +export async function* execute( |
| 32 | + options: UnitTestOptions, |
| 33 | + context: BuilderContext, |
| 34 | + extensions: ApplicationBuilderExtensions = {}, |
| 35 | +): AsyncIterable<BuilderOutput> { |
| 36 | + // Determine project name from builder context target |
| 37 | + const projectName = context.target?.project; |
| 38 | + if (!projectName) { |
| 39 | + context.logger.error( |
| 40 | + `The "${context.builder.builderName}" builder requires a target to be specified.`, |
| 41 | + ); |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + context.logger.warn( |
| 47 | + `NOTE: The "${context.builder.builderName}" builder is currently EXPERIMENTAL and not ready for production use.`, |
| 48 | + ); |
| 49 | + |
| 50 | + const normalizedOptions = await normalizeOptions(context, projectName, options); |
| 51 | + const { projectSourceRoot, workspaceRoot, runnerName } = normalizedOptions; |
| 52 | + |
| 53 | + if (runnerName !== 'vitest') { |
| 54 | + context.logger.error('Unknown test runner: ' + runnerName); |
| 55 | + |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Find test files |
| 60 | + const testFiles = await findTests( |
| 61 | + normalizedOptions.include, |
| 62 | + normalizedOptions.exclude, |
| 63 | + workspaceRoot, |
| 64 | + projectSourceRoot, |
| 65 | + ); |
| 66 | + |
| 67 | + if (testFiles.length === 0) { |
| 68 | + context.logger.error('No tests found.'); |
| 69 | + |
| 70 | + return { success: false }; |
| 71 | + } |
| 72 | + |
| 73 | + const entryPoints = getTestEntrypoints(testFiles, { projectSourceRoot, workspaceRoot }); |
| 74 | + entryPoints.set( |
| 75 | + 'init-testbed', |
| 76 | + path.join(__dirname, '..', 'karma', 'polyfills', 'init_test_bed.js'), |
| 77 | + ); |
| 78 | + |
| 79 | + const { startVitest } = await loadEsmModule<typeof import('vitest/node')>('vitest/node'); |
| 80 | + |
| 81 | + // Setup test file build options based on application build target options |
| 82 | + const buildTargetOptions = (await context.validateOptions( |
| 83 | + await context.getTargetOptions(normalizedOptions.buildTarget), |
| 84 | + await context.getBuilderNameForTarget(normalizedOptions.buildTarget), |
| 85 | + )) as unknown as ApplicationBuilderInternalOptions; |
| 86 | + |
| 87 | + if (buildTargetOptions.polyfills?.includes('zone.js')) { |
| 88 | + buildTargetOptions.polyfills.push('zone.js/testing'); |
| 89 | + } |
| 90 | + |
| 91 | + const outputPath = path.join(context.workspaceRoot, 'dist/test-out', randomUUID()); |
| 92 | + const buildOptions: ApplicationBuilderInternalOptions = { |
| 93 | + ...buildTargetOptions, |
| 94 | + watch: normalizedOptions.watch, |
| 95 | + outputPath, |
| 96 | + index: false, |
| 97 | + browser: undefined, |
| 98 | + server: undefined, |
| 99 | + localize: false, |
| 100 | + budgets: [], |
| 101 | + serviceWorker: false, |
| 102 | + appShell: false, |
| 103 | + ssr: false, |
| 104 | + prerender: false, |
| 105 | + sourceMap: { scripts: true, vendor: false, styles: false }, |
| 106 | + outputHashing: OutputHashing.None, |
| 107 | + optimization: false, |
| 108 | + tsConfig: normalizedOptions.tsConfig, |
| 109 | + entryPoints, |
| 110 | + externalDependencies: ['vitest', ...(buildTargetOptions.externalDependencies ?? [])], |
| 111 | + }; |
| 112 | + |
| 113 | + let instance: import('vitest/node').Vitest | undefined; |
| 114 | + |
| 115 | + for await (const result of buildApplicationInternal(buildOptions, context, extensions)) { |
| 116 | + if (result.kind === ResultKind.Failure) { |
| 117 | + continue; |
| 118 | + } else if (result.kind !== ResultKind.Full) { |
| 119 | + assert.fail('A full build result is required from the application builder.'); |
| 120 | + } |
| 121 | + |
| 122 | + assert(result.files, 'Builder did not provide result files.'); |
| 123 | + |
| 124 | + await writeTestFiles(result.files, outputPath); |
| 125 | + |
| 126 | + const setupFiles = ['init-testbed.js']; |
| 127 | + if (buildTargetOptions?.polyfills?.length) { |
| 128 | + setupFiles.push('polyfills.js'); |
| 129 | + } |
| 130 | + |
| 131 | + instance ??= await startVitest( |
| 132 | + 'test', |
| 133 | + undefined /* cliFilters */, |
| 134 | + undefined /* options */, |
| 135 | + { |
| 136 | + test: { |
| 137 | + root: outputPath, |
| 138 | + setupFiles, |
| 139 | + environment: 'jsdom', |
| 140 | + watch: normalizedOptions.watch, |
| 141 | + reporters: normalizedOptions.reporters, |
| 142 | + coverage: { |
| 143 | + enabled: normalizedOptions.codeCoverage, |
| 144 | + exclude: normalizedOptions.codeCoverageExclude, |
| 145 | + excludeAfterRemap: true, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + {}, |
| 150 | + ); |
| 151 | + |
| 152 | + // Check if all the tests pass to calculate the result |
| 153 | + const testModules = instance.state.getTestModules(); |
| 154 | + |
| 155 | + yield { success: testModules.every((testModule) => testModule.ok()) }; |
| 156 | + } |
| 157 | +} |
0 commit comments