diff --git a/src/globals.ts b/src/globals.ts index ebf3de925..cd9522d99 100644 --- a/src/globals.ts +++ b/src/globals.ts @@ -8,9 +8,55 @@ import type { 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; +const typedGlobals = globalThis as unknown as TestplaneGlobals; + +function itFunc(this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.it.apply(this, args); +} + +itFunc.only = function (this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.it.only.apply(this, args); +}; + +itFunc.skip = function (this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.it.skip.apply(this, args); +}; + +function describeFunc(this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.describe.apply(this, args); +} + +describeFunc.only = function ( + this: unknown, + ...args: Parameters +): ReturnType { + return typedGlobals.describe.only.apply(this, args); +}; + +describeFunc.skip = function ( + this: unknown, + ...args: Parameters +): ReturnType { + return typedGlobals.describe.skip.apply(this, args); +}; + +function beforeEachFunc(this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.beforeEach.apply(this, args); +} + +function afterEachFunc(this: unknown, ...args: Parameters): ReturnType { + return typedGlobals.afterEach.apply(this, args); +} + +const testplaneProxy = new Proxy(typedGlobals.testplane, { + get(_, prop: keyof GlobalTestplaneType): GlobalTestplaneType[keyof GlobalTestplaneType] { + return typedGlobals.testplane[prop]; + }, +}); + +export const it = itFunc as GlobalItType; +export const describe = describeFunc as GlobalDescribeType; +export const beforeEach = beforeEachFunc as GlobalBeforeEachType; +export const afterEach = afterEachFunc as GlobalAfterEachType; +export const testplane = testplaneProxy as GlobalTestplaneType; +export const hermione = testplaneProxy as GlobalHermioneType;