Skip to content

Commit 6d77ddd

Browse files
authored
feat(testplane): add beforeAll/afterAll hooks
1 parent 69836ca commit 6d77ddd

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

src/config/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ module.exports = {
4242
compositeImage: true,
4343
prepareBrowser: null,
4444
prepareEnvironment: null,
45+
beforeAll: null,
46+
afterAll: null,
4547
waitTimeout: 3000,
4648
waitInterval: 500,
4749
httpTimeout: 30000,

src/config/options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const rootSection = section(
1616
browsers: map(section(browserOptions.getPerBrowser())),
1717

1818
prepareEnvironment: options.optionalFunction("prepareEnvironment"),
19+
beforeAll: options.optionalFunction("beforeAll"),
20+
afterAll: options.optionalFunction("afterAll"),
1921

2022
system: section({
2123
debug: options.boolean("debug"),

src/config/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { BrowserTestRunEnvOptions } from "../runner/browser-env/vite/types"
33
import type { Test } from "../types";
44
import type { ChildProcessWithoutNullStreams } from "child_process";
55
import type { RequestOptions } from "https";
6+
import type { Config } from "./index";
67

78
export interface CompareOptsConfig {
89
shouldCluster: boolean;
@@ -379,19 +380,25 @@ type PartialCommonConfig = Partial<
379380
devServer?: Partial<CommonConfig["devServer"]>;
380381
};
381382

383+
export type HookType = (params: { config: Config }) => Promise<void> | undefined;
384+
382385
// Only browsers desiredCapabilities are required in input config
383386
export type ConfigInput = Partial<PartialCommonConfig> & {
384387
browsers: Record<string, PartialCommonConfig & { desiredCapabilities: WebdriverIO.Capabilities }>;
385388
plugins?: Record<string, unknown>;
386389
sets?: Record<string, SetsConfig>;
387390
prepareEnvironment?: () => void | null;
391+
beforeAll?: HookType;
392+
afterAll?: HookType;
388393
};
389394

390395
export interface ConfigParsed extends CommonConfig {
391396
browsers: Record<string, BrowserConfig>;
392397
plugins: Record<string, Record<string, unknown>>;
393398
sets: Record<string, SetsConfigParsed>;
394399
prepareEnvironment?: () => void | null;
400+
beforeAll?: HookType;
401+
afterAll?: HookType;
395402
}
396403

397404
export interface RuntimeConfig {

src/testplane.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,19 @@ export class Testplane extends BaseTestplane {
153153

154154
preloadWebdriverIO();
155155

156+
if (this.config.beforeAll) {
157+
await this.config.beforeAll.call({ config: this.config }, { config: this.config });
158+
}
159+
156160
await runner.run(
157161
await this._readTests(testPaths, { browsers, sets, grep, replMode, keepBrowserMode }),
158162
RunnerStats.create(this),
159163
);
160164

165+
if (this.config.afterAll) {
166+
await this.config.afterAll.call({ config: this.config }, { config: this.config });
167+
}
168+
161169
return !this.isFailed();
162170
}
163171

test/src/config/options.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,58 @@ describe("config options", () => {
627627
});
628628
});
629629

630+
describe("hooks beforeAll/afterAll", () => {
631+
it("should throw error if beforeAll is not a null or function", () => {
632+
const readConfig = { beforeAll: "String" };
633+
634+
Config.read.returns(readConfig);
635+
636+
assert.throws(() => createConfig(), Error, '"beforeAll" must be a function');
637+
});
638+
639+
it("should set default beforeAll option if it does not set in config file", () => {
640+
const config = createConfig();
641+
642+
assert.equal(config.beforeAll, defaults.beforeAll);
643+
});
644+
645+
it("should override beforeAll option", () => {
646+
const newFunc = () => {};
647+
const readConfig = { beforeAll: newFunc };
648+
649+
Config.read.returns(readConfig);
650+
651+
const config = createConfig();
652+
653+
assert.deepEqual(config.beforeAll, newFunc);
654+
});
655+
656+
it("should throw error if afterAll is not a null or function", () => {
657+
const readConfig = { afterAll: "String" };
658+
659+
Config.read.returns(readConfig);
660+
661+
assert.throws(() => createConfig(), Error, '"afterAll" must be a function');
662+
});
663+
664+
it("should set default afterAll option if it does not set in config file", () => {
665+
const config = createConfig();
666+
667+
assert.equal(config.afterAll, defaults.afterAll);
668+
});
669+
670+
it("should override afterAll option", () => {
671+
const newFunc = () => {};
672+
const readConfig = { afterAll: newFunc };
673+
674+
Config.read.returns(readConfig);
675+
676+
const config = createConfig();
677+
678+
assert.deepEqual(config.afterAll, newFunc);
679+
});
680+
});
681+
630682
describe("plugins", () => {
631683
it("should parse boolean value from environment", () => {
632684
const result = parse_({

0 commit comments

Comments
 (0)