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

add playwright annotations #194

Merged
merged 6 commits into from
Nov 21, 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
29 changes: 29 additions & 0 deletions example/playwright/annotating_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import test from '@playwright/test';

test(
'test with a single annotation',
{
annotation: {
type: 'annotation type',
description: 'annotation description',
},
},
async ({ page }) => {},
);

test(
'test with multiple annotations',
{
annotation: [
{
type: 'annotationType1',
description: 'annotationDescription1',
},
{
type: 'annotationType2',
description: 'annotationDescription2',
},
],
},
async ({ page }) => {},
);
3 changes: 2 additions & 1 deletion src/lib/frameworks/playwright.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,8 @@ module.exports = (ast, file = '', source = '', opts = {}) => {
line: getLineNumber(path),
code,
file,
tags: playwright.getTestTags(path.parentPath),
tags: playwright.getTestProps(path.parentPath).tags,
annotations: playwright.getTestProps(path.parentPath).annotations,
skipped: !!currentSuite.filter(s => s.skipped).length,
});

Expand Down
42 changes: 34 additions & 8 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,21 @@ function cleanAtPoint(subject, replaceAt, cleanSubject) {
}

const playwright = {
getTestTags: path => {
getTestProps: path => {
const testProps = { annotations: [], tags: [] };
const argumentsList = path.parent.expression.arguments;
if (!argumentsList?.length) return [];
if (!argumentsList?.length) return testProps;
const argumentsWithTags = argumentsList.filter(arg => arg.type === 'ObjectExpression');
if (!argumentsWithTags.length) return [];
if (!argumentsWithTags.length) return testProps;
const properties = argumentsWithTags.map(arg => arg.properties);
if (!properties.length) return [];
if (!properties.length) return testProps;

const propertiesWithTags = properties.flat().filter(prop => prop.key.name === 'tag');
if (!propertiesWithTags.length) return [];
const propertiesWithAnnotations = properties.flat().filter(prop => prop.key.name === 'annotation');

// parse TAGS
// prop value could be a string or an array of strings
let tags = propertiesWithTags
const tagsList = propertiesWithTags
.map(prop => {
if (prop.value.type === 'ArrayExpression') {
return prop.value.elements.map(el => el.value);
Expand All @@ -155,12 +158,35 @@ const playwright = {
// remove empty values
.filter(Boolean);

// parse ANNOTATIONS

propertiesWithAnnotations.forEach(prop => {
// annotations as array: [{type: 'text, description?: 'text'}]
if (prop.value.type === 'ArrayExpression') {
const annotationProperties = prop.value.elements.map(el => el.properties);
annotationProperties.forEach(annotationProp => {
const annotation = {};
annotationProp.forEach(prop => {
annotation[prop.key.name] = prop.value.value;
});
testProps.annotations.push(annotation);
});
// single annotation: {type: 'text, description?: 'text'}
} else if (prop.value.type === 'ObjectExpression') {
const annotation = {};
prop.value.properties.forEach(prop => {
annotation[prop.key.name] = prop.value.value;
});
testProps.annotations.push(annotation);
}
});

// remove @ at start of each tag
tags = tags.map(tag => {
testProps.tags = tagsList.map(tag => {
return tag.startsWith('@') ? tag.substring(1) : tag;
});

return tags;
return testProps;
},
};

Expand Down
31 changes: 31 additions & 0 deletions tests/playwright_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,37 @@ test.describe.only('my test', () => {
expect(tests[4].tags).to.have.all.members(['smoke', 'regression', 'windows']);
});
});

describe('annotations', () => {
it('should parse playwright test with single annotation', () => {
source = fs.readFileSync('./example/playwright/annotating_test.ts').toString();
ast = jsParser.parse(source, { sourceType: 'unambiguous' });
const tests = playwrightParser(ast, '', source);
expect(tests[0].annotations).to.deep.equal([
{
type: 'annotation type',
description: 'annotation description',
},
]);
});

it('should parse playwright test with multiple annotations', () => {
source = fs.readFileSync('./example/playwright/annotating_test.ts').toString();
ast = jsParser.parse(source, { sourceType: 'unambiguous' });
const tests = playwrightParser(ast, '', source);
expect(tests[1].annotations).to.deep.equal([
{
type: 'annotationType1',
description: 'annotationDescription1',
},
{
type: 'annotationType2',
description: 'annotationDescription2',
},
]);
});
});

it('should parse playwright-ts tests with params', () => {
source = fs.readFileSync('./example/playwright/params.ts').toString();
const program = tsParser.parse(source, {
Expand Down
Loading