-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathtest-manager.test.ts
238 lines (208 loc) · 6.95 KB
/
test-manager.test.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { describe, expect, it, vi } from 'vitest';
import { createVitest as actualCreateVitest } from 'vitest/node';
import { Channel, type ChannelTransport } from 'storybook/internal/channels';
import { experimental_MockUniversalStore } from 'storybook/internal/core-server';
import type { StoryIndex } from 'storybook/internal/types';
import path from 'pathe';
import { TEST_PROVIDER_ID, storeOptions } from '../constants';
import { TestManager } from './test-manager';
const setTestNamePattern = vi.hoisted(() => vi.fn());
const vitest = vi.hoisted(() => ({
projects: [{}],
init: vi.fn(),
close: vi.fn(),
onCancel: vi.fn(),
runFiles: vi.fn(),
cancelCurrentRun: vi.fn(),
globTestSpecs: vi.fn(),
getModuleProjects: vi.fn(() => []),
setGlobalTestNamePattern: setTestNamePattern,
vite: {
watcher: {
removeAllListeners: vi.fn(),
on: vi.fn(),
},
moduleGraph: {
getModulesByFile: () => [],
invalidateModule: vi.fn(),
},
},
}));
vi.mock('vitest/node', async (importOriginal) => ({
...(await importOriginal()),
createVitest: vi.fn(() => Promise.resolve(vitest)),
}));
const createVitest = vi.mocked(actualCreateVitest);
const transport = { setHandler: vi.fn(), send: vi.fn() } satisfies ChannelTransport;
const mockChannel = new Channel({ transport });
const tests = [
{
project: { config: { env: { __STORYBOOK_URL__: 'http://localhost:6006' } } },
moduleId: path.join(process.cwd(), 'path/to/file'),
},
{
project: { config: { env: { __STORYBOOK_URL__: 'http://localhost:6006' } } },
moduleId: path.join(process.cwd(), 'path/to/another/file'),
},
];
global.fetch = vi.fn().mockResolvedValue({
json: () =>
new Promise((resolve) =>
resolve({
v: 5,
entries: {
'story--one': {
type: 'story',
id: 'story--one',
name: 'One',
title: 'story/one',
importPath: 'path/to/file',
tags: ['test'],
},
'another--one': {
type: 'story',
id: 'another--one',
name: 'One',
title: 'another/one',
importPath: 'path/to/another/file',
tags: ['test'],
},
},
} as StoryIndex)
),
});
const options: ConstructorParameters<typeof TestManager>[2] = {
onError: (message, error) => {
throw error;
},
onReady: vi.fn(),
};
describe('TestManager', () => {
it('should create a vitest instance', async () => {
new TestManager(mockChannel, new experimental_MockUniversalStore(storeOptions, vi), options);
await vi.waitFor(() => {
expect(createVitest).toHaveBeenCalled();
});
});
it('should call onReady callback', async () => {
new TestManager(mockChannel, new experimental_MockUniversalStore(storeOptions, vi), options);
await vi.waitFor(() => {
expect(options.onReady).toHaveBeenCalled();
});
});
it('TestManager.start should start vitest and resolve when ready', async () => {
const testManager = await TestManager.start(
mockChannel,
new experimental_MockUniversalStore(storeOptions, vi),
options
);
expect(testManager).toBeInstanceOf(TestManager);
expect(createVitest).toHaveBeenCalled();
});
it('should handle watch mode request', async () => {
const testManager = await TestManager.start(
mockChannel,
new experimental_MockUniversalStore(storeOptions, vi),
options
);
expect(createVitest).toHaveBeenCalledTimes(1);
await testManager.handleWatchModeRequest(true);
expect(createVitest).toHaveBeenCalledTimes(1); // shouldn't restart vitest
});
it('should handle run request', async () => {
vitest.globTestSpecs.mockImplementation(() => tests);
const testManager = await TestManager.start(
mockChannel,
new experimental_MockUniversalStore(storeOptions, vi),
options
);
expect(createVitest).toHaveBeenCalledTimes(1);
await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
indexUrl: 'http://localhost:6006/index.json',
});
expect(createVitest).toHaveBeenCalledTimes(1);
expect(vitest.runFiles).toHaveBeenCalledWith(tests, true);
});
it('should filter tests', async () => {
vitest.globTestSpecs.mockImplementation(() => tests);
const testManager = await TestManager.start(
mockChannel,
new experimental_MockUniversalStore(storeOptions, vi),
options
);
await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
indexUrl: 'http://localhost:6006/index.json',
storyIds: [],
});
expect(vitest.runFiles).toHaveBeenCalledWith([], true);
await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
indexUrl: 'http://localhost:6006/index.json',
storyIds: ['story--one'],
});
expect(setTestNamePattern).toHaveBeenCalledWith(/^One$/);
expect(vitest.runFiles).toHaveBeenCalledWith(tests.slice(0, 1), true);
});
it('should handle coverage toggling', async () => {
const testManager = await TestManager.start(
mockChannel,
new experimental_MockUniversalStore(storeOptions, vi),
options
);
expect(createVitest).toHaveBeenCalledTimes(1);
createVitest.mockClear();
await testManager.handleConfigChange(
{
coverage: true,
a11y: false,
},
{
coverage: false,
a11y: false,
}
);
expect(createVitest).toHaveBeenCalledTimes(1);
createVitest.mockClear();
await testManager.handleConfigChange(
{
coverage: false,
a11y: false,
},
{
coverage: true,
a11y: false,
}
);
expect(createVitest).toHaveBeenCalledTimes(1);
});
it('should temporarily disable coverage on focused tests', async () => {
vitest.globTestSpecs.mockImplementation(() => tests);
const mockStore = new experimental_MockUniversalStore(storeOptions, vi);
const testManager = await TestManager.start(mockChannel, mockStore, options);
expect(createVitest).toHaveBeenCalledTimes(1);
createVitest.mockClear();
mockStore.setState((s) => ({ ...s, config: { coverage: true, a11y: false } }));
await vi.waitFor(() => {
expect(createVitest).toHaveBeenCalledTimes(1);
});
createVitest.mockClear();
await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
indexUrl: 'http://localhost:6006/index.json',
storyIds: ['button--primary', 'button--secondary'],
});
// expect vitest to be restarted twice, without and with coverage
expect(createVitest).toHaveBeenCalledTimes(2);
expect(vitest.runFiles).toHaveBeenCalledWith([], true);
createVitest.mockClear();
await testManager.handleRunRequest({
providerId: TEST_PROVIDER_ID,
indexUrl: 'http://localhost:6006/index.json',
});
// don't expect vitest to be restarted, as we're running all tests
expect(createVitest).not.toHaveBeenCalled();
expect(vitest.runFiles).toHaveBeenCalledWith(tests, true);
});
});