From 7c5253f33624eeeb904e309bc8fd7c12c518a3dc Mon Sep 17 00:00:00 2001 From: Younes Jaaidi Date: Wed, 13 Nov 2024 22:17:08 +0100 Subject: [PATCH] feat(@angular-devkit/build-angular): add aot option to jest --- .../build_angular/src/builders/jest/index.ts | 2 +- .../src/builders/jest/schema.json | 5 +++ tests/legacy-cli/e2e/tests/jest/aot.ts | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/legacy-cli/e2e/tests/jest/aot.ts diff --git a/packages/angular_devkit/build_angular/src/builders/jest/index.ts b/packages/angular_devkit/build_angular/src/builders/jest/index.ts index f730d588cbba..c972dfa41a46 100644 --- a/packages/angular_devkit/build_angular/src/builders/jest/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/jest/index.ts @@ -85,7 +85,7 @@ export default createBuilder( tsConfig: options.tsConfig, polyfills: options.polyfills ?? ['zone.js', 'zone.js/testing'], outputPath: testOut, - aot: false, + aot: options.aot, index: false, outputHashing: OutputHashing.None, outExtension: 'mjs', // Force native ESM. diff --git a/packages/angular_devkit/build_angular/src/builders/jest/schema.json b/packages/angular_devkit/build_angular/src/builders/jest/schema.json index 6bd5670abb5d..272a1a906597 100644 --- a/packages/angular_devkit/build_angular/src/builders/jest/schema.json +++ b/packages/angular_devkit/build_angular/src/builders/jest/schema.json @@ -32,6 +32,11 @@ "uniqueItems": true }, "default": [] + }, + "aot": { + "type": "boolean", + "description": "Run tests using Ahead of Time compilation.", + "default": false } }, "additionalProperties": false, diff --git a/tests/legacy-cli/e2e/tests/jest/aot.ts b/tests/legacy-cli/e2e/tests/jest/aot.ts new file mode 100644 index 000000000000..b015e2a58757 --- /dev/null +++ b/tests/legacy-cli/e2e/tests/jest/aot.ts @@ -0,0 +1,41 @@ +import { deleteFile, writeFile } from '../../utils/fs'; +import { updateJsonFile } from '../../utils/project'; +import { applyJestBuilder } from '../../utils/jest'; +import { ng } from '../../utils/process'; + +export default async function (): Promise { + await applyJestBuilder(); + + { + await updateJsonFile('tsconfig.spec.json', (json) => { + return { + ...json, + include: ['src/**/*.spec.ts'], + }; + }); + + await writeFile( + 'src/aot.spec.ts', + ` + import { Component } from '@angular/core'; + + describe('Hello', () => { + it('should *not* contain jit instructions', () => { + @Component({ + template: 'Hello', + }) + class Hello {} + + expect((Hello as any).ɵcmp.template.toString()).not.toContain('jit'); + }); + }); + `.trim(), + ); + + const { stderr } = await ng('test', '--aot'); + + if (!stderr.includes('Ran all test suites.') || stderr.includes('failed')) { + throw new Error(`Components were not transformed using AOT.\STDERR:\n\n${stderr}`); + } + } +}