Skip to content

Commit

Permalink
add alias for custom fixtures (#192)
Browse files Browse the repository at this point in the history
* add alias for custom fixtures

* fix unit test

* add unit test

* upd text

* 0.10.0-beta-alias

* rename alias to test-alias

* fix unit test

* remove pwCustomFixtureNames
  • Loading branch information
olexandr13 authored Nov 11, 2024
1 parent fe6dcc5 commit 8425cd0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 35 deletions.
11 changes: 3 additions & 8 deletions bin/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -41,8 +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('--test-alias <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;
const frameworkOpts = {};
Expand All @@ -55,7 +50,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 {
Expand Down
14 changes: 14 additions & 0 deletions example/playwright/custom-fixture-name.ts
Original file line number Diff line number Diff line change
@@ -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);
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
52 changes: 29 additions & 23 deletions src/lib/frameworks/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?.testAlias || [])];
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' })) {
Expand Down
8 changes: 8 additions & 0 deletions tests/playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, { testAlias: ['customTestName'] });

expect(tests.length).to.equal(1);
});
});

0 comments on commit 8425cd0

Please sign in to comment.