Skip to content

Commit

Permalink
test: run subset of tests on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalets committed Mar 31, 2024
1 parent 86317e2 commit c01d943
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 26 deletions.
10 changes: 0 additions & 10 deletions lint-staged.config.js

This file was deleted.

26 changes: 26 additions & 0 deletions lint-staged.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import path from 'node:path';
import { getSmokeTestDirs } from './test/smoke.mjs';

export default {
'**/*.{js,mjs,ts}': [
'eslint --fix --no-warn-ignored',
'prettier --write --ignore-unknown',
(changedFiles) => {
// run tests only on smoke dirs + changed test dirs
const testDirs = [...new Set([...getSmokeTestDirs(), ...extractTestDirs(changedFiles)])];
return `npm run only ${testDirs.join(' ')}`;
},
],
'**/*.feature': 'node scripts/no-only-in-features.mjs',
'docs/**': () => 'npm run docs:validate',
'!**/*.{js,mjs,ts,feature}': 'prettier --write --ignore-unknown',
};

function extractTestDirs(absPaths) {
const testDirs = absPaths
.map((file) => path.relative(process.cwd(), file))
.map((file) => file.split(path.sep))
.filter((parts) => parts[0] === 'test' && parts.length > 2)
.map((parts) => path.join(parts[0], parts[1]));
return [...new Set(testDirs)];
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"prettier": "prettier --check --ignore-unknown .",
"prettier:w": "prettier --write --ignore-unknown .",
"test": "node test/setup.mjs && node --test test/**/test.mjs",
"test:smoke": "node test/setup.mjs && node --test $(node test/smoke.mjs)",
"only": "node test/setup.mjs && node --test",
"only:d": "node test/setup.mjs && TEST_DEBUG=1 node --test",
"build": "bash scripts/build.sh",
Expand Down
3 changes: 2 additions & 1 deletion test/bdd-syntax/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
3 changes: 2 additions & 1 deletion test/component-tests/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
3 changes: 2 additions & 1 deletion test/decorators/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
3 changes: 2 additions & 1 deletion test/duplicate-steps/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
3 changes: 2 additions & 1 deletion test/hooks-order/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
2 changes: 1 addition & 1 deletion test/reporter-cucumber-html/check-report/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pathToFileURL } from 'node:url';
import { Locator, Page } from '@playwright/test';

export async function openReport(page: Page) {
await page.goto(pathToFileURL('reports/report.html').href);
await page.goto(pathToFileURL('actual-reports/report.html').href);
}

export function getFeature(page: Page) {
Expand Down
3 changes: 2 additions & 1 deletion test/reporter-cucumber-html/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
4 changes: 2 additions & 2 deletions test/reporter-cucumber-html/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default defineConfig({
testDir,
reporter: [
['dot'],
cucumberReporter('message', { outputFile: 'reports/message.ndjson' }),
cucumberReporter('html', { outputFile: 'reports/report.html' }),
cucumberReporter('message', { outputFile: 'actual-reports/message.ndjson' }),
cucumberReporter('html', { outputFile: 'actual-reports/report.html' }),
],
use: {
screenshot: 'only-on-failure',
Expand Down
4 changes: 2 additions & 2 deletions test/reporter-cucumber-html/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { test, TestDir, execPlaywrightTest, execPlaywrightTestWithError } from '
const testDir = new TestDir(import.meta);

test(testDir.name, () => {
testDir.clearDir('reports');
testDir.clearDir('actual-reports');
testDir.clearDir('test-results');
execPlaywrightTestWithError(testDir.name);
checkHtmlReport();
});

function checkHtmlReport() {
expect(testDir.isFileExists('reports/report.html')).toEqual(true);
expect(testDir.isFileExists('actual-reports/report.html')).toEqual(true);
execPlaywrightTest(testDir.name, 'npx playwright test --config check-report');
}
3 changes: 2 additions & 1 deletion test/reporter-merge/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}
4 changes: 2 additions & 2 deletions test/reporter-merge/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export default defineConfig({
reporter: isShardRun
? 'blob'
: [
cucumberReporter('message', { outputFile: 'reports/message.ndjson' }),
cucumberReporter('html', { outputFile: 'reports/report.html' }),
cucumberReporter('message', { outputFile: 'actual-reports/message.ndjson' }),
cucumberReporter('html', { outputFile: 'actual-reports/report.html' }),
],
use: {
screenshot: 'only-on-failure',
Expand Down
2 changes: 1 addition & 1 deletion test/reporter-merge/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function mergeReports() {
}

function checkHtmlReport() {
expect(testDir.isFileExists('reports/report.html')).toEqual(true);
expect(testDir.isFileExists('actual-reports/report.html')).toEqual(true);
execPlaywrightTest(testDir.name, 'npx playwright test --config ./check-report');
}

Expand Down
32 changes: 32 additions & 0 deletions test/smoke.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Outputs subset of tests for the smoke run.
* Smoke tests are detected by 'smoke: true' field in the package.json file.
*
* Usage:
* node test/smoke.mjs
*/
import path from 'node:path';
import fs from 'node:fs';
import { fileURLToPath } from 'node:url';

export function getSmokeTestDirs() {
return fs
.readdirSync('test')
.filter((dir) => {
const pkgJsonPath = `test/${dir}/package.json`;
if (fs.existsSync(pkgJsonPath)) {
const pkg = JSON.parse(fs.readFileSync(pkgJsonPath));
return pkg.smoke;
}
})
.map((dir) => path.join('test', dir));
}

if (isCalledFromCli()) {
process.stdout.write(getSmokeTestDirs().join(' '));
}

// See: https://github.com/nodejs/node/issues/49440#issuecomment-1703467440
function isCalledFromCli() {
return fileURLToPath(import.meta.url) === process.argv[1];
}
3 changes: 2 additions & 1 deletion test/snippets-playwright-style/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts."
"description": "This file is required for Playwright to consider this dir as a <package-json dir>. It ensures to load 'playwright-bdd' from './test/node_modules/playwright-bdd' and output './test-results' here to avoid conficts.",
"smoke": true
}

0 comments on commit c01d943

Please sign in to comment.