|
| 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 { execute } from '../../index'; |
| 10 | +import { |
| 11 | + BASE_OPTIONS, |
| 12 | + describeBuilder, |
| 13 | + UNIT_TEST_BUILDER_INFO, |
| 14 | + setupApplicationTarget, |
| 15 | +} from '../setup'; |
| 16 | + |
| 17 | +describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => { |
| 18 | + describe('Behavior: "Vitest shared chunk initialization"', () => { |
| 19 | + // Regression test for https://github.com/angular/angular-cli/issues/33728. |
| 20 | + // |
| 21 | + // Without `disableCodeSplitting`, esbuild hoists a module imported by more than one spec |
| 22 | + // entry point into a shared chunk behind a lazy `__esm` initializer, and a class-field |
| 23 | + // initializer in another chunk reads the exported value as `undefined` under the jsdom |
| 24 | + // runner. All four trigger conditions are required and encoded below: |
| 25 | + // 1. two spec entry points import the shared module (so it lands in a shared chunk); |
| 26 | + // 2. a component in one entry reads the export during class-field initialization; |
| 27 | + // 3. that component's spec file contains an `async` test callback (no `await` needed); |
| 28 | + // 4. zone.js is in the polyfills (the `setupApplicationTarget` default), which downlevels |
| 29 | + // async and makes esbuild emit the spec entry CommonJS-wrapped. |
| 30 | + // |
| 31 | + // NOTE: the failure this guards against is sensitive to inert content — adding a top-level |
| 32 | + // side effect (even a `console.log`) to the shared or importing module below defused it |
| 33 | + // during reduction. Mirror https://github.com/jonmarozick/ng-shared-chunk-repro when |
| 34 | + // modifying these fixtures. |
| 35 | + it('should provide shared-module exports to class-field initializers in async specs', async () => { |
| 36 | + setupApplicationTarget(harness); |
| 37 | + |
| 38 | + harness.useTarget('test', { |
| 39 | + ...BASE_OPTIONS, |
| 40 | + }); |
| 41 | + |
| 42 | + // Keep the default project's spec deterministic; a third spec entry that does not touch |
| 43 | + // the shared module does not affect the reproduction (verified in a fresh workspace). |
| 44 | + await harness.writeFile( |
| 45 | + 'src/app/app.component.spec.ts', |
| 46 | + ` |
| 47 | + import { describe, it, expect } from 'vitest'; |
| 48 | +
|
| 49 | + describe('AppComponent placeholder', () => { |
| 50 | + it('runs', () => { |
| 51 | + expect(1 + 1).toBe(2); |
| 52 | + }); |
| 53 | + }); |
| 54 | + `, |
| 55 | + ); |
| 56 | + |
| 57 | + // The shared `const`. Reached from both spec entry points, so it is hoisted into a chunk |
| 58 | + // shared between them. |
| 59 | + await harness.writeFile( |
| 60 | + 'src/environments/env-config.ts', |
| 61 | + ` |
| 62 | + export interface DealerConfig { |
| 63 | + dealerId: string; |
| 64 | + clientKey: string; |
| 65 | + } |
| 66 | +
|
| 67 | + export const DEALERS: DealerConfig[] = [ |
| 68 | + { dealerId: 'dealer-one', clientKey: 'KEY-ONE' }, |
| 69 | + { dealerId: 'dealer-two', clientKey: 'KEY-TWO' }, |
| 70 | + { dealerId: 'dealer-three', clientKey: 'KEY-THREE' }, |
| 71 | + { dealerId: 'dealer-four', clientKey: 'KEY-FOUR' }, |
| 72 | + ]; |
| 73 | + `, |
| 74 | + ); |
| 75 | + |
| 76 | + // Reached by both spec entries, so it and env-config.ts land in the shared chunk. Reads the |
| 77 | + // const inside a method — after module initialization — and is the passing control. |
| 78 | + await harness.writeFile( |
| 79 | + 'src/app/features/lead-generator/services/lead.service.ts', |
| 80 | + ` |
| 81 | + import { Injectable } from '@angular/core'; |
| 82 | +
|
| 83 | + import { DEALERS } from '../../../../environments/env-config'; |
| 84 | +
|
| 85 | + @Injectable({ providedIn: 'root' }) |
| 86 | + export class LeadService { |
| 87 | + resolveClientKey(dealerId: string, clientKey?: string): string { |
| 88 | + return clientKey ?? DEALERS.find((d) => d.dealerId === dealerId)?.clientKey ?? ''; |
| 89 | + } |
| 90 | + } |
| 91 | + `, |
| 92 | + ); |
| 93 | + |
| 94 | + await harness.writeFile( |
| 95 | + 'src/app/features/lead-generator/services/index.ts', |
| 96 | + `export * from './lead.service';\n`, |
| 97 | + ); |
| 98 | + |
| 99 | + // Spec entry point 1 — the second importer that causes the chunk to be shared at all. |
| 100 | + await harness.writeFile( |
| 101 | + 'src/app/features/lead-generator/services/lead.service.spec.ts', |
| 102 | + ` |
| 103 | + import { describe, it, expect } from 'vitest'; |
| 104 | + import { TestBed } from '@angular/core/testing'; |
| 105 | +
|
| 106 | + import { LeadService } from './lead.service'; |
| 107 | +
|
| 108 | + describe('LeadService', () => { |
| 109 | + it('reads DEALERS inside a method', () => { |
| 110 | + TestBed.configureTestingModule({ providers: [LeadService] }); |
| 111 | +
|
| 112 | + expect(TestBed.inject(LeadService).resolveClientKey('dealer-one')).toBe('KEY-ONE'); |
| 113 | + }); |
| 114 | + }); |
| 115 | + `, |
| 116 | + ); |
| 117 | + |
| 118 | + // In the other chunk; reads the shared export eagerly during class-field initialization. |
| 119 | + await harness.writeFile( |
| 120 | + 'src/app/features/lead-generator/lead-generator.container.ts', |
| 121 | + ` |
| 122 | + import { Component, inject } from '@angular/core'; |
| 123 | +
|
| 124 | + import { LeadService } from './services'; |
| 125 | + import { DEALERS, DealerConfig } from '../../../environments/env-config'; |
| 126 | +
|
| 127 | + @Component({ |
| 128 | + selector: 'app-lead-generator', |
| 129 | + standalone: true, |
| 130 | + template: '', |
| 131 | + }) |
| 132 | + export class LeadGeneratorContainer { |
| 133 | + private readonly leadService = inject(LeadService); |
| 134 | +
|
| 135 | + readonly dealers: DealerConfig[] = DEALERS; |
| 136 | + readonly dealerOptions = this.dealers.map((d) => d.dealerId); |
| 137 | +
|
| 138 | + hasService(): boolean { |
| 139 | + return this.leadService != null; |
| 140 | + } |
| 141 | + } |
| 142 | + `, |
| 143 | + ); |
| 144 | + |
| 145 | + // Spec entry point 2 — the failing case without the fix. The `async` is load-bearing: |
| 146 | + // zone.js makes the builder downlevel it, the spec then imports the `__async` helper, and |
| 147 | + // esbuild emits this entry CommonJS-wrapped with the component module behind a lazy |
| 148 | + // `__esm` initializer. No `await` is needed; a synchronous callback hides the defect. |
| 149 | + await harness.writeFile( |
| 150 | + 'src/app/features/lead-generator/lead-generator.container.spec.ts', |
| 151 | + ` |
| 152 | + import { describe, it, expect } from 'vitest'; |
| 153 | + import { TestBed } from '@angular/core/testing'; |
| 154 | +
|
| 155 | + import { LeadGeneratorContainer } from './lead-generator.container'; |
| 156 | +
|
| 157 | + describe('LeadGeneratorContainer', () => { |
| 158 | + it('reads DEALERS in a class-field initialiser', async () => { |
| 159 | + const fixture = TestBed.createComponent(LeadGeneratorContainer); |
| 160 | +
|
| 161 | + expect(fixture.componentInstance.dealers.length).toBe(4); |
| 162 | + }); |
| 163 | + }); |
| 164 | + `, |
| 165 | + ); |
| 166 | + |
| 167 | + const { result } = await harness.executeOnce(); |
| 168 | + |
| 169 | + expect(result?.success).toBeTrue(); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments