2020-08-21 01:04:27 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright 2019 Google Inc. All rights reserved.
|
|
|
|
|
* Modifications copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
2020-08-22 02:14:11 +02:00
|
|
|
|
2020-08-23 17:22:14 +02:00
|
|
|
import * as fs from 'fs';
|
|
|
|
|
import * as path from 'path';
|
2020-08-21 01:04:27 +02:00
|
|
|
import './builtin.fixtures';
|
2020-08-21 18:53:02 +02:00
|
|
|
import './expect';
|
2020-08-23 01:44:56 +02:00
|
|
|
import { registerFixture as registerFixtureT, registerWorkerFixture as registerWorkerFixtureT } from './fixtures';
|
2020-08-23 17:22:14 +02:00
|
|
|
import { reporters } from './reporters';
|
|
|
|
|
import { Runner } from './runner';
|
2020-08-23 01:44:56 +02:00
|
|
|
import { RunnerConfig } from './runnerConfig';
|
2020-08-23 17:22:14 +02:00
|
|
|
import { Suite, Test } from './test';
|
|
|
|
|
import { Matrix, TestCollector } from './testCollector';
|
|
|
|
|
import { installTransform } from './transform';
|
2020-08-23 01:44:56 +02:00
|
|
|
export { parameters, registerParameter } from './fixtures';
|
2020-08-23 17:22:14 +02:00
|
|
|
export { RunnerConfig } from './runnerConfig';
|
|
|
|
|
export { Suite, Test } from './test';
|
|
|
|
|
|
|
|
|
|
let beforeFunctions: Function[] = [];
|
|
|
|
|
let afterFunctions: Function[] = [];
|
|
|
|
|
let matrix: Matrix = {};
|
|
|
|
|
|
|
|
|
|
global['before'] = (fn: Function) => beforeFunctions.push(fn);
|
|
|
|
|
global['after'] = (fn: Function) => afterFunctions.push(fn);
|
|
|
|
|
global['matrix'] = (m: Matrix) => matrix = m;
|
2020-08-23 01:44:56 +02:00
|
|
|
|
|
|
|
|
export function registerFixture<T extends keyof TestState>(name: T, fn: (params: FixtureParameters & WorkerState & TestState, runTest: (arg: TestState[T]) => Promise<void>, config: RunnerConfig, test: Test) => Promise<void>) {
|
|
|
|
|
registerFixtureT<RunnerConfig, T>(name, fn);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function registerWorkerFixture<T extends keyof (WorkerState & FixtureParameters)>(name: T, fn: (params: FixtureParameters & WorkerState, runTest: (arg: (WorkerState & FixtureParameters)[T]) => Promise<void>, config: RunnerConfig) => Promise<void>) {
|
|
|
|
|
registerWorkerFixtureT<RunnerConfig, T>(name, fn);
|
|
|
|
|
};
|
2020-08-23 17:22:14 +02:00
|
|
|
|
|
|
|
|
export function collectTests(config: RunnerConfig, files: string[]): Suite {
|
|
|
|
|
const revertBabelRequire = installTransform();
|
|
|
|
|
let hasSetup = false;
|
|
|
|
|
try {
|
|
|
|
|
hasSetup = fs.statSync(path.join(config.testDir, 'setup.js')).isFile();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
hasSetup = hasSetup || fs.statSync(path.join(config.testDir, 'setup.ts')).isFile();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
}
|
|
|
|
|
if (hasSetup)
|
|
|
|
|
require(path.join(config.testDir, 'setup'));
|
|
|
|
|
revertBabelRequire();
|
|
|
|
|
|
|
|
|
|
const testCollector = new TestCollector(files, matrix, config);
|
|
|
|
|
return testCollector.suite;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function runTests(config: RunnerConfig, suite: Suite, reporterFactory: any) {
|
|
|
|
|
// Trial run does not need many workers, use one.
|
|
|
|
|
const jobs = (config.trialRun || config.debug) ? 1 : config.jobs;
|
|
|
|
|
const runner = new Runner(suite, { ...config, jobs });
|
|
|
|
|
new reporterFactory(runner);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
for (const f of beforeFunctions)
|
|
|
|
|
await f();
|
|
|
|
|
await runner.run();
|
|
|
|
|
await runner.stop();
|
|
|
|
|
} finally {
|
|
|
|
|
for (const f of afterFunctions)
|
|
|
|
|
await f();
|
|
|
|
|
}
|
|
|
|
|
}
|