Skip to content

Commit

Permalink
refactor: use typed globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkenos committed Jul 2, 2024
1 parent 6d78326 commit 9784127
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 23 deletions.
18 changes: 12 additions & 6 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"plugins": ["@typescript-eslint"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/ban-ts-comment": ["off"],
"@typescript-eslint/ban-types": ["off"],
"@typescript-eslint/no-empty-function": ["off"],
"@typescript-eslint/no-explicit-any": ["off"],
"@typescript-eslint/no-var-requires": ["off"],
"@typescript-eslint/ban-ts-comment": ["off", {}],
"@typescript-eslint/ban-types": ["off", {}],
"@typescript-eslint/no-empty-function": ["off", {}],
"@typescript-eslint/no-explicit-any": ["off", {}],
"@typescript-eslint/no-var-requires": ["off", {}],
"indent": [
"error",
2,
Expand Down Expand Up @@ -45,10 +45,16 @@
{
"files": ["*.js"],
"rules": {
"@typescript-eslint/no-unused-vars": ["off"],
"@typescript-eslint/no-unused-vars": ["off", {}],
"quotes": ["error", "single"],
"no-undef": ["off"]
}
},
{
"files": ["types.ts"],
"rules": {
"no-var": ["off"]
}
}
]
}
2 changes: 1 addition & 1 deletion demo/test/fixtures/pages/demo/demo.def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface This extends World {
}

Before({}, async function(this: This) {
this.demoPage = new DemoPage(this);
this.demoPage = new DemoPage();
});

When(
Expand Down
8 changes: 4 additions & 4 deletions src/cli/resources/fixtures/pages/the-internet.def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export interface This extends World<Parameters> {
}

Before({}, async function(this: This) {
this.theInternetPage = new TheInternetPage(this);
this.exitIntentPage = new ExitIntentPage(this);
this.loginPage = new LoginPage(this);
this.securePage = new SecurePage(this);
this.theInternetPage = new TheInternetPage();
this.exitIntentPage = new ExitIntentPage();
this.loginPage = new LoginPage();
this.securePage = new SecurePage();
});

When(
Expand Down
2 changes: 0 additions & 2 deletions src/commands/context/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { BrowserContext as PlaywrightBrowserContextType } from "@playwright
import type { Page } from "@commands/page/types";
import type { Locator } from "@commands/locator/types";
import type { Config } from "@config/types";
import type { World } from "@core/world";
import type { BrowserContext as BrowserContextClassType } from "./context";
import type { closeLastPage } from "./command/close-last-page";
import type { closeOtherPages } from "./command/close-other-pages";
Expand All @@ -12,7 +11,6 @@ import type { newPage } from "./command/new-page";
export interface BrowserContext extends PlaywrightBrowserContextType, BrowserContextClassType {
config: Config;
locatorSearchLimit: Locator;
reporter: World["reporter"];
closeLastPage: typeof closeLastPage;
closeOtherPages: typeof closeOtherPages;
lastPage: typeof lastPage;
Expand Down
2 changes: 1 addition & 1 deletion src/conditions/page/condition/snapshot-match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class SnapshotMatch extends PageCondition {
const { outDir } = this.page.context().config.snapshots.images;
const attach = (title: string, filename: string) => {
if (fs.existsSync(filename)) {
this.page.context().reporter.step(title, (step: AllureCommandStepExecutable) => step.attach(fs.readFileSync(filename), "image/png"));
_kyk_world.reporter.step(title, (step: AllureCommandStepExecutable) => step.attach(fs.readFileSync(filename), "image/png"));
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function configure(overrides?: Partial<Config>) {
}
};
// resolve and prepare snapshot directories
process.env.KYK_RESULTS = resultsDir;
globalThis._kyk_allure_results_dir = resultsDir;
process.env.LOG_LEVEL = debug ? "debug" : logLevel;
fs.removeSync(resultsDir);
Object.keys(snapshots).forEach(key => {
Expand Down
12 changes: 6 additions & 6 deletions src/core/page-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export abstract class PageObject<ParametersType = WorldParameters> {
abstract url: string;
abstract title: string;

constructor(world: World) {
this.reporter = world.reporter;
this.logger = world.logger;
this.context = world.context;
this.page = world.page;
this.parameters = world.parameters as any;
constructor() {
this.reporter = _kyk_world.reporter;
this.logger = _kyk_world.logger;
this.context = _kyk_world.context;
this.page = _kyk_world.page;
this.parameters = _kyk_world.parameters as any;
}

async navigate() {
Expand Down
6 changes: 6 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { This as World } from "./world";

declare global {
var _kyk_allure_results_dir: string;
var _kyk_world: World;
}
2 changes: 1 addition & 1 deletion src/core/utils/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { CucumberJSAllureFormatter } from "allure-cucumberjs";
/** see https://github.com/allure-framework/allure-js/blob/master/packages/allure-cucumberjs/README.md */
export default class extends CucumberJSAllureFormatter {
constructor(options: any) {
super(options, new AllureRuntime({ resultsDir: path.join(process.env.KYK_RESULTS, "allure") }), {});
super(options, new AllureRuntime({ resultsDir: path.join(_kyk_allure_results_dir, "allure") }), {});
}
}
2 changes: 1 addition & 1 deletion src/core/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export abstract class World extends AllureWorld implements PrivateWorld {
this.setPageObjects();
this.setReporter();
this.loadCommands();
globalThis._kyk_world = this;
}

private setPageObjects() {
Expand Down Expand Up @@ -122,7 +123,6 @@ export abstract class World extends AllureWorld implements PrivateWorld {
const from = await browser.newContext(this.config.contextOptions);
const context = new BrowserContextClass(from) as BrowserContext;
context.setDefaultTimeout(this.config.timeout);
context.reporter = this.reporter;
context.config = this.config;
return context;
}
Expand Down

0 comments on commit 9784127

Please sign in to comment.