@eggjs/bin 7.2.0 → 7.3.1-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -21
- package/bin/dev.js +0 -0
- package/dist/baseCommand.d.ts +52 -0
- package/dist/baseCommand.js +288 -0
- package/dist/commands/cov.d.ts +27 -0
- package/dist/commands/cov.js +86 -0
- package/dist/commands/dev.d.ts +26 -0
- package/dist/commands/dev.js +86 -0
- package/dist/commands/test.d.ts +28 -0
- package/dist/commands/test.js +168 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/scripts/postinstall.mjs +14 -7
- package/dist/scripts/start-cluster.cjs +3 -2
- package/dist/scripts/start-cluster.mjs +3 -2
- package/{src/types.ts → dist/types.d.ts} +4 -1
- package/dist/utils.js +34 -0
- package/package.json +48 -68
- package/scripts/postinstall.mjs +14 -7
- package/scripts/start-cluster.cjs +3 -2
- package/scripts/start-cluster.mjs +3 -2
- package/dist/commonjs/baseCommand.d.ts +0 -49
- package/dist/commonjs/baseCommand.js +0 -369
- package/dist/commonjs/commands/cov.d.ts +0 -22
- package/dist/commonjs/commands/cov.js +0 -98
- package/dist/commonjs/commands/dev.d.ts +0 -21
- package/dist/commonjs/commands/dev.js +0 -95
- package/dist/commonjs/commands/test.d.ts +0 -23
- package/dist/commonjs/commands/test.js +0 -222
- package/dist/commonjs/index.d.ts +0 -7
- package/dist/commonjs/index.js +0 -30
- package/dist/commonjs/package.json +0 -3
- package/dist/commonjs/types.d.ts +0 -9
- package/dist/commonjs/types.js +0 -3
- package/dist/commonjs/utils.d.ts +0 -4
- package/dist/commonjs/utils.js +0 -45
- package/dist/esm/baseCommand.d.ts +0 -49
- package/dist/esm/baseCommand.js +0 -361
- package/dist/esm/commands/cov.d.ts +0 -22
- package/dist/esm/commands/cov.js +0 -92
- package/dist/esm/commands/dev.d.ts +0 -21
- package/dist/esm/commands/dev.js +0 -92
- package/dist/esm/commands/test.d.ts +0 -23
- package/dist/esm/commands/test.js +0 -216
- package/dist/esm/index.d.ts +0 -7
- package/dist/esm/index.js +0 -8
- package/dist/esm/package.json +0 -3
- package/dist/esm/types.d.ts +0 -9
- package/dist/esm/types.js +0 -2
- package/dist/esm/utils.d.ts +0 -4
- package/dist/esm/utils.js +0 -36
- package/dist/package.json +0 -4
- package/src/baseCommand.ts +0 -390
- package/src/commands/cov.ts +0 -101
- package/src/commands/dev.ts +0 -99
- package/src/commands/test.ts +0 -236
- package/src/index.ts +0 -9
- package/src/utils.ts +0 -37
package/src/commands/test.ts
DELETED
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { debuglog } from 'node:util';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import os from 'node:os';
|
|
4
|
-
import fs from 'node:fs/promises';
|
|
5
|
-
import { Args, Flags } from '@oclif/core';
|
|
6
|
-
import globby from 'globby';
|
|
7
|
-
import { importResolve, detectType, EggType } from '@eggjs/utils';
|
|
8
|
-
import { getChangedFilesForRoots } from 'jest-changed-files';
|
|
9
|
-
// @ts-expect-error no types
|
|
10
|
-
import ciParallelVars from 'ci-parallel-vars';
|
|
11
|
-
import { BaseCommand } from '../baseCommand.js';
|
|
12
|
-
|
|
13
|
-
const debug = debuglog('@eggjs/bin/commands/test');
|
|
14
|
-
|
|
15
|
-
export default class Test<T extends typeof Test> extends BaseCommand<T> {
|
|
16
|
-
static override args = {
|
|
17
|
-
file: Args.string({
|
|
18
|
-
description: 'file(s) to test',
|
|
19
|
-
}),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
static override description = 'Run the test';
|
|
23
|
-
|
|
24
|
-
static override examples = [
|
|
25
|
-
'<%= config.bin %> <%= command.id %>',
|
|
26
|
-
'<%= config.bin %> <%= command.id %> test/index.test.ts',
|
|
27
|
-
'<%= config.bin %> <%= command.id %> test/index.test.ts,test/user.test.ts,...',
|
|
28
|
-
'<%= config.bin %> <%= command.id %> --json',
|
|
29
|
-
'<%= config.bin %> <%= command.id %> --log-level debug',
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
static override flags = {
|
|
33
|
-
bail: Flags.boolean({
|
|
34
|
-
description: 'bbort ("bail") after first test failure',
|
|
35
|
-
default: false,
|
|
36
|
-
char: 'b',
|
|
37
|
-
}),
|
|
38
|
-
timeout: Flags.integer({
|
|
39
|
-
char: 't',
|
|
40
|
-
description: 'set test-case timeout in milliseconds',
|
|
41
|
-
default: parseInt(process.env.TEST_TIMEOUT ?? '60000'),
|
|
42
|
-
}),
|
|
43
|
-
'no-timeout': Flags.boolean({
|
|
44
|
-
description: 'disable timeout',
|
|
45
|
-
}),
|
|
46
|
-
grep: Flags.string({
|
|
47
|
-
char: 'g',
|
|
48
|
-
description: 'only run tests matching <pattern>',
|
|
49
|
-
}),
|
|
50
|
-
changed: Flags.boolean({
|
|
51
|
-
description: 'only test with changed files and match test/**/*.test.(js|ts)',
|
|
52
|
-
char: 'c',
|
|
53
|
-
}),
|
|
54
|
-
mochawesome: Flags.boolean({
|
|
55
|
-
description: '[default: true] enable mochawesome reporter',
|
|
56
|
-
default: true,
|
|
57
|
-
allowNo: true,
|
|
58
|
-
}),
|
|
59
|
-
parallel: Flags.boolean({
|
|
60
|
-
description: 'mocha parallel mode',
|
|
61
|
-
default: false,
|
|
62
|
-
char: 'p',
|
|
63
|
-
}),
|
|
64
|
-
jobs: Flags.integer({
|
|
65
|
-
char: 't',
|
|
66
|
-
description: 'number of jobs to run in parallel',
|
|
67
|
-
default: os.cpus().length - 1,
|
|
68
|
-
}),
|
|
69
|
-
'auto-agent': Flags.boolean({
|
|
70
|
-
description: '[default: true] auto bootstrap agent in mocha master process',
|
|
71
|
-
default: true,
|
|
72
|
-
allowNo: true,
|
|
73
|
-
}),
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
public async run(): Promise<void> {
|
|
77
|
-
const { flags } = this;
|
|
78
|
-
|
|
79
|
-
try {
|
|
80
|
-
await fs.access(flags.base);
|
|
81
|
-
} catch (err) {
|
|
82
|
-
console.error('baseDir: %o not exists', flags.base);
|
|
83
|
-
throw err;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const mochaFile = process.env.MOCHA_FILE || importResolve('mocha/bin/_mocha');
|
|
87
|
-
if (flags.parallel) {
|
|
88
|
-
this.env.ENABLE_MOCHA_PARALLEL = 'true';
|
|
89
|
-
if (flags['auto-agent']) {
|
|
90
|
-
this.env.AUTO_AGENT = 'true';
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
// set NODE_ENV=test, let egg application load unittest logic
|
|
94
|
-
// https://eggjs.org/basics/env#difference-from-node_env
|
|
95
|
-
this.env.NODE_ENV = 'test';
|
|
96
|
-
|
|
97
|
-
if (flags['no-timeout']) {
|
|
98
|
-
flags.timeout = 0;
|
|
99
|
-
}
|
|
100
|
-
debug('run test: %s %o flags: %o', mochaFile, this.args, flags);
|
|
101
|
-
|
|
102
|
-
const mochaArgs = await this.formatMochaArgs();
|
|
103
|
-
if (!mochaArgs) return;
|
|
104
|
-
await this.runMocha(mochaFile, mochaArgs);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
protected async runMocha(mochaFile: string, mochaArgs: string[]) {
|
|
108
|
-
await this.forkNode(mochaFile, mochaArgs, {
|
|
109
|
-
execArgv: [
|
|
110
|
-
...process.execArgv,
|
|
111
|
-
// https://github.com/mochajs/mocha/issues/2640#issuecomment-1663388547
|
|
112
|
-
'--unhandled-rejections=strict',
|
|
113
|
-
],
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
protected async formatMochaArgs() {
|
|
118
|
-
const { args, flags } = this;
|
|
119
|
-
// collect require
|
|
120
|
-
const requires = await this.formatRequires();
|
|
121
|
-
const eggType = await detectType(flags.base);
|
|
122
|
-
debug('eggType: %s', eggType);
|
|
123
|
-
if (eggType === EggType.application) {
|
|
124
|
-
try {
|
|
125
|
-
const eggMockRegister = importResolve('@eggjs/mock/register', { paths: [ flags.base ] });
|
|
126
|
-
requires.push(eggMockRegister);
|
|
127
|
-
debug('auto register @eggjs/mock/register: %o', eggMockRegister);
|
|
128
|
-
} catch (err: any) {
|
|
129
|
-
// ignore @eggjs/mock not exists
|
|
130
|
-
debug('auto register @eggjs/mock fail, can not require @eggjs/mock on %o, error: %s',
|
|
131
|
-
flags.base, err.message);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// handle mochawesome enable
|
|
136
|
-
let reporter = this.env.TEST_REPORTER;
|
|
137
|
-
let reporterOptions = '';
|
|
138
|
-
if (!reporter && flags.mochawesome) {
|
|
139
|
-
// use https://github.com/node-modules/mochawesome/pull/1 instead
|
|
140
|
-
reporter = importResolve('mochawesome-with-mocha');
|
|
141
|
-
reporterOptions = 'reportDir=node_modules/.mochawesome-reports';
|
|
142
|
-
if (flags.parallel) {
|
|
143
|
-
// https://github.com/adamgruber/mochawesome#parallel-mode
|
|
144
|
-
requires.push(path.join(reporter, '../register.js'));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const ext = flags.typescript ? 'ts' : 'js';
|
|
149
|
-
let pattern = args.file ? args.file.split(',') : [];
|
|
150
|
-
// changed
|
|
151
|
-
if (flags.changed) {
|
|
152
|
-
pattern = await this.getChangedTestFiles(flags.base, ext);
|
|
153
|
-
if (!pattern.length) {
|
|
154
|
-
console.log('No changed test files');
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
debug('changed files: %o', pattern);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (!pattern.length && process.env.TESTS) {
|
|
161
|
-
pattern = process.env.TESTS.split(',');
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// collect test files when nothing is changed
|
|
165
|
-
if (!pattern.length) {
|
|
166
|
-
pattern = [ `test/**/*.test.${ext}` ];
|
|
167
|
-
}
|
|
168
|
-
pattern = pattern.concat([ '!test/fixtures', '!test/node_modules' ]);
|
|
169
|
-
|
|
170
|
-
// expand glob and skip node_modules and fixtures
|
|
171
|
-
let files = globby.sync(pattern, { cwd: flags.base });
|
|
172
|
-
files.sort();
|
|
173
|
-
|
|
174
|
-
if (files.length === 0) {
|
|
175
|
-
console.log('No test files found with pattern %o', pattern);
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// split up test files in parallel CI jobs
|
|
180
|
-
if (ciParallelVars) {
|
|
181
|
-
const { index: currentIndex, total: totalRuns } = ciParallelVars as { index: number, total: number };
|
|
182
|
-
const fileCount = files.length;
|
|
183
|
-
const each = Math.floor(fileCount / totalRuns);
|
|
184
|
-
const remainder = fileCount % totalRuns;
|
|
185
|
-
const offset = Math.min(currentIndex, remainder) + (currentIndex * each);
|
|
186
|
-
const currentFileCount = each + (currentIndex < remainder ? 1 : 0);
|
|
187
|
-
files = files.slice(offset, offset + currentFileCount);
|
|
188
|
-
console.log('# Split test files in parallel CI jobs: %d/%d, files: %d/%d',
|
|
189
|
-
currentIndex + 1, totalRuns, files.length, fileCount);
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// auto add setup file as the first test file
|
|
193
|
-
const setupFile = path.join(flags.base, `test/.setup.${ext}`);
|
|
194
|
-
try {
|
|
195
|
-
await fs.access(setupFile);
|
|
196
|
-
files.unshift(setupFile);
|
|
197
|
-
} catch {
|
|
198
|
-
// ignore
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
const grep = flags.grep ? flags.grep.split(',') : [];
|
|
202
|
-
|
|
203
|
-
return [
|
|
204
|
-
// force exit
|
|
205
|
-
'--exit',
|
|
206
|
-
flags.bail ? '--bail' : '',
|
|
207
|
-
grep.map(pattern => `--grep='${pattern}'`).join(' '),
|
|
208
|
-
flags.timeout ? `--timeout=${flags.timeout}` : '--no-timeout',
|
|
209
|
-
flags.parallel ? '--parallel' : '',
|
|
210
|
-
flags.parallel && flags.jobs ? `--jobs=${flags.jobs}` : '',
|
|
211
|
-
reporter ? `--reporter=${reporter}` : '',
|
|
212
|
-
reporterOptions ? `--reporter-options=${reporterOptions}` : '',
|
|
213
|
-
...requires.map(r => `--require=${r}`),
|
|
214
|
-
...files,
|
|
215
|
-
flags['dry-run'] ? '--dry-run' : '',
|
|
216
|
-
].filter(a => a.trim());
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
protected async getChangedTestFiles(dir: string, ext: string) {
|
|
220
|
-
const res = await getChangedFilesForRoots([ path.join(dir, 'test') ], {});
|
|
221
|
-
const changedFiles = res.changedFiles;
|
|
222
|
-
const files: string[] = [];
|
|
223
|
-
for (let cf of changedFiles) {
|
|
224
|
-
// only find test/**/*.test.(js|ts)
|
|
225
|
-
if (cf.endsWith(`.test.${ext}`)) {
|
|
226
|
-
// Patterns MUST use forward slashes (not backslashes)
|
|
227
|
-
// This should be converted on Windows
|
|
228
|
-
if (process.platform === 'win32') {
|
|
229
|
-
cf = cf.replace(/\\/g, '/');
|
|
230
|
-
}
|
|
231
|
-
files.push(cf);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
return files;
|
|
235
|
-
}
|
|
236
|
-
}
|
package/src/index.ts
DELETED
package/src/utils.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { fileURLToPath } from 'node:url';
|
|
4
|
-
|
|
5
|
-
export async function readPackageJSON(baseDir: string) {
|
|
6
|
-
const pkgFile = path.join(baseDir, 'package.json');
|
|
7
|
-
try {
|
|
8
|
-
const pkgJSON = await fs.readFile(pkgFile, 'utf8');
|
|
9
|
-
return JSON.parse(pkgJSON);
|
|
10
|
-
} catch {
|
|
11
|
-
return {};
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export async function hasTsConfig(baseDir: string) {
|
|
16
|
-
const pkgFile = path.join(baseDir, 'tsconfig.json');
|
|
17
|
-
try {
|
|
18
|
-
await fs.access(pkgFile);
|
|
19
|
-
return true;
|
|
20
|
-
} catch {
|
|
21
|
-
return false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function getSourceDirname() {
|
|
26
|
-
if (typeof __dirname === 'string') {
|
|
27
|
-
return __dirname;
|
|
28
|
-
}
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
30
|
-
// @ts-ignore
|
|
31
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
32
|
-
return path.dirname(__filename);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function getSourceFilename(filename: string) {
|
|
36
|
-
return path.join(getSourceDirname(), filename);
|
|
37
|
-
}
|