[jest] try running unit tests in parallel (#130823)

This commit is contained in:
Spencer 2022-04-21 16:37:06 -05:00 committed by GitHub
parent 47918ae0be
commit fa7323769d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 8 deletions

View file

@ -10,10 +10,17 @@ JOB_COUNT=$BUILDKITE_PARALLEL_JOB_COUNT
i=0
exitCode=0
# run unit tests in parallel
if [[ "$1" == 'jest.config.js' ]]; then
parallelism="-w2"
else
parallelism="--runInBand"
fi
while read -r config; do
if [ "$((i % JOB_COUNT))" -eq "$JOB" ]; then
echo "--- $ node scripts/jest --config $config"
node --max-old-space-size=14336 ./scripts/jest --config="$config" --runInBand --coverage=false --passWithNoTests
node --max-old-space-size=14336 ./scripts/jest --config="$config" "$parallelism" --coverage=false --passWithNoTests
lastCode=$?
if [ $lastCode -ne 0 ]; then

View file

@ -72,6 +72,7 @@ const defaultOptions: Options = {
processExit$,
sigint$,
sigterm$,
forceColor: true,
};
expect.addSnapshotSerializer(extendedEnvSerializer);
@ -80,7 +81,6 @@ beforeEach(() => {
jest.clearAllMocks();
log.messages.length = 0;
process.execArgv = ['--inheritted', '--exec', '--argv'];
process.env.FORCE_COLOR = process.env.FORCE_COLOR || '1';
currentProc = undefined;
});

View file

@ -34,6 +34,7 @@ export interface Options {
sigint$?: Rx.Observable<void>;
sigterm$?: Rx.Observable<void>;
mapLogLine?: DevServer['mapLogLine'];
forceColor?: boolean;
}
export class DevServer {
@ -50,6 +51,7 @@ export class DevServer {
private readonly argv: string[];
private readonly gracefulTimeout: number;
private readonly mapLogLine?: (line: string) => string | null;
private readonly forceColor: boolean;
constructor(options: Options) {
this.log = options.log;
@ -62,6 +64,7 @@ export class DevServer {
this.sigint$ = options.sigint$ ?? Rx.fromEvent<void>(process, 'SIGINT');
this.sigterm$ = options.sigterm$ ?? Rx.fromEvent<void>(process, 'SIGTERM');
this.mapLogLine = options.mapLogLine;
this.forceColor = options.forceColor ?? !!process.stdout.isTTY;
}
isReady$() {
@ -141,8 +144,13 @@ export class DevServer {
})
);
const serverOptions = {
script: this.script,
argv: this.argv,
forceColor: this.forceColor,
};
const runServer = () =>
usingServerProcess(this.script, this.argv, (proc) => {
usingServerProcess(serverOptions, (proc) => {
this.phase$.next('starting');
this.ready$.next(false);

View file

@ -18,14 +18,19 @@ interface ProcResource extends Rx.Unsubscribable {
unsubscribe(): void;
}
interface Options {
script: string;
argv: string[];
forceColor: boolean;
}
export function usingServerProcess<T>(
script: string,
argv: string[],
options: Options,
fn: (proc: execa.ExecaChildProcess) => Rx.Observable<T>
) {
return Rx.using(
(): ProcResource => {
const proc = execa.node(script, argv, {
const proc = execa.node(options.script, options.argv, {
stdio: 'pipe',
nodeOptions: [
...process.execArgv,
@ -36,7 +41,7 @@ export function usingServerProcess<T>(
NODE_OPTIONS: process.env.NODE_OPTIONS,
isDevCliChild: 'true',
ELASTIC_APM_SERVICE_NAME: 'kibana',
...(process.stdout.isTTY ? { FORCE_COLOR: 'true' } : {}),
...(options.forceColor ? { FORCE_COLOR: 'true' } : {}),
},
});

View file

@ -14,7 +14,7 @@ describe('encryption key configuration', () => {
let encryptionConfig = null;
beforeEach(() => {
jest.spyOn(fs, 'readFileSync').mockReturnValue('xpack.security.encryptionKey: foo');
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('xpack.security.encryptionKey: foo');
jest.spyOn(crypto, 'randomBytes').mockReturnValue('random-key');
encryptionConfig = new EncryptionConfig();
});