forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.ts
126 lines (101 loc) · 3.72 KB
/
run.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
// Run Jest tests
//
// Provides Jest with `--config` to the first jest.config.js file found in the current
// directory, or while going up in the directory chain. If the current working directory
// is nested under the config path, a pattern will be provided to Jest to only run the
// tests within that directory.
//
// Any additional options passed will be forwarded to Jest.
//
// See all cli options in https://facebook.github.io/jest/docs/cli.html
import { resolve, relative, sep as osSep } from 'path';
import { existsSync } from 'fs';
import { run } from 'jest';
import { buildArgv } from 'jest-cli/build/cli';
import { ToolingLog, getTimeReporter } from '@kbn/dev-utils';
import { map } from 'lodash';
// yarn test:jest src/core/server/saved_objects
// yarn test:jest src/core/public/core_system.test.ts
// :kibana/src/core/server/saved_objects yarn test:jest
// Patch node 16 types to be compatible with jest 26
// https://github.com/facebook/jest/issues/11640#issuecomment-893867514
/* eslint-disable */
declare global {
namespace NodeJS {
interface Global {}
interface InspectOptions {}
interface ConsoleConstructor
extends console.ConsoleConstructor {}
}
}
/* eslint-enable */
export function runJest(configName = 'jest.config.js') {
const argv = buildArgv(process.argv);
const log = new ToolingLog({
level: argv.verbose ? 'verbose' : 'info',
writeTo: process.stdout,
});
const runStartTime = Date.now();
const reportTime = getTimeReporter(log, 'scripts/jest');
let cwd: string;
let testFiles: string[];
if (!argv.config) {
cwd = process.env.INIT_CWD || process.cwd();
testFiles = argv._.splice(2).map((p) => resolve(cwd, p));
const commonTestFiles = commonBasePath(testFiles);
const testFilesProvided = testFiles.length > 0;
log.verbose('cwd:', cwd);
log.verbose('testFiles:', testFiles.join(', '));
log.verbose('commonTestFiles:', commonTestFiles);
let configPath;
// sets the working directory to the cwd or the common
// base directory of the provided test files
let wd = testFilesProvided ? commonTestFiles : cwd;
configPath = resolve(wd, configName);
while (!existsSync(configPath)) {
wd = resolve(wd, '..');
configPath = resolve(wd, configName);
}
log.verbose(`no config provided, found ${configPath}`);
process.argv.push('--config', configPath);
if (!testFilesProvided) {
log.verbose(`no test files provided, setting to current directory`);
process.argv.push(relative(wd, cwd));
}
log.info('yarn jest', process.argv.slice(2).join(' '));
}
if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}
run().then(() => {
// Success means that tests finished, doesn't mean they passed.
reportTime(runStartTime, 'total', {
success: true,
isXpack: cwd.includes('x-pack'),
testFiles: map(testFiles, (testFile) => relative(cwd, testFile)),
});
});
}
/**
* Finds the common basePath by sorting the array
* and comparing the first and last element
*/
export function commonBasePath(paths: string[] = [], sep = osSep) {
if (paths.length === 0) return '';
paths = paths.concat().sort();
const first = paths[0].split(sep);
const last = paths[paths.length - 1].split(sep);
const length = first.length;
let i = 0;
while (i < length && first[i] === last[i]) {
i++;
}
return first.slice(0, i).join(sep);
}