2021-06-07 02:09:53 +02:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { expect } from './expect';
|
|
|
|
|
import { currentlyLoadingFileSuite, currentTestInfo, setCurrentlyLoadingFileSuite } from './globals';
|
|
|
|
|
import { Spec, Suite } from './test';
|
2021-06-23 19:30:54 +02:00
|
|
|
import { wrapFunctionWithLocation } from './transform';
|
2021-07-03 00:49:05 +02:00
|
|
|
import { Fixtures, FixturesWithLocation, Location, TestType } from './types';
|
2021-06-07 02:09:53 +02:00
|
|
|
|
|
|
|
|
const countByFile = new Map<string, number>();
|
|
|
|
|
|
|
|
|
|
export class DeclaredFixtures {
|
|
|
|
|
testType!: TestTypeImpl;
|
|
|
|
|
location!: Location;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class TestTypeImpl {
|
|
|
|
|
readonly fixtures: (FixturesWithLocation | DeclaredFixtures)[];
|
|
|
|
|
readonly test: TestType<any, any>;
|
|
|
|
|
|
|
|
|
|
constructor(fixtures: (FixturesWithLocation | DeclaredFixtures)[]) {
|
|
|
|
|
this.fixtures = fixtures;
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
const test: any = wrapFunctionWithLocation(this._spec.bind(this, 'default'));
|
2021-06-07 02:09:53 +02:00
|
|
|
test.expect = expect;
|
2021-06-23 19:30:54 +02:00
|
|
|
test.only = wrapFunctionWithLocation(this._spec.bind(this, 'only'));
|
|
|
|
|
test.describe = wrapFunctionWithLocation(this._describe.bind(this, 'default'));
|
|
|
|
|
test.describe.only = wrapFunctionWithLocation(this._describe.bind(this, 'only'));
|
|
|
|
|
test.beforeEach = wrapFunctionWithLocation(this._hook.bind(this, 'beforeEach'));
|
|
|
|
|
test.afterEach = wrapFunctionWithLocation(this._hook.bind(this, 'afterEach'));
|
|
|
|
|
test.beforeAll = wrapFunctionWithLocation(this._hook.bind(this, 'beforeAll'));
|
|
|
|
|
test.afterAll = wrapFunctionWithLocation(this._hook.bind(this, 'afterAll'));
|
|
|
|
|
test.skip = wrapFunctionWithLocation(this._modifier.bind(this, 'skip'));
|
|
|
|
|
test.fixme = wrapFunctionWithLocation(this._modifier.bind(this, 'fixme'));
|
|
|
|
|
test.fail = wrapFunctionWithLocation(this._modifier.bind(this, 'fail'));
|
|
|
|
|
test.slow = wrapFunctionWithLocation(this._modifier.bind(this, 'slow'));
|
2021-06-07 02:09:53 +02:00
|
|
|
test.setTimeout = this._setTimeout.bind(this);
|
2021-06-23 19:30:54 +02:00
|
|
|
test.use = wrapFunctionWithLocation(this._use.bind(this));
|
|
|
|
|
test.extend = wrapFunctionWithLocation(this._extend.bind(this));
|
|
|
|
|
test.declare = wrapFunctionWithLocation(this._declare.bind(this));
|
2021-06-07 02:09:53 +02:00
|
|
|
this.test = test;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _spec(type: 'default' | 'only', location: Location, title: string, fn: Function) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (!suite)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw new Error(`test() can only be called in a test file`);
|
2021-06-07 02:09:53 +02:00
|
|
|
|
2021-06-21 20:25:15 +02:00
|
|
|
const ordinalInFile = countByFile.get(suite._requireFile) || 0;
|
|
|
|
|
countByFile.set(suite._requireFile, ordinalInFile + 1);
|
2021-06-07 02:09:53 +02:00
|
|
|
|
|
|
|
|
const spec = new Spec(title, fn, ordinalInFile, this);
|
2021-06-21 20:25:15 +02:00
|
|
|
spec._requireFile = suite._requireFile;
|
2021-06-07 02:09:53 +02:00
|
|
|
spec.file = location.file;
|
|
|
|
|
spec.line = location.line;
|
|
|
|
|
spec.column = location.column;
|
|
|
|
|
suite._addSpec(spec);
|
|
|
|
|
|
|
|
|
|
if (type === 'only')
|
|
|
|
|
spec._only = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _describe(type: 'default' | 'only', location: Location, title: string, fn: Function) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (!suite)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw new Error(`describe() can only be called in a test file`);
|
2021-06-07 02:09:53 +02:00
|
|
|
|
|
|
|
|
const child = new Suite(title);
|
2021-06-21 20:25:15 +02:00
|
|
|
child._requireFile = suite._requireFile;
|
|
|
|
|
child.file = location.file;
|
2021-06-07 02:09:53 +02:00
|
|
|
child.line = location.line;
|
|
|
|
|
child.column = location.column;
|
|
|
|
|
suite._addSuite(child);
|
|
|
|
|
|
|
|
|
|
if (type === 'only')
|
|
|
|
|
child._only = true;
|
|
|
|
|
|
|
|
|
|
setCurrentlyLoadingFileSuite(child);
|
|
|
|
|
fn();
|
|
|
|
|
setCurrentlyLoadingFileSuite(suite);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _hook(name: 'beforeEach' | 'afterEach' | 'beforeAll' | 'afterAll', location: Location, fn: Function) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (!suite)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw new Error(`${name} hook can only be called in a test file`);
|
|
|
|
|
suite._hooks.push({ type: name, fn, location });
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
2021-07-03 00:49:05 +02:00
|
|
|
private _modifier(type: 'skip' | 'fail' | 'fixme' | 'slow', location: Location, ...modifierArgs: [arg?: any | Function, description?: string]) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (suite) {
|
2021-07-03 00:49:05 +02:00
|
|
|
if (typeof modifierArgs[0] === 'function') {
|
|
|
|
|
suite._modifiers.push({ type, fn: modifierArgs[0], location, description: modifierArgs[1] });
|
2021-06-07 02:09:53 +02:00
|
|
|
} else {
|
2021-07-03 00:49:05 +02:00
|
|
|
if (modifierArgs.length >= 1 && !modifierArgs[0])
|
|
|
|
|
return;
|
|
|
|
|
const description = modifierArgs[1];
|
|
|
|
|
suite._annotations.push({ type, description });
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
|
if (!testInfo)
|
|
|
|
|
throw new Error(`test.${type}() can only be called inside test, describe block or fixture`);
|
2021-07-03 00:49:05 +02:00
|
|
|
if (typeof modifierArgs[0] === 'function')
|
2021-06-07 02:09:53 +02:00
|
|
|
throw new Error(`test.${type}() with a function can only be called inside describe block`);
|
2021-07-03 00:49:05 +02:00
|
|
|
testInfo[type](...modifierArgs as [any, any]);
|
2021-06-07 02:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _setTimeout(timeout: number) {
|
2021-06-29 22:33:13 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (suite) {
|
|
|
|
|
suite._timeout = timeout;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 02:09:53 +02:00
|
|
|
const testInfo = currentTestInfo();
|
|
|
|
|
if (!testInfo)
|
2021-06-29 22:33:13 +02:00
|
|
|
throw new Error(`test.setTimeout() can only be called from a test file`);
|
2021-06-07 02:09:53 +02:00
|
|
|
testInfo.setTimeout(timeout);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _use(location: Location, fixtures: Fixtures) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const suite = currentlyLoadingFileSuite();
|
|
|
|
|
if (!suite)
|
2021-06-23 19:30:54 +02:00
|
|
|
throw new Error(`test.use() can only be called in a test file`);
|
2021-06-07 02:09:53 +02:00
|
|
|
suite._fixtureOverrides = { ...suite._fixtureOverrides, ...fixtures };
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _extend(location: Location, fixtures: Fixtures) {
|
|
|
|
|
const fixturesWithLocation = { fixtures, location };
|
2021-06-07 02:09:53 +02:00
|
|
|
return new TestTypeImpl([...this.fixtures, fixturesWithLocation]).test;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 19:30:54 +02:00
|
|
|
private _declare(location: Location) {
|
2021-06-07 02:09:53 +02:00
|
|
|
const declared = new DeclaredFixtures();
|
2021-06-23 19:30:54 +02:00
|
|
|
declared.location = location;
|
2021-06-07 02:09:53 +02:00
|
|
|
const child = new TestTypeImpl([...this.fixtures, declared]);
|
|
|
|
|
declared.testType = child;
|
|
|
|
|
return child.test;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const rootTestType = new TestTypeImpl([]);
|