|
| 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('Option: "polyfills"', () => { |
| 19 | + beforeEach(async () => { |
| 20 | + setupApplicationTarget(harness); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should include polyfills specified on test target', async () => { |
| 24 | + await harness.writeFiles({ |
| 25 | + 'src/custom-polyfill.js': `globalThis['CUSTOM_POLYFILL_RAN'] = true;`, |
| 26 | + 'src/app/app.component.spec.ts': ` |
| 27 | + import { describe, expect, test } from 'vitest'; |
| 28 | + describe('Polyfill Test', () => { |
| 29 | + test('should have run test polyfill', () => { |
| 30 | + expect((globalThis as any)['CUSTOM_POLYFILL_RAN']).toBe(true); |
| 31 | + }); |
| 32 | + });`, |
| 33 | + }); |
| 34 | + |
| 35 | + harness.useTarget('test', { |
| 36 | + ...BASE_OPTIONS, |
| 37 | + polyfills: ['src/custom-polyfill.js'], |
| 38 | + }); |
| 39 | + |
| 40 | + const { result } = await harness.executeOnce(); |
| 41 | + expect(result?.success).toBeTrue(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should override buildTarget polyfills when polyfills is specified on test target', async () => { |
| 45 | + setupApplicationTarget(harness, { |
| 46 | + polyfills: ['src/app-polyfill.js'], |
| 47 | + }); |
| 48 | + |
| 49 | + await harness.writeFiles({ |
| 50 | + 'src/app-polyfill.js': `globalThis['APP_POLYFILL_RAN'] = true;`, |
| 51 | + 'src/test-polyfill.js': `globalThis['TEST_POLYFILL_RAN'] = true;`, |
| 52 | + 'src/app/app.component.spec.ts': ` |
| 53 | + import { describe, expect, test } from 'vitest'; |
| 54 | + describe('Polyfill Override Test', () => { |
| 55 | + test('should have run test polyfill and not app polyfill', () => { |
| 56 | + expect((globalThis as any)['TEST_POLYFILL_RAN']).toBe(true); |
| 57 | + expect((globalThis as any)['APP_POLYFILL_RAN']).toBeUndefined(); |
| 58 | + }); |
| 59 | + });`, |
| 60 | + }); |
| 61 | + |
| 62 | + harness.useTarget('test', { |
| 63 | + ...BASE_OPTIONS, |
| 64 | + polyfills: ['src/test-polyfill.js'], |
| 65 | + }); |
| 66 | + |
| 67 | + const { result } = await harness.executeOnce(); |
| 68 | + expect(result?.success).toBeTrue(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should allow overriding buildTarget polyfills with an empty array', async () => { |
| 72 | + setupApplicationTarget(harness, { |
| 73 | + polyfills: ['src/app-polyfill.js'], |
| 74 | + }); |
| 75 | + |
| 76 | + await harness.writeFiles({ |
| 77 | + 'src/app-polyfill.js': `globalThis['APP_POLYFILL_RAN'] = true;`, |
| 78 | + 'src/app/app.component.spec.ts': ` |
| 79 | + import { describe, expect, test } from 'vitest'; |
| 80 | + describe('Empty Polyfill Override Test', () => { |
| 81 | + test('should not have run app polyfill', () => { |
| 82 | + expect((globalThis as any)['APP_POLYFILL_RAN']).toBeUndefined(); |
| 83 | + }); |
| 84 | + });`, |
| 85 | + }); |
| 86 | + |
| 87 | + harness.useTarget('test', { |
| 88 | + ...BASE_OPTIONS, |
| 89 | + polyfills: [], |
| 90 | + }); |
| 91 | + |
| 92 | + const { result } = await harness.executeOnce(); |
| 93 | + expect(result?.success).toBeTrue(); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments