Skip to content

Commit

Permalink
fix: workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
syzzana committed Mar 2, 2024
1 parent d5ebf81 commit 9bb7531
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 62 deletions.
3 changes: 2 additions & 1 deletion dist/src/indent-list-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const defaultListTestsWithColors = {
environment: "dev",
};
class IndentListReporter {
options;
allTests = [];
passed = 0;
failed = 0;
Expand All @@ -24,6 +23,7 @@ class IndentListReporter {
timedOut = 0;
retries = 0;
failedTests = [];
options;
/**
* Constructor to pass on custom options for the reporter
* @param options
Expand All @@ -40,6 +40,7 @@ class IndentListReporter {
return true;
}
onBegin(config, suite) {
console.log("AI Solution 3.0");
howToReadTestResults(this.options.environment);
log(`${Color.text("TEST RESULTS:").cyan().bgBlack().valueOf()}`);
const number = suite.allTests().length;
Expand Down
51 changes: 29 additions & 22 deletions dist/src/loggin-tests-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,25 @@ import { lineBreak, setIconAndColorPerTestStatus } from "./color-text/styling-te
import { filterOutDuplicateFailedTestsOnRetry } from "./filtering-tests.js";
import { logTestError } from "./loggin-error-message.js";
import { adaptFilePathImportForWindows, isWindows } from "./utils/utils.js";
export const doesModuleExist = (moduleName) => {
// Dynamically determine the Playwright config file's extension (.ts or .js)
export const getPlaywrightConfigFile = async () => {
const tsConfigPath = `${process.cwd()}/playwright.config.ts`;
const jsConfigPath = `${process.cwd()}/playwright.config.js`;
try {
require.resolve((`${process.cwd()}/${moduleName}`));
return true;
await import(tsConfigPath);
return tsConfigPath;
}
catch (e) {
return false;
catch {
return jsConfigPath; // Fallback to .js if .ts import fails
}
};
export const isPlaywrightConfigJSOrTS = doesModuleExist("playwright.config.ts") ? "playwright.config.ts" : "playwright.config.js";
/**
* Get the config from playwright.config.ts
*/
export const userPlaywrightConfigFile = `${process.cwd()}/${isPlaywrightConfigJSOrTS}`;
export const userPlaywrightConfigFile = await getPlaywrightConfigFile();
export const convertImportFilePathForWindows = adaptFilePathImportForWindows(userPlaywrightConfigFile);
export const whichPlatForm = isWindows ? convertImportFilePathForWindows : userPlaywrightConfigFile;
export const playwrightConfigDetails = await import(whichPlatForm);
/**
* Log the results of the function
* Resuses the console.log function
* We just simplified the name of the method to log
* @param data
*/
export const log = (...data) => {
console.log(...data);
};
/**
* Log the name of the spec file only once
* Example output:
Expand All @@ -45,7 +38,12 @@ export const log = (...data) => {
* `
* @param specFileName
*/
export const logSpecFileName = async (specFileName) => {
// This function is now async due to dynamic import
export const logSpecFileName = async (specFileName, playwrightConfigDetails) => {
// const userPlaywrightConfigFile = await getPlaywrightConfigFile();
// const convertImportFilePathForWindows = adaptFilePathImportForWindows(userPlaywrightConfigFile);
// const whichPlatForm = isWindows ? convertImportFilePathForWindows : userPlaywrightConfigFile;
// const defineConfig = await import(whichPlatForm);
// @ts-ignore
const reporterOptions = getReporterOptions(playwrightConfigDetails.default.reporter);
let specFileNameColor;
Expand All @@ -64,6 +62,15 @@ export const logSpecFileName = async (specFileName) => {
log(`${Color.text(specFileName).cyan().valueOf()}:`);
}
};
/**
* Log the results of the function
* Resuses the console.log function
* We just simplified the name of the method to log
* @param data
*/
export const log = (...data) => {
console.log(...data);
};
/**
* Log the name of the suite only once
* Example output:
Expand All @@ -75,7 +82,7 @@ export const logSpecFileName = async (specFileName) => {
* `
* @param suiteName
*/
export const logSuiteDescription = (suiteName) => {
export const logSuiteDescription = (suiteName, playwrightConfigDetails) => {
// @ts-ignore
const reporterOptions = getReporterOptions(playwrightConfigDetails.default.reporter);
let suiteDescriptionColor;
Expand All @@ -101,7 +108,7 @@ export const logSuiteDescription = (suiteName) => {
* @param count
* @param test
*/
export const logTestCaseData = (count, test) => {
export const logTestCaseData = (count, test, playwrightConfigDetails) => {
const status = setIconAndColorPerTestStatus(test.status);
const duration = Color.text(`(${test.duration}ms)`).gray().dim().valueOf();
const counter = `${Color.text(`${count}.`).gray().valueOf()}`;
Expand Down Expand Up @@ -146,13 +153,13 @@ export const logTestCaseData = (count, test) => {
export const logTestResults = (allTests) => {
let testCounter = 0;
allTests.forEach((specFile) => {
logSpecFileName(specFile.getSpecName());
logSpecFileName(specFile.getSpecName(), playwrightConfigDetails);
specFile.getSuiteTests().forEach((suite) => {
logSuiteDescription(suite.getSuiteDescription());
logSuiteDescription(suite.getSuiteDescription(), playwrightConfigDetails);
suite.getTestCases().forEach((test) => {
//TODO: filter getTests() here failed tests that were retried and failed again
testCounter++;
logTestCaseData(testCounter, test);
logTestCaseData(testCounter, test, playwrightConfigDetails);
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "indent-list-reporter",
"version": "1.0.15",
"version": "1.0.16",
"description": "Playwright list reporter for terminal with colors and indentation'",
"main": "./dist/src/indent-list-reporter.ts",
"main": "./dist/src/indent-list-reporter.js",
"type": "module",
"homepage": "https://github.com/syzzana/indent-list-reporter/blob/main/README.md",
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions src/indent-list-reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import {TestCase, TestResult, Reporter, FullResult, Suite, FullConfig} from "@pl
import {SuiteTestCases, TestCaseData, TestCaseError, TestsPerSpecFile} from "./TestsPerSpecFile.js";
import {getFileNameOrParentSuite, howToReadTestResults, logSummary, StatusCounter} from "./general-tests-info.js";
import {filterUniqueSpecsBySpecName} from "./filtering-tests.js";
import {TestStatus} from "@playwright/test";
import { TestStatus} from "@playwright/test";
import {TestError} from "playwright/types/testReporter";
import Color from "./color-text/Color.js";
import {log, logTestResults} from "./loggin-tests-data.js";
import {lineBreak} from "./color-text/styling-terminal.js";
import {logTestError} from "./loggin-error-message.js";

const defaultListTestsWithColors: IndentListReporterOptions = {
ignoreColors: false,
ignoreColors: false,
baseColors: {
specFileNameColor: "cyan",
suiteDescriptionColor: "cyan",
Expand All @@ -27,15 +27,17 @@ interface ListTestsWithColors {
isDimmed?: boolean;
}

export type MyReporterOptions = ['indent-list-reporter' | './src/indent-list-reporter.ts'] | ['indent-list-reporter' | './src/indent-list-reporter.ts', IndentListReporterOptions];
export type MyReporterOptions =
['indent-list-reporter' | './src/indent-list-reporter.ts']
| ['indent-list-reporter' | './src/indent-list-reporter.ts', IndentListReporterOptions];

interface IndentListReporterOptions {
ignoreColors: boolean;
baseColors: ListTestsWithColors;
environment?: string;
}

class IndentListReporter implements Reporter {
private options: IndentListReporterOptions;
allTests: TestsPerSpecFile[] = [];
passed = 0;
failed = 0;
Expand All @@ -44,6 +46,7 @@ class IndentListReporter implements Reporter {
timedOut = 0;
retries = 0;
failedTests: TestCaseError[] = [];
private options: IndentListReporterOptions;

/**
* Constructor to pass on custom options for the reporter
Expand Down
55 changes: 30 additions & 25 deletions src/loggin-tests-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,27 @@ import {logTestError} from "./loggin-error-message.js";
import {ColorsAvailable} from "./indent-list-reporter.js";
import { adaptFilePathImportForWindows, isWindows } from "./utils/utils.js";

export const doesModuleExist = (moduleName: string) => {
// Dynamically determine the Playwright config file's extension (.ts or .js)
export const getPlaywrightConfigFile = async () => {
const tsConfigPath = `${process.cwd()}/playwright.config.ts`;
const jsConfigPath = `${process.cwd()}/playwright.config.js`;

try {
require.resolve((`${process.cwd()}/${moduleName}`));
return true;
} catch (e) {
return false;
await import(tsConfigPath);
return tsConfigPath;
} catch {
return jsConfigPath; // Fallback to .js if .ts import fails
}
}

export const isPlaywrightConfigJSOrTS = doesModuleExist("playwright.config.ts") ? "playwright.config.ts" : "playwright.config.js";
};

/**
* Get the config from playwright.config.ts
* Get the config from playwright.config.ts
*/
export const userPlaywrightConfigFile = `${process.cwd()}/${isPlaywrightConfigJSOrTS}`;
export const userPlaywrightConfigFile = await getPlaywrightConfigFile();
export const convertImportFilePathForWindows = adaptFilePathImportForWindows(userPlaywrightConfigFile);
export const whichPlatForm = isWindows ? convertImportFilePathForWindows : userPlaywrightConfigFile;
export const playwrightConfigDetails: PlaywrightTestConfig = await import(whichPlatForm)
/**
* Log the results of the function
* Resuses the console.log function
* We just simplified the name of the method to log
* @param data
*/
export const log = (...data: any[]) => {
console.log(...data);
};


/**
* Log the name of the spec file only once
Expand All @@ -51,7 +45,8 @@ export const log = (...data: any[]) => {
* `
* @param specFileName
*/
export const logSpecFileName = async (specFileName: string) => {
// This function is now async due to dynamic import
export const logSpecFileName = async (specFileName: string, playwrightConfigDetails: PlaywrightTestConfig) => {
// @ts-ignore
const reporterOptions = getReporterOptions(playwrightConfigDetails.default.reporter);
let specFileNameColor: ColorsAvailable;
Expand All @@ -69,6 +64,16 @@ export const logSpecFileName = async (specFileName: string) => {
}
};

/**
* Log the results of the function
* Resuses the console.log function
* We just simplified the name of the method to log
* @param data
*/
export const log = (...data: any[]) => {
console.log(...data);
};

/**
* Log the name of the suite only once
* Example output:
Expand All @@ -80,7 +85,7 @@ export const logSpecFileName = async (specFileName: string) => {
* `
* @param suiteName
*/
export const logSuiteDescription = (suiteName: string) => {
export const logSuiteDescription = (suiteName: string, playwrightConfigDetails: PlaywrightTestConfig) => {
// @ts-ignore
const reporterOptions = getReporterOptions(playwrightConfigDetails.default.reporter);
let suiteDescriptionColor: ColorsAvailable;
Expand All @@ -105,7 +110,7 @@ export const logSuiteDescription = (suiteName: string) => {
* @param count
* @param test
*/
export const logTestCaseData = (count: number, test: TestCaseData) => {
export const logTestCaseData = (count: number, test: TestCaseData, playwrightConfigDetails: PlaywrightTestConfig) => {
const status = setIconAndColorPerTestStatus(test.status);
const duration = Color.text(`(${test.duration}ms)`).gray().dim().valueOf();
const counter = `${Color.text(`${count}.`).gray().valueOf()}`;
Expand Down Expand Up @@ -148,13 +153,13 @@ export const logTestResults = (allTests: TestsPerSpecFile[]) => {
let testCounter = 0;

allTests.forEach((specFile) => {
logSpecFileName(specFile.getSpecName());
logSpecFileName(specFile.getSpecName(), playwrightConfigDetails);
specFile.getSuiteTests().forEach((suite) => {
logSuiteDescription(suite.getSuiteDescription());
logSuiteDescription(suite.getSuiteDescription(), playwrightConfigDetails);
suite.getTestCases().forEach((test) => {
//TODO: filter getTests() here failed tests that were retried and failed again
testCounter++;
logTestCaseData(testCounter, test);
logTestCaseData(testCounter, test, playwrightConfigDetails);
});
});
});
Expand Down
17 changes: 9 additions & 8 deletions tests/loggin-test-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import {doesModuleExist, isPlaywrightConfigJSOrTS, playwrightConfigDetails, userPlaywrightConfigFile} from "../src/loggin-tests-data";
import {getPlaywrightConfigFile, userPlaywrightConfigFile} from "../src/loggin-tests-data";

import {test, expect} from 'vitest';
test("check module exists", () => {
const myModule = doesModuleExist("playwright.config.ts");
expect(myModule).toBe(true);
test("check module exists", async () => {
const myModule = await getPlaywrightConfigFile();
expect(myModule).toContain("playwright.config.ts");
})

test("check module does not exist", () => {
const myModule = doesModuleExist("playwright.config.js");
expect(myModule).toBe(false);
test("check module does not exist", async () => {
const myModule = await getPlaywrightConfigFile();
expect(myModule).not.toContain("playwright.config.js");
});

test("check we can import config data from playwright.config.ts on repo file", async () => {
expect(userPlaywrightConfigFile).toContain("indent-list-reporter/playwright.config.ts");
expect(isPlaywrightConfigJSOrTS).toBe("playwright.config.ts");
let playwrightConfigDetails = await import(`${process.cwd()}/playwright.config.ts`);
let playwrightConfigDetailsJS = await import(`${process.cwd()}/playwright.config.js`);
expect(playwrightConfigDetails).toBeDefined()
})

Expand Down

0 comments on commit 9bb7531

Please sign in to comment.