From e43748a00be91b37123cdbb58852268c1a93cad4 Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:04:41 +0200 Subject: [PATCH 1/9] add alias for custom fixtures --- bin/check.js | 12 +++----- index.js | 2 +- src/lib/frameworks/playwright.js | 52 ++++++++++++++++++-------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/bin/check.js b/bin/check.js index f2cb8d52..0e39b068 100755 --- a/bin/check.js +++ b/bin/check.js @@ -12,13 +12,6 @@ const debug = require('debug')('testomatio:check'); const { version } = require('../package.json'); console.log(chalk.cyan.bold(` 🤩 Tests checker by Testomat.io v${version}`)); -function checkPattern(pattern) { - pattern = pattern.trim(); - if (!pattern) return true; - if (pattern == '.') return true; - return pattern.includes('*'); -} - process.env.isTestomatioCli = true; const program = require('commander'); @@ -41,10 +34,13 @@ program .option('--clean-ids', 'Remove testomatio ids from test and suite') .option('--no-hooks', 'Exclude test hooks code from the code on the client') .option('--line-numbers', 'Adding an extra line number to each block of code') + .option('--alias ', 'Specify custom test name for Playwright tests') .action(async (framework, files, opts) => { framework = framework.toLowerCase(); + opts.alias = opts.alias ? opts.alias.split(',') : []; opts.framework = framework; opts.pattern = files; + opts.pwCustomFixtureNames = opts.pwCustomFixtureNames ? opts.pwCustomFixtureNames.split(',') : []; const frameworkOpts = {}; if (opts.lineNumbers) { @@ -55,7 +51,7 @@ program frameworkOpts.noHooks = !opts.hooks; } - const analyzer = new Analyzer(framework, opts.dir || process.cwd(), frameworkOpts); + const analyzer = new Analyzer(framework, opts.dir || process.cwd(), { ...opts, ...frameworkOpts }); try { if (opts.typescript) { try { diff --git a/index.js b/index.js index 2108c5fd..afb74176 100644 --- a/index.js +++ b/index.js @@ -31,7 +31,7 @@ async function run() { /* prettier-ignore */ const docBranch = core.getInput('documentation-branch') || (await octokit.repos.get({ owner, repo })).data.default_branch; const pullRequest = new PullRequest(core.getInput('token', { required: true })); - const analyzer = new Analyzer(framework, mainRepoPath, { framework }); + const analyzer = new Analyzer(framework, mainRepoPath, opts); if (core.getInput('typescript')) analyzer.withTypeScript(); diff --git a/src/lib/frameworks/playwright.js b/src/lib/frameworks/playwright.js index 838ace19..079c0b0e 100644 --- a/src/lib/frameworks/playwright.js +++ b/src/lib/frameworks/playwright.js @@ -186,36 +186,42 @@ module.exports = (ast, file = '', source = '', opts = {}) => { } } - if (path.isIdentifier({ name: 'test' }) || path.isIdentifier({ name: 'it' })) { - if (!hasStringOrTemplateArgument(path.parent)) return; - - let code = ''; - - beforeCode = beforeCode ?? ''; - beforeEachCode = beforeEachCode ?? ''; - afterCode = afterCode ?? ''; - /* prettier-ignore */ - code = noHooks + const fixtureNames = [...['test', 'it'], ...opts?.alias]; + for (const fiixtureName of fixtureNames || []) { + if (path.isIdentifier({ name: fiixtureName })) { + if (!hasStringOrTemplateArgument(path.parent)) return; + + let code = ''; + + beforeCode = beforeCode ?? ''; + beforeEachCode = beforeEachCode ?? ''; + afterCode = afterCode ?? ''; + /* prettier-ignore */ + code = noHooks ? getCode(source, getLineNumber(path), getEndLineNumber(path), isLineNumber) : beforeEachCode + beforeCode + getCode(source, getLineNumber(path), getEndLineNumber(path), isLineNumber) + afterCode; - const testName = getStringValue(path.parent); + const testName = getStringValue(path.parent); - tests.push({ - name: testName, - suites: currentSuite - .filter(s => getEndLineNumber({ container: s }) >= getLineNumber(path)) - .map(s => getStringValue(s)), - updatePoint: getUpdatePoint(path.parent), - line: getLineNumber(path), - code, - file, - tags: playwright.getTestTags(path.parentPath), - skipped: !!currentSuite.filter(s => s.skipped).length, - }); + tests.push({ + name: testName, + suites: currentSuite + .filter(s => getEndLineNumber({ container: s }) >= getLineNumber(path)) + .map(s => getStringValue(s)), + updatePoint: getUpdatePoint(path.parent), + line: getLineNumber(path), + code, + file, + tags: playwright.getTestTags(path.parentPath), + skipped: !!currentSuite.filter(s => s.skipped).length, + }); + + // stop the loop if the test is found + break; + } } if (path.isIdentifier({ name: 'each' })) { From bc6da1e49e2bad0dfc94459994100100dca513fc Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:09:12 +0200 Subject: [PATCH 2/9] fix unit test --- src/lib/frameworks/playwright.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/frameworks/playwright.js b/src/lib/frameworks/playwright.js index 079c0b0e..773f2baa 100644 --- a/src/lib/frameworks/playwright.js +++ b/src/lib/frameworks/playwright.js @@ -186,7 +186,7 @@ module.exports = (ast, file = '', source = '', opts = {}) => { } } - const fixtureNames = [...['test', 'it'], ...opts?.alias]; + const fixtureNames = [...['test', 'it'], ...(opts?.alias || [])]; for (const fiixtureName of fixtureNames || []) { if (path.isIdentifier({ name: fiixtureName })) { if (!hasStringOrTemplateArgument(path.parent)) return; From 73993ebbdb621e4bd5779b35f6fd49eb4e83bb39 Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:16:30 +0200 Subject: [PATCH 3/9] add unit test --- example/playwright/custom-fixture-name.ts | 14 ++++++++++++++ tests/playwright_test.js | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 example/playwright/custom-fixture-name.ts diff --git a/example/playwright/custom-fixture-name.ts b/example/playwright/custom-fixture-name.ts new file mode 100644 index 00000000..21c5f54e --- /dev/null +++ b/example/playwright/custom-fixture-name.ts @@ -0,0 +1,14 @@ +import { test as base } from '@playwright/test'; +import { test } from '@playwright/test'; + +const customTestName = base.extend<{ someFixture: any }>({ + someFixture: async ({}, use) => { + console.log('custom fixture called'); + await use({ name: 'custom fixture name', value: 'custom fixture value' }); + }, +}); + +customTestName('custom test name is parsed', async ({ someFixture }) => { + console.warn(someFixture.name); + console.warn(someFixture.value); +}); diff --git a/tests/playwright_test.js b/tests/playwright_test.js index d78a8fcc..add8d690 100644 --- a/tests/playwright_test.js +++ b/tests/playwright_test.js @@ -465,4 +465,12 @@ test.describe.only('my test', () => { expect(tests[0].code).to.not.include('8: test.beforeEach(async ({ page }) => {\n'); }); }); + + it('should parse playwright test with custom alias (fixture/test name)', () => { + source = fs.readFileSync('./example/playwright/custom-fixture-name.ts').toString(); + ast = jsParser.parse(source, { sourceType: 'unambiguous' }); + const tests = playwrightParser(ast, '', source, { alias: ['customTestName'] }); + + expect(tests.length).to.equal(1); + }); }); From 975cfecd9830921227770a36973a7fd98a5a17da Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:19:27 +0200 Subject: [PATCH 4/9] upd text --- bin/check.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/check.js b/bin/check.js index 0e39b068..562969e2 100755 --- a/bin/check.js +++ b/bin/check.js @@ -34,7 +34,7 @@ program .option('--clean-ids', 'Remove testomatio ids from test and suite') .option('--no-hooks', 'Exclude test hooks code from the code on the client') .option('--line-numbers', 'Adding an extra line number to each block of code') - .option('--alias ', 'Specify custom test name for Playwright tests') + .option('--alias ', 'Specify custom fixture names for Playwright tests (separated by commas)') .action(async (framework, files, opts) => { framework = framework.toLowerCase(); opts.alias = opts.alias ? opts.alias.split(',') : []; From 128b3a665d4037480a30564205b46b062ddb778b Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:28:09 +0200 Subject: [PATCH 5/9] 0.10.0-beta-alias --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 689deb18..cbbc294a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "check-tests", - "version": "0.9.6-beta1.prepend", + "version": "0.10.0-beta-alias", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "check-tests", - "version": "0.9.6-beta1.prepend", + "version": "0.10.0-beta-alias", "license": "MIT", "dependencies": { "@babel/core": "^7.15.5", diff --git a/package.json b/package.json index 991a82db..d1bb9a30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "check-tests", - "version": "0.9.6", + "version": "0.10.0-beta-alias", "description": "Static analysis for tests. Prints all tests in console and fails when exclusive or skipped tests found.", "keywords": [ "testing", From 6186848934764a5e11eae75f6fabd73b211d853d Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:53:11 +0200 Subject: [PATCH 6/9] rename alias to test-alias --- bin/check.js | 4 ++-- src/lib/frameworks/playwright.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/check.js b/bin/check.js index 562969e2..7e72646d 100755 --- a/bin/check.js +++ b/bin/check.js @@ -34,10 +34,10 @@ program .option('--clean-ids', 'Remove testomatio ids from test and suite') .option('--no-hooks', 'Exclude test hooks code from the code on the client') .option('--line-numbers', 'Adding an extra line number to each block of code') - .option('--alias ', 'Specify custom fixture names for Playwright tests (separated by commas)') + .option('--test-alias ', 'Specify custom fixture names for Playwright tests (separated by commas)') .action(async (framework, files, opts) => { framework = framework.toLowerCase(); - opts.alias = opts.alias ? opts.alias.split(',') : []; + opts.testAlias = opts.testAlias ? opts.testAlias.split(',') : []; opts.framework = framework; opts.pattern = files; opts.pwCustomFixtureNames = opts.pwCustomFixtureNames ? opts.pwCustomFixtureNames.split(',') : []; diff --git a/src/lib/frameworks/playwright.js b/src/lib/frameworks/playwright.js index 773f2baa..e734583d 100644 --- a/src/lib/frameworks/playwright.js +++ b/src/lib/frameworks/playwright.js @@ -186,7 +186,7 @@ module.exports = (ast, file = '', source = '', opts = {}) => { } } - const fixtureNames = [...['test', 'it'], ...(opts?.alias || [])]; + const fixtureNames = [...['test', 'it'], ...(opts?.testAlias || [])]; for (const fiixtureName of fixtureNames || []) { if (path.isIdentifier({ name: fiixtureName })) { if (!hasStringOrTemplateArgument(path.parent)) return; From 0cefa3d3e17f69b3931dd3c87e89bd060da4eb54 Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:55:18 +0200 Subject: [PATCH 7/9] fix unit test --- tests/playwright_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/playwright_test.js b/tests/playwright_test.js index add8d690..fd6e0488 100644 --- a/tests/playwright_test.js +++ b/tests/playwright_test.js @@ -469,7 +469,7 @@ test.describe.only('my test', () => { it('should parse playwright test with custom alias (fixture/test name)', () => { source = fs.readFileSync('./example/playwright/custom-fixture-name.ts').toString(); ast = jsParser.parse(source, { sourceType: 'unambiguous' }); - const tests = playwrightParser(ast, '', source, { alias: ['customTestName'] }); + const tests = playwrightParser(ast, '', source, { testAlias: ['customTestName'] }); expect(tests.length).to.equal(1); }); From 13d2c752afa73aa96227cfd0ea19f70417738245 Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Mon, 11 Nov 2024 20:58:58 +0200 Subject: [PATCH 8/9] remove pwCustomFixtureNames --- bin/check.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/check.js b/bin/check.js index 7e72646d..c8d8a8e4 100755 --- a/bin/check.js +++ b/bin/check.js @@ -34,13 +34,12 @@ program .option('--clean-ids', 'Remove testomatio ids from test and suite') .option('--no-hooks', 'Exclude test hooks code from the code on the client') .option('--line-numbers', 'Adding an extra line number to each block of code') - .option('--test-alias ', 'Specify custom fixture names for Playwright tests (separated by commas)') + .option('--test-alias ', 'Specify custom alias for test/it etc (separated by commas if multiple)') .action(async (framework, files, opts) => { framework = framework.toLowerCase(); opts.testAlias = opts.testAlias ? opts.testAlias.split(',') : []; opts.framework = framework; opts.pattern = files; - opts.pwCustomFixtureNames = opts.pwCustomFixtureNames ? opts.pwCustomFixtureNames.split(',') : []; const frameworkOpts = {}; if (opts.lineNumbers) { From 8ab9e7b7cd6a274fceb3be242643523501d65a2c Mon Sep 17 00:00:00 2001 From: Oleksandr Pelykh Date: Tue, 12 Nov 2024 23:43:25 +0200 Subject: [PATCH 9/9] add readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index d1442a7f..b611c427 100644 --- a/README.md +++ b/README.md @@ -607,6 +607,14 @@ Now tests TypeScript can be imported with `--typescript` option: TESTOMATIO=11111111 npx check-tests CodeceptJS "**/*{.,_}{test,spec}.js" --typescript ``` +## Test aliases + +Test aliases are used to map tests in source code to tests in Testomat.io. By default `test` and `it` are parsed. But if you rename them or use another function to define tests (e.g. created/extended test object in Playwright), you can add alias (or multiple aliases, separated by comma) via `--alias` option: + +``` +TESTOMATIO=11111111 npx check-tests Playwright "**/*{.,_}{test,spec}.ts" --alias myTest,myCustomFunction +``` + ## Programmatic API Import Analyzer from module: