Skip to content

Commit

Permalink
test(playwright): fix ts-ignore and eslint warnings (via #441)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt committed May 17, 2022
1 parent 7fd26db commit 44763e7
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1,076 deletions.
2 changes: 1 addition & 1 deletion packages/allure-playwright/package.json
Expand Up @@ -23,7 +23,7 @@
"lint:fix": "eslint ./src --ext .ts --fix"
},
"devDependencies": {
"@playwright/test": "^1.21.1",
"@playwright/test": "^1.22.0",
"eslint": "^7.32.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
Expand Down
45 changes: 26 additions & 19 deletions packages/allure-playwright/test/fixtures.ts
Expand Up @@ -14,11 +14,11 @@
* limitations under the License.
*/

import { test as base, TestInfo } from "@playwright/test";
import type { InMemoryAllureWriter } from "allure-js-commons";
import { spawn } from "child_process";
import fs from "fs";
import path from "path";
import { test as base, TestInfo } from "@playwright/test";
import type { InMemoryAllureWriter } from "allure-js-commons";
export { expect } from "@playwright/test";

type RunResult = any;
Expand All @@ -27,7 +27,7 @@ type Files = { [key: string]: string | Buffer };
type Params = { [key: string]: string | number | boolean | string[] };
type Env = { [key: string]: string | number | boolean | undefined };

async function writeFiles(testInfo: TestInfo, files: Files) {
const writeFiles = async (testInfo: TestInfo, files: Files) => {
const baseDir = testInfo.outputPath();

const hasConfig = Object.keys(files).some((name) => name.includes(".config."));
Expand All @@ -49,14 +49,14 @@ async function writeFiles(testInfo: TestInfo, files: Files) {
);

return baseDir;
}
};

async function runPlaywrightTest(
const runPlaywrightTest = async (
baseDir: string,
postProcess: (writer: InMemoryAllureWriter) => any,
params: any,
env: NodeJS.ProcessEnv,
): Promise<RunResult> {
env: Env,
): Promise<RunResult> => {
const paramList = [];
let additionalArgs = "";
for (const key of Object.keys(params)) {
Expand All @@ -65,19 +65,21 @@ async function runPlaywrightTest(
continue;
}
for (const value of Array.isArray(params[key]) ? params[key] : [params[key]]) {
const k = key.startsWith("-") ? key : "--" + key;
const k = key.startsWith("-") ? key : `--${ key}`;
paramList.push(params[key] === true ? `${k}` : `${k}=${value}`);
}
}
const outputDir = path.join(baseDir, "test-results");
const args = [require.resolve("@playwright/test/cli"), "test"];
args.push(
"--output=" + outputDir,
"--reporter=" + require.resolve("../dist/index.js"),
`--output=${ outputDir}`,
`--reporter=${ require.resolve("../dist/index.js")}`,
"--workers=2",
...paramList,
);
if (additionalArgs) args.push(...additionalArgs);
if (additionalArgs) {
args.push(...additionalArgs);
}
const testProcess = spawn("node", args, {
env: {
...process.env,
Expand All @@ -89,14 +91,18 @@ async function runPlaywrightTest(
let output = "";
testProcess.stdout.on("data", (chunk) => {
output += String(chunk);
if (process.env.PW_RUNNER_DEBUG) process.stdout.write(String(chunk));
if (process.env.PW_RUNNER_DEBUG) {
process.stdout.write(String(chunk));
}
});
testProcess.stderr.on("data", (chunk) => {
if (process.env.PW_RUNNER_DEBUG) process.stderr.write(String(chunk));
if (process.env.PW_RUNNER_DEBUG) {
process.stderr.write(String(chunk));
}
});
await new Promise<number>((x) => testProcess.on("close", x));
return JSON.parse(output.toString());
}
};

type Fixtures = {
runInlineTest: (
Expand All @@ -108,18 +114,19 @@ type Fixtures = {
};

export const test = base.extend<Fixtures>({
// @ts-ignore
// eslint-disable-next-line no-empty-pattern
runInlineTest: async ({}, use, testInfo: TestInfo) => {
let runResult: RunResult | undefined;
await use(
// @ts-ignore
async (files: Files, postProcess, params: Params = {}, env: NodeJS.ProcessEnv = {}) => {
async (files: Files, postProcess, params: Params = {}, env: Env = {}) => {
const baseDir = await writeFiles(testInfo, files);
runResult = await runPlaywrightTest(baseDir, postProcess, params, env);
return runResult;
},
);
if (testInfo.status !== testInfo.expectedStatus && runResult && !process.env.PW_RUNNER_DEBUG)
console.log(runResult.output);
if (testInfo.status !== testInfo.expectedStatus && runResult && !process.env.PW_RUNNER_DEBUG) {
// eslint-disable-next-line no-console
console.error(runResult.output);
}
},
});
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/label.spec.ts
@@ -1,5 +1,5 @@
import { Label, LabelName } from "allure-js-commons";
import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";

test("should have label", async ({ runInlineTest }) => {
const result: Label[] = await runInlineTest(
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/labels.spec.ts
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";

test("should report structure", async ({ runInlineTest }) => {
const result = await runInlineTest(
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/link.spec.ts
Expand Up @@ -15,7 +15,7 @@
*/

import { Label } from "allure-js-commons";
import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";
test("should have link", async ({ runInlineTest }) => {
const result: Label[] = await runInlineTest(
{
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/status.spec.ts
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";

test("should report test status", async ({ runInlineTest }) => {
const result = await runInlineTest(
Expand Down
2 changes: 1 addition & 1 deletion packages/allure-playwright/test/stdio.spec.ts
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";

test("should report stdout and stderr", async ({ runInlineTest }) => {
const result = await runInlineTest(
Expand Down
13 changes: 9 additions & 4 deletions packages/allure-playwright/test/steps.spec.ts
Expand Up @@ -15,7 +15,12 @@
*/

import { StepResult, TestResult } from "allure-js-commons";
import { test, expect } from "./fixtures";
import { expect, test } from "./fixtures";

type SerializableStep = {
name?: string;
children: SerializableStep[];
};

test("should report test status", async ({ runInlineTest }) => {
const result = await runInlineTest(
Expand All @@ -39,10 +44,10 @@ test("should report test status", async ({ runInlineTest }) => {
`,
},
(writer) => {
const convert = (test: StepResult | TestResult) => {
const convert = (testResult: StepResult | TestResult): SerializableStep => {
return {
name: test.name,
children: test.steps.map(convert),
name: testResult.name,
children: testResult.steps.map(convert),
};
};
return convert(writer.tests[0]);
Expand Down

0 comments on commit 44763e7

Please sign in to comment.