Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/config/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module.exports = {
compositeImage: true,
prepareBrowser: null,
prepareEnvironment: null,
beforeAll: null,
afterAll: null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where I can find docs about this new methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs will be here gemini-testing/testplane-docs#89 with docs for saveState/restoreState

waitTimeout: 3000,
waitInterval: 500,
httpTimeout: 30000,
Expand Down
2 changes: 2 additions & 0 deletions src/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const rootSection = section(
browsers: map(section(browserOptions.getPerBrowser())),

prepareEnvironment: options.optionalFunction("prepareEnvironment"),
beforeAll: options.optionalFunction("beforeAll"),
afterAll: options.optionalFunction("afterAll"),

system: section({
debug: options.boolean("debug"),
Expand Down
7 changes: 7 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { BrowserTestRunEnvOptions } from "../runner/browser-env/vite/types"
import type { Test } from "../types";
import type { ChildProcessWithoutNullStreams } from "child_process";
import type { RequestOptions } from "https";
import type { Config } from "./index";

export interface CompareOptsConfig {
shouldCluster: boolean;
Expand Down Expand Up @@ -379,19 +380,25 @@ type PartialCommonConfig = Partial<
devServer?: Partial<CommonConfig["devServer"]>;
};

export type HookType = (params: { config: Config }) => Promise<void> | undefined;

// Only browsers desiredCapabilities are required in input config
export type ConfigInput = Partial<PartialCommonConfig> & {
browsers: Record<string, PartialCommonConfig & { desiredCapabilities: WebdriverIO.Capabilities }>;
plugins?: Record<string, unknown>;
sets?: Record<string, SetsConfig>;
prepareEnvironment?: () => void | null;
beforeAll?: HookType;
afterAll?: HookType;
};

export interface ConfigParsed extends CommonConfig {
browsers: Record<string, BrowserConfig>;
plugins: Record<string, Record<string, unknown>>;
sets: Record<string, SetsConfigParsed>;
prepareEnvironment?: () => void | null;
beforeAll?: HookType;
afterAll?: HookType;
}

export interface RuntimeConfig {
Expand Down
8 changes: 8 additions & 0 deletions src/testplane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,19 @@ export class Testplane extends BaseTestplane {

preloadWebdriverIO();

if (this.config.beforeAll) {
await this.config.beforeAll.call({ config: this.config }, { config: this.config });
}

await runner.run(
await this._readTests(testPaths, { browsers, sets, grep, replMode, keepBrowserMode }),
RunnerStats.create(this),
);

if (this.config.afterAll) {
await this.config.afterAll.call({ config: this.config }, { config: this.config });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we didn't write tests for this code?

}

return !this.isFailed();
}

Expand Down
52 changes: 52 additions & 0 deletions test/src/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,58 @@ describe("config options", () => {
});
});

describe("hooks beforeAll/afterAll", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, it was possible to simply reduce duplication:

["beforeAll", "afterAll"].forEach(hookName => {
  describe(`hook ${hookName}`, () => {
    it('...', () => {
      const readConfig = { [hookName]: "String" };
 
      Config.read.returns(readConfig);

      assert.throws(() => createConfig(), Error, `"${hookName}" must be a function`);
    });

    // ...
  });
});

it("should throw error if beforeAll is not a null or function", () => {
const readConfig = { beforeAll: "String" };

Config.read.returns(readConfig);

assert.throws(() => createConfig(), Error, '"beforeAll" must be a function');
});

it("should set default beforeAll option if it does not set in config file", () => {
const config = createConfig();

assert.equal(config.beforeAll, defaults.beforeAll);
});

it("should override beforeAll option", () => {
const newFunc = () => {};
const readConfig = { beforeAll: newFunc };

Config.read.returns(readConfig);

const config = createConfig();

assert.deepEqual(config.beforeAll, newFunc);
});

it("should throw error if afterAll is not a null or function", () => {
const readConfig = { afterAll: "String" };

Config.read.returns(readConfig);

assert.throws(() => createConfig(), Error, '"afterAll" must be a function');
});

it("should set default afterAll option if it does not set in config file", () => {
const config = createConfig();

assert.equal(config.afterAll, defaults.afterAll);
});

it("should override afterAll option", () => {
const newFunc = () => {};
const readConfig = { afterAll: newFunc };

Config.read.returns(readConfig);

const config = createConfig();

assert.deepEqual(config.afterAll, newFunc);
});
});

describe("plugins", () => {
it("should parse boolean value from environment", () => {
const result = parse_({
Expand Down
Loading