Skip to content
Open
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
16 changes: 16 additions & 0 deletions src/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {
GlobalAfterEachType,
GlobalBeforeEachType,
GlobalDescribeType,
GlobalHermioneType,
GlobalItType,
GlobalTestplaneType,
TestplaneGlobals,
} from "./types/globals";

export const it: GlobalItType = (globalThis as unknown as TestplaneGlobals).it;
export const describe: GlobalDescribeType = (globalThis as unknown as TestplaneGlobals).describe;
export const beforeEach: GlobalBeforeEachType = (globalThis as unknown as TestplaneGlobals).beforeEach;
export const afterEach: GlobalAfterEachType = (globalThis as unknown as TestplaneGlobals).afterEach;
export const testplane: GlobalTestplaneType = (globalThis as unknown as TestplaneGlobals).testplane;
export const hermione: GlobalHermioneType = (globalThis as unknown as TestplaneGlobals).hermione;
Comment on lines +11 to +16
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Types of globals are exported instead of being declared

So user could type:

import {it, describe} from "testplane";

Also same exported types will be used in "@testplane/globals"

23 changes: 1 addition & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import "./browser/types";
// Declares global expect function
import "expect-webdriverio";

import { GlobalHelper } from "./types";
export { run as runCli } from "./cli";
export { Testplane as default } from "./testplane";
export { Key } from "@testplane/webdriverio";
export * from "./mock";
export * from "./globals";

export * as unstable from "./unstable";

Expand Down Expand Up @@ -46,24 +46,3 @@ export type { SaveStateData } from "./browser/commands/saveState";

import type { TestDefinition, SuiteDefinition, TestHookDefinition } from "./test-reader/test-object/types";
export type { TestDefinition, SuiteDefinition, TestHookDefinition };

declare global {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Now we dont pollute global by default

/* eslint-disable no-var */
// Here, we ignore clashes of types between Mocha and Testplane, because in production we don't include @types/mocha,
// but we need mocha types in development, so this is an issue only during development.
///@ts-expect-error: see explanation above
var it: TestDefinition;
// @ts-expect-error: see explanation above
var describe: SuiteDefinition;
// @ts-expect-error: see explanation above
var beforeEach: TestHookDefinition;
// @ts-expect-error: see explanation above
var afterEach: TestHookDefinition;

var testplane: GlobalHelper;
/**
* @deprecated Use `testplane` instead
*/
var hermione: GlobalHelper;
/* eslint-enable no-var */
}
5 changes: 3 additions & 2 deletions src/test-reader/test-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { isRunInBrowserEnv } from "../utils/config";
import { BrowserConfig } from "../config/browser-config";
import type { ReadTestsOpts } from "../testplane";
import { TagFilter } from "../utils/cli";
import { TestplaneGlobals } from "../types/globals";

export type TestParserParseOpts = {
browserId: string;
Expand Down Expand Up @@ -63,8 +64,8 @@ export class TestParser extends EventEmitter {
also: AlsoController.create(eventBus),
};

global.testplane = toolGlobals;
global.hermione = toolGlobals;
(globalThis as unknown as TestplaneGlobals).testplane = toolGlobals;
(globalThis as unknown as TestplaneGlobals).hermione = toolGlobals;

this.#buildInstructions
.push(Instructions.extendWithBrowserId)
Expand Down
18 changes: 18 additions & 0 deletions src/types/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { GlobalHelper } from ".";
import type { SuiteDefinition, TestDefinition, TestHookDefinition } from "../test-reader/test-object/types";

export type GlobalItType = TestDefinition;
export type GlobalDescribeType = SuiteDefinition;
export type GlobalBeforeEachType = TestHookDefinition;
export type GlobalAfterEachType = TestHookDefinition;
export type GlobalTestplaneType = GlobalHelper;
export type GlobalHermioneType = GlobalHelper;

export type TestplaneGlobals = {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Instead, we have type of testplane-related globals to use

it: GlobalItType;
describe: GlobalDescribeType;
beforeEach: GlobalBeforeEachType;
afterEach: GlobalAfterEachType;
testplane: GlobalTestplaneType;
hermione: GlobalHermioneType;
};
Loading