-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathoptions.test.ts
More file actions
271 lines (246 loc) · 8.35 KB
/
options.test.ts
File metadata and controls
271 lines (246 loc) · 8.35 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { strict as assert } from 'node:assert';
import os from 'node:os';
import path from 'node:path';
import { mm } from '@eggjs/mock';
import { importResolve } from '@eggjs/utils';
import { describe, it, afterEach, beforeAll, afterAll } from 'vitest';
import { parseOptions } from '../src/utils/options.ts';
import { getFilepath, cluster } from './utils.ts';
const __dirname = import.meta.dirname;
describe('test/options.test.ts', () => {
afterEach(mm.restore);
it('should return undefined by port as default', async () => {
let options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert.equal(options.port, undefined);
});
it('should start with https and listen 8443', async () => {
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
https: {
key: getFilepath('server.key'),
cert: getFilepath('server.cert'),
},
});
assert.equal(options.port, 8443);
assert.equal(typeof options.https, 'object');
assert(options.https instanceof Object);
assert.equal(typeof options.https.key, 'string');
assert(options.https.cert);
});
it('should start with httpsOptions and listen 8443', async () => {
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
https: {
passphrase: '123456',
key: getFilepath('server.key'),
cert: getFilepath('server.cert'),
ca: getFilepath('server.ca'),
},
});
assert.equal(options.port, 8443);
assert(options.https instanceof Object);
assert(options.https.key);
assert(options.https.cert);
assert(options.https.ca);
assert(options.https.passphrase);
});
it('should listen custom port 6001', async () => {
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
port: '6001',
});
assert.equal(options.port, 6001);
});
it('should set NO_DEPRECATION on production env', async () => {
mm(process.env, 'NODE_ENV', 'production');
let options = await parseOptions({
baseDir: path.join(__dirname, '..'),
workers: 1,
});
assert.equal(options.workers, 1);
options = await parseOptions({
baseDir: path.join(__dirname, '..'),
workers: '101',
});
assert.equal(options.workers, 101);
assert.equal(process.env.NO_DEPRECATION, '*');
});
it('should not extend when port is null/undefined', async () => {
let options = await parseOptions({
baseDir: path.join(__dirname, '..'),
port: null,
});
assert.equal(options.port, undefined);
options = await parseOptions({
baseDir: path.join(__dirname, '..'),
port: undefined,
});
assert.equal(options.port, undefined);
options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert.equal(options.port, undefined);
});
it('should not call os.cpus when specify workers', async () => {
mm.syncError(os, 'cpus', 'should not call os.cpus');
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
workers: 1,
});
assert.equal(options.workers, 1);
});
describe('debug', () => {
it('empty', async () => {
mm(process, 'execArgv', []);
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert(options.isDebug === undefined);
});
it('--inspect', async () => {
mm(process, 'execArgv', ['--inspect=9229']);
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert(options.isDebug === true);
});
it('--debug', async () => {
mm(process, 'execArgv', ['--debug=5858']);
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert(options.isDebug === true);
});
});
describe('env', () => {
it('default env is undefined', async () => {
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert.equal(options.env, undefined);
});
it('custom env = prod', async () => {
const options = await parseOptions({
env: 'prod',
baseDir: path.join(__dirname, '..'),
});
assert.equal(options.env, 'prod');
});
it('default env set to process.env.EGG_SERVER_ENV', async () => {
mm(process.env, 'EGG_SERVER_ENV', 'prod');
const options = await parseOptions({
baseDir: path.join(__dirname, '..'),
});
assert.equal(options.env, 'prod');
});
});
// TODO: flaky test on windows, Hook timed out in 20000ms
describe.skipIf(process.platform === 'win32')('options', () => {
let app: any;
beforeAll(async () => {
app = cluster('apps/options', {
foo: true,
} as any).debug();
await app.ready();
});
afterAll(() => app.close());
it('should be passed through', () => {
app.expect('stdout', /app options foo: true/);
app.expect('stdout', /agent options foo: true/);
});
});
describe('framework', () => {
it('should get from absolute path', async () => {
let eggMockPackagePath = path.join(__dirname, '../../../plugins/mock');
const frameworkPath = path.dirname(importResolve('egg', { paths: [eggMockPackagePath] }));
const options = await parseOptions({
framework: frameworkPath,
});
assert.equal(options.framework, frameworkPath);
});
it('should get from absolute path but not exist', async () => {
const frameworkPath = path.join(__dirname, 'noexist');
try {
await parseOptions({
framework: frameworkPath,
});
throw new Error('should not run');
} catch (err: any) {
assert.equal(err.message, `${frameworkPath} should exist`);
}
});
it.skip('should get from npm package', async () => {
const frameworkPath = path.join(process.cwd(), 'node_modules/egg');
const options = await parseOptions({
framework: 'egg',
});
assert.equal(options.framework, frameworkPath);
});
it('should get from npm package but not exist', async () => {
try {
await parseOptions({
framework: 'noexist',
});
throw new Error('should not run');
} catch (err: any) {
assert.match(err.message, /noexist is not found in /);
}
});
// Node.js v20: SyntaxError: Unexpected identifier 'SingleModeApplication'
// Bun: fixture yadan/index.js does require('egg') which fails in Bun's module resolution
it.skipIf(process.version.startsWith('v20.') || !!process.versions.bun)(
'should get from pkg.egg.framework',
async () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-pkg-egg');
const options = await parseOptions({
baseDir,
});
assert.equal(options.framework, path.join(baseDir, 'node_modules/yadan'));
},
);
it('should get from pkg.egg.framework but not exist', async () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-pkg-egg-noexist');
try {
await parseOptions({
baseDir,
});
throw new Error('should not run');
} catch (err: any) {
assert.match(err.message, /noexist is not found in /);
}
});
it('should get egg by default', async () => {
const baseDir = path.join(__dirname, 'fixtures/apps/framework-egg-default');
const options = await parseOptions({
baseDir,
});
const expectPaths = [
// run int workspace root
path.join(__dirname, '../../egg'),
// run in project root
path.join(__dirname, '../node_modules/egg'),
// pnpm virtual store (Bun resolves through this path)
path.join(__dirname, '../../../node_modules/.pnpm/node_modules/egg'),
];
assert(
expectPaths.includes(options.framework),
`should get egg at ${expectPaths.join(', ')}, but got ${options.framework}`,
);
});
});
// it('should exist when specify baseDir', () => {
// it('should get egg by default but not exist', () => {
// const baseDir = path.join(__dirname, 'noexist');
// try {
// parseOptions({
// baseDir,
// });
// throw new Error('should not run');
// } catch (err) {
// assert(err.message === `${path.join(baseDir, 'package.json')} should exist`);
// }
// });
// });
});