Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/playwright improvements #167

Merged
merged 6 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions example/playwright/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ test.describe.parallel('Main suite parallel option', async () => {
});
});
});

test.describe.serial('Main suite serial option', async () => {
test('test case #1', async ({ page }) => {
await test.step('[Check 1] Open page and confirm title', async () => {
await page.goto('https://todomvc.com/examples/vanilla-es6/');
});
});
});
14 changes: 7 additions & 7 deletions src/lib/frameworks/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ module.exports = (ast, file = '', source = '', opts = {}) => {
if (['describe', 'it', 'context', 'test'].includes(name)) {
const line = getLineNumber(path);
throw new CommentError(
'Exclusive tests detected. `.only` call found in ' +
`${file}:${line}\n` +
'Remove `.only` to restore test checks',
'Exclusive tests detected. `.only` call found in '
+ `${file}:${line}\n`
+ 'Remove `.only` to restore test checks',
);
}
}
Expand Down Expand Up @@ -137,10 +137,10 @@ module.exports = (ast, file = '', source = '', opts = {}) => {

code = noHooks
? getCode(source, getLineNumber(path), getEndLineNumber(path), isLineNumber)
: beforeEachCode +
beforeCode +
getCode(source, getLineNumber(path), getEndLineNumber(path), isLineNumber) +
afterCode;
: beforeEachCode
+ beforeCode
+ getCode(source, getLineNumber(path), getEndLineNumber(path), isLineNumber)
+ afterCode;

const testName = getStringValue(path.parent);
tests.push({
Expand Down
12 changes: 11 additions & 1 deletion src/lib/frameworks/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@ module.exports = (ast, file = '', source = '', opts = {}) => {
addSuite(path.parentPath.container);
}

if (path.isIdentifier({ name: 'serial' })) {
if (!path.parentPath && !path.parentPath.container) return;
if (!hasStringOrTemplateArgument(path.parentPath.container)) return;
addSuite(path.parentPath.container);
}

// forbid only
if (path.isIdentifier({ name: 'only' })) {
if (!path.parent || !path.parent.object) {
return;
}

const name = path.parent.object.name || path.parent.object.callee.object.name;
const name =
path.parent?.object?.name ||
path.parent?.object?.callee?.object?.name ||
path.container?.object?.property?.name;

if (['describe', 'it', 'context', 'test'].includes(name)) {
const line = getLineNumber(path);
throw new CommentError(
Expand Down
3 changes: 1 addition & 2 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ function replaceAtPoint(subject, replaceAt, replaceTo) {
if (updateLine.includes('|')) {
lines[replaceAt.line - 1] = updateLine.replace(' |', `${replaceTo} |`);
} else {
lines[replaceAt.line - 1] =
updateLine.substring(0, replaceAt.column) + replaceTo + updateLine.substring(replaceAt.column);
lines[replaceAt.line - 1] = updateLine.substring(0, replaceAt.column) + replaceTo + updateLine.substring(replaceAt.column);
}
return lines.join('\n');
}
Expand Down
37 changes: 36 additions & 1 deletion tests/playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const tsParser = require('@typescript-eslint/typescript-estree');
const fs = require('fs');
const { expect } = require('chai');
const playwrightParser = require('../src/lib/frameworks/playwright');
const { assert } = require('console');

let source;
let ast;
Expand All @@ -13,11 +14,38 @@ describe('playwright parser', () => {
ast = jsParser.parse(source, { sourceType: 'unambiguous' });
const tests = playwrightParser(ast, '', source);
expect(tests[0]).to.include.key('code');
expect(tests.length).to.equal(3);
expect(tests.length).to.equal(4);
expect(tests[0].code).to.include("test('basic");
expect(tests[0].name).to.equal('basic test');
});

it('should forbid describe.only tests', () => {
source = `
test.describe.only('my test', () => {
test('my test', async ({ page }) => {
await page.goto('https://playwright.dev/');
});
});
`;

const program = tsParser.parse(source, {
sourceType: 'unambiguous',
loc: true,
range: true,
tokens: true,
});
ast = {
program,
type: 'File',
};
try {
playwrightParser(ast, '', source);
assert.fail('Expected an error');
} catch (err) {
expect(err.message).to.include('Exclusive tests detected');
}
});

it('should parse basic playwright-ts tests', () => {
source = fs.readFileSync('./example/playwright/basic.ts').toString();
const program = tsParser.parse(source, {
Expand Down Expand Up @@ -357,6 +385,13 @@ describe('playwright parser', () => {

expect(tests[2].suites[0]).to.equal('Main suite parallel option');
});
it('should return suite name if used test.describe.serial mode', () => {
source = fs.readFileSync('./example/playwright/basic.js').toString();
ast = jsParser.parse(source, { sourceType: 'unambiguous' });
const tests = playwrightParser(ast, '', source);

expect(tests[3].suites[0]).to.equal('Main suite serial option');
});
});

context('test with --line-numbers option', () => {
Expand Down
Loading