Skip to content

Commit

Permalink
chore(testrunner): complete ts migration (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 23, 2020
1 parent 224d3df commit 53ac35a
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 171 deletions.
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@babel/core": "^7.10.3",
"@babel/preset-env": "^7.10.3",
"@babel/preset-typescript": "^7.10.1",
"@types/babel__core": "^7.1.9",
"@types/debug": "0.0.31",
"@types/extract-zip": "^1.6.2",
"@types/mime": "^2.0.1",
Expand All @@ -63,6 +64,7 @@
"@types/progress": "^2.0.3",
"@types/proxy-from-env": "^1.0.0",
"@types/rimraf": "^2.0.2",
"@types/source-map-support": "^0.5.3",
"@types/stack-utils": "^1.0.1",
"@types/ws": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^2.6.1",
Expand Down
17 changes: 0 additions & 17 deletions test-runner/src/cli.js

This file was deleted.

15 changes: 8 additions & 7 deletions test-runner/src/expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import GoldenUtils from './GoldenUtils';
import { compare } from './golden';
import { RunnerConfig } from './runnerConfig';

declare global {
const expect: typeof import('expect');
Expand All @@ -28,16 +29,16 @@ declare module 'expect/build/types' {

global['expect'] = require('expect');

let relativeTestFile: string;
let testFile: string;

export function initializeImageMatcher(options) {
function toMatchImage(received, name, config) {
const { pass, message } = GoldenUtils.compare(received, name, { ...options, relativeTestFile, config });
export function initializeImageMatcher(config: RunnerConfig) {
function toMatchImage(received: Buffer, name: string, options?: { threshold?: number }) {
const { pass, message } = compare(received, name, config, testFile, options);
return { pass, message: () => message };
};
expect.extend({ toMatchImage });
}

export function setCurrentTestFile(testFile: string) {
relativeTestFile = testFile;
export function setCurrentTestFile(file: string) {
testFile = file;
}
55 changes: 26 additions & 29 deletions test-runner/src/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,29 @@
import debug from 'debug';
import { Test } from './test';

declare global {
interface WorkerState {
}

interface TestState {
}
type Scope = 'test' | 'worker';

interface FixtureParameters {
}
}
type FixtureRegistration = {
name: string;
scope: Scope;
fn: Function;
};

const registrations = new Map();
const registrationsByFile = new Map();
export let parameters: FixtureParameters = {} as FixtureParameters;
const registrations = new Map<string, FixtureRegistration>();
const registrationsByFile = new Map<string, FixtureRegistration[]>();
export let parameters: any = {};
export const parameterRegistrations = new Map();

export function setParameters(params: any) {
parameters = Object.assign(parameters, params);
for (const name of Object.keys(params))
registerWorkerFixture(name as keyof WorkerState, async ({}, test) => await test(parameters[name] as never));
registerWorkerFixture(name, async ({}, test) => await test(parameters[name]));
}

class Fixture<Config> {
pool: FixturePool<Config>;
name: string;
scope: string;
scope: Scope;
fn: Function;
deps: string[];
usages: Set<string>;
Expand All @@ -53,7 +50,7 @@ class Fixture<Config> {
_setup = false;
_teardown = false;

constructor(pool: FixturePool<Config>, name: string, scope: string, fn: any) {
constructor(pool: FixturePool<Config>, name: string, scope: Scope, fn: any) {
this.pool = pool;
this.name = name;
this.scope = scope;
Expand Down Expand Up @@ -137,7 +134,7 @@ export class FixturePool<Config> {
}
}

async resolveParametersAndRun(fn: (arg0: {}) => any, timeout: number, config: Config, test?: Test) {
async resolveParametersAndRun(fn: Function, timeout: number, config: Config, test?: Test) {
const names = fixtureParameterNames(fn);
for (const name of names)
await this.setupFixture(name, config, test);
Expand All @@ -148,7 +145,7 @@ export class FixturePool<Config> {
if (!timeout)
return fn(params);

let timer;
let timer: NodeJS.Timer;
let timerPromise = new Promise(f => timer = setTimeout(f, timeout));
return Promise.race([
Promise.resolve(fn(params)).then(() => clearTimeout(timer)),
Expand All @@ -169,9 +166,9 @@ export class FixturePool<Config> {
}
}

export function fixturesForCallback(callback: any): string[] {
export function fixturesForCallback(callback: Function): string[] {
const names = new Set<string>();
const visit = (callback: any) => {
const visit = (callback: Function) => {
for (const name of fixtureParameterNames(callback)) {
if (name in names)
continue;
Expand All @@ -189,7 +186,7 @@ export function fixturesForCallback(callback: any): string[] {
return result;
}

function fixtureParameterNames(fn: { toString: () => any; }): string[] {
function fixtureParameterNames(fn: Function): string[] {
const text = fn.toString();
const match = text.match(/async(?:\s+function)?\s*\(\s*{\s*([^}]*)\s*}/);
if (!match || !match[1].trim())
Expand All @@ -198,7 +195,7 @@ function fixtureParameterNames(fn: { toString: () => any; }): string[] {
return signature.split(',').map((t: string) => t.trim());
}

function innerRegisterFixture(name: string, scope: string, fn: Function, caller: Function) {
function innerRegisterFixture(name: string, scope: Scope, fn: Function, caller: Function) {
const obj = {stack: ''};
Error.captureStackTrace(obj, caller);
const stackFrame = obj.stack.split('\n')[2];
Expand All @@ -211,20 +208,20 @@ function innerRegisterFixture(name: string, scope: string, fn: Function, caller:
registrationsByFile.get(file).push(registration);
};

export function registerFixture<Config, T extends keyof TestState>(name: T, fn: (params: FixtureParameters & WorkerState & TestState, runTest: (arg: TestState[T]) => Promise<void>, config: Config, test: Test) => Promise<void>) {
export function registerFixture<Config>(name: string, fn: (params: any, runTest: (arg: any) => Promise<void>, config: Config, test: Test) => Promise<void>) {
innerRegisterFixture(name, 'test', fn, registerFixture);
};

export function registerWorkerFixture<Config, T extends keyof (WorkerState & FixtureParameters)>(name: T, fn: (params: FixtureParameters & WorkerState, runTest: (arg: (WorkerState & FixtureParameters)[T]) => Promise<void>, config: Config) => Promise<void>) {
export function registerWorkerFixture<Config>(name: string, fn: (params: any, runTest: (arg: any) => Promise<void>, config: Config) => Promise<void>) {
innerRegisterFixture(name, 'worker', fn, registerWorkerFixture);
};

export function registerParameter<T extends keyof WorkerState>(name: T, fn: () => WorkerState[T][]) {
registerWorkerFixture(name, async ({}: any, test: (arg0: any) => any) => await test(parameters[name]));
export function registerParameter(name: string, fn: () => any) {
registerWorkerFixture(name, async ({}: any, test: Function) => await test(parameters[name]));
parameterRegistrations.set(name, fn);
}

function collectRequires(file: string | number, result: Set<unknown>) {
function collectRequires(file: string, result: Set<string>) {
if (result.has(file))
return;
result.add(file);
Expand All @@ -236,8 +233,8 @@ function collectRequires(file: string | number, result: Set<unknown>) {
collectRequires(dep, result);
}

export function lookupRegistrations(file: any, scope: any) {
const deps = new Set();
export function lookupRegistrations(file: string, scope: Scope) {
const deps = new Set<string>();
collectRequires(file, deps);
const allDeps = [...deps].reverse();
let result = new Map();
Expand All @@ -254,7 +251,7 @@ export function lookupRegistrations(file: any, scope: any) {
return result;
}

export function rerunRegistrations(file: any, scope: any) {
export function rerunRegistrations(file: string, scope: Scope) {
// When we are running several tests in the same worker, we should re-run registrations before
// each file. That way we erase potential fixture overrides from the previous test runs.
for (const registration of lookupRegistrations(file, scope).values())
Expand Down
Loading

0 comments on commit 53ac35a

Please sign in to comment.