-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Expand file tree
/
Copy pathtest_spec.ts
More file actions
132 lines (114 loc) · 4.11 KB
/
test_spec.ts
File metadata and controls
132 lines (114 loc) · 4.11 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
121
122
123
124
125
126
127
128
129
130
131
132
/**
* @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 { CommandError } from '../host';
import type { MockHost } from '../testing/mock-host';
import {
MockMcpToolContext,
addProjectToWorkspace,
createMockContext,
} from '../testing/test-utils';
import { runTest } from './test';
describe('Test Tool', () => {
let mockHost: MockHost;
let mockContext: MockMcpToolContext;
beforeEach(() => {
const mock = createMockContext();
mockHost = mock.host;
mockContext = mock.context;
addProjectToWorkspace(mock.projects, 'my-app');
});
it('should construct the command correctly with defaults', async () => {
mockContext.workspace.extensions['defaultProject'] = 'my-app';
await runTest({}, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
['test', 'my-app', '--browsers', 'ChromeHeadless', '--watch', 'false'],
{ cwd: '/test' },
);
});
it('should construct the command correctly with a specified project', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'my-lib');
await runTest({ project: 'my-lib' }, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
['test', 'my-lib', '--browsers', 'ChromeHeadless', '--watch', 'false'],
{ cwd: '/test' },
);
});
it('should construct the command correctly with filter', async () => {
mockContext.workspace.extensions['defaultProject'] = 'my-app';
await runTest({ filter: 'AppComponent' }, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
[
'test',
'my-app',
'--browsers',
'ChromeHeadless',
'--watch',
'false',
'--filter',
'AppComponent',
],
{ cwd: '/test' },
);
});
it('should handle a successful test run and capture logs', async () => {
const testLogs = ['Executed 10 of 10 SUCCESS', 'Total: 10 success'];
mockHost.runCommand.and.resolveTo({
logs: testLogs,
});
const { structuredContent } = await runTest({ project: 'my-app' }, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
['test', 'my-app', '--browsers', 'ChromeHeadless', '--watch', 'false'],
{ cwd: '/test' },
);
expect(structuredContent.status).toBe('success');
expect(structuredContent.logs).toEqual(testLogs);
});
it('should handle a failed test run and capture logs', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'my-failed-app');
const testLogs = ['Executed 10 of 10 FAILED', 'Error: Some test failed'];
const error = new CommandError('Test failed', testLogs, 1);
mockHost.runCommand.and.rejectWith(error);
const { structuredContent } = await runTest({ project: 'my-failed-app' }, mockContext);
expect(structuredContent.status).toBe('failure');
expect(structuredContent.logs).toEqual([...testLogs, 'Test failed']);
});
it('should use the headless option for the unit-test builder when using Vitest', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'my-vitest-app', {
test: {
builder: '@angular/build:unit-test',
options: {
runner: 'vitest',
},
},
});
await runTest({ project: 'my-vitest-app' }, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
['test', 'my-vitest-app', '--headless', 'true', '--watch', 'false'],
{ cwd: '/test' },
);
});
it('should use the headless option for the unit-test builder when the runner is omitted', async () => {
addProjectToWorkspace(mockContext.workspace.projects, 'my-default-vitest-app', {
test: {
builder: '@angular/build:unit-test',
options: {},
},
});
await runTest({ project: 'my-default-vitest-app' }, mockContext);
expect(mockHost.runCommand).toHaveBeenCalledWith(
'ng',
['test', 'my-default-vitest-app', '--headless', 'true', '--watch', 'false'],
{ cwd: '/test' },
);
});
});