-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathproviders-file_spec.ts
More file actions
120 lines (104 loc) · 3.9 KB
/
providers-file_spec.ts
File metadata and controls
120 lines (104 loc) · 3.9 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
110
111
112
113
114
115
116
117
118
119
120
/**
* @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,
UNIT_TEST_BUILDER_INFO,
setupApplicationTarget,
} from '../setup';
describeBuilder(execute, UNIT_TEST_BUILDER_INFO, (harness) => {
describe('Option: "providersFile"', () => {
beforeEach(async () => {
setupApplicationTarget(harness);
});
it('should fail when providersFile does not exist', async () => {
harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/my.providers.ts',
});
const { result, logs } = await harness.executeOnce({ outputLogsOnFailure: false });
expect(result?.success).toBeFalse();
expect(logs).toContain(
jasmine.objectContaining({
message: jasmine.stringMatching('Could not resolve "./my.providers"'),
}),
);
});
it('should use providers from the specified file', async () => {
await harness.writeFiles({
'src/my.providers.ts': `
import { importProvidersFrom } from '@angular/core';
import { CommonModule } from '@angular/common';
export default [importProvidersFrom(CommonModule)];
`,
});
await harness.modifyFile('src/tsconfig.spec.json', (content) => {
const tsConfig = JSON.parse(content);
tsConfig.files ??= [];
tsConfig.files.push('my.providers.ts');
return JSON.stringify(tsConfig);
});
harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/my.providers.ts',
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
it('should work with providers that inject services requiring JIT compilation', async () => {
// This test reproduces the issue from https://github.com/angular/angular-cli/issues/31993
// where using providersFile with a service that injects Router causes JIT compilation errors
await harness.writeFiles({
'src/test.service.ts': `
import { Injectable, inject } from '@angular/core';
import { Router } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class TestService {
router = inject(Router);
}
`,
'src/test.providers.ts': `
import { TestService } from './test.service';
export default [TestService];
`,
'src/app/app.component.spec.ts': `
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { TestService } from '../test.service';
import { describe, expect, it } from 'vitest';
describe('AppComponent', () => {
it('should create the app and inject TestService', () => {
TestBed.configureTestingModule({
declarations: [AppComponent],
});
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
const testService = TestBed.inject(TestService);
expect(app).toBeTruthy();
expect(testService).toBeTruthy();
expect(testService.router).toBeTruthy();
});
});
`,
});
await harness.modifyFile('src/tsconfig.spec.json', (content) => {
const tsConfig = JSON.parse(content);
tsConfig.files ??= [];
tsConfig.files.push('test.service.ts', 'test.providers.ts');
return JSON.stringify(tsConfig);
});
harness.useTarget('test', {
...BASE_OPTIONS,
providersFile: 'src/test.providers.ts',
});
const { result } = await harness.executeOnce();
expect(result?.success).toBeTrue();
});
});
});