-
Notifications
You must be signed in to change notification settings - Fork 34
/
plugins.test.ts
80 lines (77 loc) · 2.78 KB
/
plugins.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
import {test, expect, vi} from 'vitest';
import {ProjectAware} from '@reskript/core';
import {fillProjectSettings} from '../defaults.js';
import {ProjectSettings, SettingsPlugin} from '../interface.js';
import {applyPlugins} from '../plugins.js';
test('one plugin', async () => {
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const plugin = vi.fn((settings: ProjectSettings, cmd: ProjectAware): ProjectSettings => {
return {
...settings,
devServer: {
...settings.devServer,
port: 8000,
defaultProxyDomain: cmd.cwd,
},
};
});
const options = {cwd: 'cwd', command: 'build'};
const output = await applyPlugins(settings, [plugin], options);
expect(plugin).toHaveBeenCalled();
// @ts-expect-error
expect(plugin.mock.calls[0][0]).toBe(settings);
// @ts-expect-error
expect(plugin.mock.calls[0][1]).toBe(options);
expect(output.devServer.port).toBe(8000);
});
test('multiple plugins', async () => {
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const port = (settings: ProjectSettings): ProjectSettings => {
return {
...settings,
devServer: {
...settings.devServer,
port: 8000,
},
};
};
const domain = async (settings: ProjectSettings): Promise<ProjectSettings> => {
return {
...settings,
devServer: {
...settings.devServer,
defaultProxyDomain: 'random.api.js',
},
};
};
const output = await applyPlugins(settings, [port, domain], {cwd: '', command: 'build'});
expect(output.devServer.port).toBe(8000);
expect(output.devServer.defaultProxyDomain).toBe('random.api.js');
});
test('plugins factory', async () => {
const settings: ProjectSettings = fillProjectSettings({provider: 'webpack', devServer: {}});
const port: SettingsPlugin = settings => {
return {
...settings,
devServer: {
...settings.devServer,
port: 8000,
},
};
};
const domain: SettingsPlugin = settings => {
return {
...settings,
devServer: {
...settings.devServer,
defaultProxyDomain: 'random.api.js',
},
};
};
const factory = vi.fn(() => [port, domain]);
const output = await applyPlugins(settings, factory, {cwd: '', command: 'build'});
expect(factory).toHaveBeenCalled();
expect(factory.mock.calls[0][0]).toBe('build');
expect(output.devServer.port).toBe(8000);
expect(output.devServer.defaultProxyDomain).toBe('random.api.js');
});