Skip to content

Commit

Permalink
test: parallelize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Jul 1, 2023
1 parent f6373ce commit 878bad8
Show file tree
Hide file tree
Showing 31 changed files with 165 additions and 142 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"knip": "knip -c knip.config.ts",
"prettier": "prettier --check --ignore-unknown .",
"prettier:w": "prettier --write --ignore-unknown .",
"test": "node test/runner.mjs",
"only": "node --test-only test/runner.mjs",
"test": "node --test test/**/test.mjs",
"only": "node --test --test-only test/**/test.mjs",
"only:d": "TEST_DEBUG=1 npm run only",
"build": "rm -rf dist && tsc -p tsconfig.build.json",
"examples": "ts-node scripts/examples.ts",
Expand Down
4 changes: 4 additions & 0 deletions test/bdd-syntax-cucumber-style/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/bdd-syntax/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/cucumber-config-file/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/custom-fixtures/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/custom-world/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/default-world/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
6 changes: 6 additions & 0 deletions test/error-import-test-from-steps/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, `Option "importTestFrom" should point to separate file`),
);
6 changes: 6 additions & 0 deletions test/error-import-test-not-from-bdd/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, `createBdd() should use test extended from "playwright-bdd"`),
);
4 changes: 4 additions & 0 deletions test/esm-ts/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name, {}, 'npm test'));
4 changes: 4 additions & 0 deletions test/esm/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
30 changes: 4 additions & 26 deletions test/helpers.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import fs from 'node:fs';
import { execSync } from 'node:child_process';
import assert from 'node:assert/strict';
import path from 'node:path';

const stdioForErrors = process.env.TEST_DEBUG ? 'inherit' : 'pipe';

export function getTestName(importMeta) {
return importMeta.url.split('/').slice(-2)[0];
}

export function execPlaywrightTest(dir, opts, cmd) {
opts = Object.assign({ stdio: 'inherit' }, opts);
cmd = cmd || 'node ../../dist/gen/cli && npx playwright test';
Expand Down Expand Up @@ -39,28 +42,3 @@ export function execPlaywrightTestWithError(dir, substr) {
},
);
}

export function showPlaywrightVersion() {
const { version } = JSON.parse(
fs.readFileSync('node_modules/@playwright/test/package.json', 'utf8'),
);

console.log(`Playwright version: ${version}`);
}

export function symlinkPlaywrghtBdd() {
// symlink node_modules/playwright-bdd to dist
// as generated files import { test } from "playwright-bdd"
execSync('ln -sfn ../dist ./node_modules/playwright-bdd', { stdio: 'inherit' });
}

export function buildDist() {
execSync('npm run build', { stdio: 'inherit' });
}

export function defineTestOnly(test) {
test.only = (title, fn) => {
if (process.env.FORBID_ONLY) throw new Error(`test.only is forbidden`);
test(title, { only: true }, fn);
};
}
4 changes: 4 additions & 0 deletions test/i18n/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/no-define-bdd-config/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTestWithError(t.name, `No BDD configs found`));
9 changes: 9 additions & 0 deletions test/no-import-test-from/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(
t.name,
`When using custom "test" function in createBdd() you should`,
),
);
13 changes: 0 additions & 13 deletions test/no-steps/playwright.config.ts

This file was deleted.

4 changes: 0 additions & 4 deletions test/no-steps/sample.feature

This file was deleted.

Empty file removed test/no-steps/steps.ts
Empty file.
6 changes: 6 additions & 0 deletions test/parse-error/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, `Parse error in "sample.feature" (1:1)`),
);
97 changes: 0 additions & 97 deletions test/runner.mjs

This file was deleted.

44 changes: 44 additions & 0 deletions test/setup.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Setup
*/
import fs from 'node:fs';
import { execSync } from 'node:child_process';

setup();

function setup() {
!process.env.CI && ensureNodeVersion(20);

showPlaywrightVersion();

// link node_modules/playwright-bdd to dist
// as generated files import { test } from "playwright-bdd"
symlinkPlaywrghtBdd();

// must build project before tests as we run tests without ts-node
buildDist();
}

function ensureNodeVersion(version) {
if (!process.version.startsWith(`v${version}.`)) {
throw new Error(`Expected node version: ${version}`);
}
}

function showPlaywrightVersion() {
const { version } = JSON.parse(
fs.readFileSync('node_modules/@playwright/test/package.json', 'utf8'),
);

console.log(`Playwright version: ${version}`);
}

function symlinkPlaywrghtBdd() {
// symlink node_modules/playwright-bdd to dist
// as generated files import { test } from "playwright-bdd"
execSync('ln -sfn ../dist ./node_modules/playwright-bdd', { stdio: 'inherit' });
}

function buildDist() {
execSync('npm run build', { stdio: 'inherit' });
}
6 changes: 6 additions & 0 deletions test/several-projects-no-outputdir/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, `please manually provide different "outputDir" option`),
);
4 changes: 4 additions & 0 deletions test/several-projects/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
4 changes: 4 additions & 0 deletions test/snapshots/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));
11 changes: 11 additions & 0 deletions test/undefined-steps-cucumber-snippet/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, [
`1. Missing step definition for "sample.feature:4:9"`,
`Given('Step without parameters', async function () {`,
`Given('Step with one string parameter {string}', async function (string) {`,
`Missing step definitions (2)`,
]),
);
19 changes: 19 additions & 0 deletions test/undefined-steps-playwright-snippet/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import test from 'node:test';
import { getTestName, execPlaywrightTestWithError } from '../helpers.mjs';

test(getTestName(import.meta), (t) =>
execPlaywrightTestWithError(t.name, [
`1. Missing step definition for "sample.feature:4:9"`,
`Given('Step without parameters', async ({}) => {`,
`Given('Step with one string parameter {string}', async ({}, arg: string) => {`,
`Given('Step with two string parameters {string} and {string}', async ({}, arg: string, arg1: string) => {`,
`Given('Step with one float parameter {float}', async ({}, arg: number) => {`,
`Given('Step with one int parameter {int}', async ({}, arg: number) => {`,
`Given('Step with two int parameters {int} and {int}', async ({}, arg: number, arg1: number) => {`,
`Given('Step with docString', async ({}, docString: string) => {`,
`Given('Step with dataTable', async ({}, dataTable: DataTable) => {`,
`When('I click link {string}', async ({}, arg: string) => {`,
`Then('I see in title {string}', async ({}, arg: string) => {`,
`Missing step definitions (10)`,
]),
);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions test/without-custom-fixtures/test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import test from 'node:test';
import { getTestName, execPlaywrightTest } from '../helpers.mjs';

test(getTestName(import.meta), (t) => execPlaywrightTest(t.name));

0 comments on commit 878bad8

Please sign in to comment.