|
| 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 { |
| 10 | + setupConditionImport, |
| 11 | + setTargetMapping, |
| 12 | +} from '../../../../../../../../modules/testing/builder/src/dev_prod_mode'; |
| 13 | +import { executeDevServer } from '../../index'; |
| 14 | +import { executeOnceAndFetch } from '../execute-fetch'; |
| 15 | +import { describeServeBuilder } from '../jasmine-helpers'; |
| 16 | +import { BASE_OPTIONS, DEV_SERVER_BUILDER_INFO } from '../setup'; |
| 17 | + |
| 18 | +describeServeBuilder( |
| 19 | + executeDevServer, |
| 20 | + DEV_SERVER_BUILDER_INFO, |
| 21 | + (harness, setupTarget, isApplicationBuilder) => { |
| 22 | + describe('Behavior: "conditional imports"', () => { |
| 23 | + if (!isApplicationBuilder) { |
| 24 | + it('requires esbuild', () => { |
| 25 | + expect(true).toBeTrue(); |
| 26 | + }); |
| 27 | + |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + beforeEach(async () => { |
| 32 | + setupTarget(harness); |
| 33 | + |
| 34 | + await setupConditionImport(harness); |
| 35 | + }); |
| 36 | + |
| 37 | + interface ImportsTestCase { |
| 38 | + name: string; |
| 39 | + mapping: unknown; |
| 40 | + output?: string; |
| 41 | + } |
| 42 | + |
| 43 | + const GOOD_TARGET = './src/good.js'; |
| 44 | + const BAD_TARGET = './src/bad.js'; |
| 45 | + |
| 46 | + const testCases: ImportsTestCase[] = [ |
| 47 | + { name: 'simple string', mapping: GOOD_TARGET }, |
| 48 | + { |
| 49 | + name: 'default fallback without matching condition', |
| 50 | + mapping: { |
| 51 | + 'never': BAD_TARGET, |
| 52 | + 'default': GOOD_TARGET, |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: 'development condition', |
| 57 | + mapping: { |
| 58 | + 'development': GOOD_TARGET, |
| 59 | + 'default': BAD_TARGET, |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: 'production condition', |
| 64 | + mapping: { |
| 65 | + 'production': BAD_TARGET, |
| 66 | + 'default': GOOD_TARGET, |
| 67 | + }, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: 'browser condition (in browser)', |
| 71 | + mapping: { |
| 72 | + 'browser': GOOD_TARGET, |
| 73 | + 'default': BAD_TARGET, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ]; |
| 77 | + |
| 78 | + for (const testCase of testCases) { |
| 79 | + describe(testCase.name, () => { |
| 80 | + beforeEach(async () => { |
| 81 | + await setTargetMapping(harness, testCase.mapping); |
| 82 | + }); |
| 83 | + |
| 84 | + it('resolves to expected target', async () => { |
| 85 | + harness.useTarget('serve', { |
| 86 | + ...BASE_OPTIONS, |
| 87 | + }); |
| 88 | + |
| 89 | + const { result, response } = await executeOnceAndFetch(harness, '/main.js'); |
| 90 | + |
| 91 | + expect(result?.success).toBeTrue(); |
| 92 | + const output = await response?.text(); |
| 93 | + expect(output).toContain('good-value'); |
| 94 | + expect(output).not.toContain('bad-value'); |
| 95 | + }); |
| 96 | + }); |
| 97 | + } |
| 98 | + }); |
| 99 | + }, |
| 100 | +); |
0 commit comments