-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
plugin.unit.ts
198 lines (178 loc) · 7.19 KB
/
plugin.unit.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
import { resolve } from 'path';
import type { QwikManifest } from '../types';
import { createPlugin } from './plugin';
describe('qwik plugin', () => {
const cwd = process.cwd();
describe('normalizeOptions', () => {
it('defaults', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions();
expect(opts.target).toBe('client');
expect(opts.buildMode).toBe('development');
expect(opts.entryStrategy).toEqual({ type: 'hook' });
expect(opts.forceFullBuild).toBe(false);
expect(opts.debug).toBe(false);
expect(opts.rootDir).toBe(cwd);
expect(opts.input).toEqual([resolve(cwd, 'src', 'root.tsx')]);
expect(opts.outDir).toBe(resolve(cwd, 'dist'));
expect(opts.manifestInput).toBe(null);
expect(opts.manifestOutput).toBe(null);
expect(opts.srcDir).toBe(resolve(cwd, 'src'));
expect(opts.srcInputs).toBe(null);
});
it('defaults (buildMode: production)', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ buildMode: 'production' });
expect(opts.target).toBe('client');
expect(opts.buildMode).toBe('production');
expect(opts.entryStrategy).toEqual({ type: 'smart' });
expect(opts.forceFullBuild).toBe(true);
expect(opts.debug).toBe(false);
expect(opts.rootDir).toBe(cwd);
expect(opts.input).toEqual([resolve(cwd, 'src', 'root.tsx')]);
expect(opts.outDir).toBe(resolve(cwd, 'dist'));
expect(opts.manifestInput).toBe(null);
expect(opts.manifestOutput).toBe(null);
expect(opts.srcDir).toBe(resolve(cwd, 'src'));
expect(opts.srcInputs).toBe(null);
expect(opts.entryStrategy).toEqual({ type: 'smart' });
});
it('defaults (target: ssr)', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ target: 'ssr' });
expect(opts.target).toBe('ssr');
expect(opts.buildMode).toBe('development');
expect(opts.entryStrategy).toEqual({ type: 'inline' });
expect(opts.forceFullBuild).toBe(false);
expect(opts.debug).toBe(false);
expect(opts.rootDir).toBe(cwd);
expect(opts.input).toEqual([resolve(cwd, 'src', 'entry.ssr.tsx')]);
expect(opts.outDir).toBe(resolve(cwd, 'server'));
expect(opts.manifestInput).toBe(null);
expect(opts.manifestOutput).toBe(null);
expect(opts.srcDir).toBe(resolve(cwd, 'src'));
expect(opts.srcInputs).toBe(null);
});
it('defaults (buildMode: production, target: ssr)', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ buildMode: 'production', target: 'ssr' });
expect(opts.target).toBe('ssr');
expect(opts.buildMode).toBe('production');
expect(opts.entryStrategy).toEqual({ type: 'inline' });
expect(opts.forceFullBuild).toBe(false);
expect(opts.debug).toBe(false);
expect(opts.rootDir).toBe(cwd);
expect(opts.input).toEqual([resolve(cwd, 'src', 'entry.ssr.tsx')]);
expect(opts.outDir).toBe(resolve(cwd, 'server'));
expect(opts.manifestInput).toBe(null);
expect(opts.manifestOutput).toBe(null);
expect(opts.srcDir).toBe(resolve(cwd, 'src'));
expect(opts.srcInputs).toBe(null);
});
it('debug true', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ debug: true });
expect(opts.debug).toBe(true);
});
it('override entryStrategy', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({
entryStrategy: { type: 'component' },
buildMode: 'production',
});
expect(opts.entryStrategy.type).toBe('component');
});
it('entryStrategy, smart', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({
entryStrategy: { type: 'smart' },
forceFullBuild: false,
});
expect(opts.entryStrategy.type).toBe('smart');
expect(opts.forceFullBuild).toBe(true);
});
it('entryStrategy, hook no forceFullBuild', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ entryStrategy: { type: 'hook' } });
expect(opts.entryStrategy.type).toBe('hook');
expect(opts.forceFullBuild).toBe(false);
});
it('entryStrategy, hook and srcInputs', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({
entryStrategy: { type: 'hook' },
srcInputs: [],
});
expect(opts.entryStrategy.type).toBe('hook');
expect(opts.forceFullBuild).toBe(true);
});
it('entryStrategy, forceFullBuild false', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ forceFullBuild: false });
expect(opts.forceFullBuild).toBe(false);
});
it('entryStrategy, forceFullBuild true', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ forceFullBuild: true });
expect(opts.forceFullBuild).toBe(true);
});
it('rootDir, abs path', async () => {
const plugin = await mockPlugin();
const customRoot = resolve(cwd, 'abs-path');
const opts = plugin.normalizeOptions({ rootDir: customRoot });
expect(opts.rootDir).toBe(customRoot);
});
it('rootDir, rel path', async () => {
const plugin = await mockPlugin();
const customRoot = 'rel-path';
const opts = plugin.normalizeOptions({ rootDir: customRoot });
expect(opts.rootDir).toBe(resolve(cwd, customRoot));
});
it('input string', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ input: 'src/cmps/main.tsx' });
expect(opts.input).toEqual([resolve(cwd, 'src', 'cmps', 'main.tsx')]);
});
it('input array', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({
input: ['src/cmps/a.tsx', 'src/cmps/b.tsx'],
});
expect(opts.input).toEqual([
resolve(cwd, 'src', 'cmps', 'a.tsx'),
resolve(cwd, 'src', 'cmps', 'b.tsx'),
]);
});
it('outDir', async () => {
const plugin = await mockPlugin();
const opts = plugin.normalizeOptions({ outDir: 'out' });
expect(opts.outDir).toBe(resolve(cwd, 'out'));
});
it('manifestOutput', async () => {
const plugin = await mockPlugin();
const manifestOutput = () => {};
const opts = plugin.normalizeOptions({ manifestOutput });
expect(opts.manifestOutput).toBe(manifestOutput);
});
it(' manifestInput', async () => {
const plugin = await mockPlugin();
const manifestInput: QwikManifest = { mapping: {}, symbols: {}, bundles: {}, version: '1' };
const opts = plugin.normalizeOptions({ manifestInput });
expect(opts.manifestInput).toBe(manifestInput);
});
});
async function mockPlugin() {
const plugin = createPlugin({
sys: {
cwd: () => process.cwd(),
env: 'node',
os: process.platform,
dynamicImport: async (path) => require(path),
path: require('path'),
},
binding: { mockBinding: true },
});
await plugin.init();
return plugin;
}
});