forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-files_spec.ts
More file actions
109 lines (94 loc) · 3.39 KB
/
setup-files_spec.ts
File metadata and controls
109 lines (94 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import { execute } from '../../index';
import {
BASE_OPTIONS,
describeBuilder,
setupApplicationTarget,
UNIT_TEST_BUILDER_INFO,
} from '../setup';
describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
describe('Option: "setupFiles"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});
it('should fail when a setup file does not exist', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
setupFiles: ['src/setup.ts'],
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeFalse();
// TODO: Re-enable once Vite logs are remapped through build system
// expectLog(logs, `The specified setup file "src/setup.ts" does not exist.`);
});
it('should include the setup files', async () => {
await harness.writeFiles({
'src/setup.ts': `(globalThis as any).setupLoaded = true;`,
'src/app/app.component.spec.ts': `
import { describe, expect, test } from 'vitest'
describe('AppComponent', () => {
test('should create the app', () => {
expect((globalThis as any).setupLoaded).toBe(true);
});
});`,
});
await harness.modifyFile('src/tsconfig.spec.json', (content) => {
const tsConfig = JSON.parse(content);
tsConfig.files ??= [];
tsConfig.files.push('setup.ts');
return JSON.stringify(tsConfig);
});
harness.useTarget('test', {
...BASE_OPTIONS,
setupFiles: ['src/setup.ts'],
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
it('should allow setup files to configure testing module', async () => {
await harness.writeFiles({
'src/setup.ts': `
import { TestBed } from '@angular/core/testing';
import { beforeEach } from 'vitest';
import { SETUP_LOADED_TOKEN } from './setup-loaded-token';
beforeEach(() => {
TestBed.configureTestingModule({
providers: [{provide: SETUP_LOADED_TOKEN, useValue: true}],
});
});
`,
'src/setup-loaded-token.ts': `
import { InjectionToken } from '@angular/core';
export const SETUP_LOADED_TOKEN = new InjectionToken<boolean>('SETUP_LOADED_TOKEN');
`,
'src/app/app.component.spec.ts': `
import { beforeEach, describe, expect, test } from 'vitest';
import { TestBed } from '@angular/core/testing';
import { SETUP_LOADED_TOKEN } from '../setup-loaded-token';
describe('AppComponent', () => {
test('should create the app', () => {
expect(TestBed.inject(SETUP_LOADED_TOKEN)).toBe(true);
});
});`,
});
await harness.modifyFile('src/tsconfig.spec.json', (content) => {
const tsConfig = JSON.parse(content);
tsConfig.files ??= [];
tsConfig.files.push('setup.ts');
return JSON.stringify(tsConfig);
});
harness.useTarget('test', {
...BASE_OPTIONS,
setupFiles: ['src/setup.ts'],
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});