From 0f1e68777ced8929794a50c133e8e9098bbf454d Mon Sep 17 00:00:00 2001 From: Michael Bodnarchuk Date: Tue, 2 Jul 2024 01:53:52 +0300 Subject: [PATCH] Fix/quasi params (#162) * added validation for test titles * fixed quasi strings --- src/lib/frameworks/jest.js | 14 +++++++------- src/lib/frameworks/playwright.js | 20 +++++++++----------- src/lib/utils.js | 6 +++--- tests/playwright_test.js | 16 ++++++++++++++++ 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lib/frameworks/jest.js b/src/lib/frameworks/jest.js index b960a183..f5ff97e1 100644 --- a/src/lib/frameworks/jest.js +++ b/src/lib/frameworks/jest.js @@ -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', ); } } @@ -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({ diff --git a/src/lib/frameworks/playwright.js b/src/lib/frameworks/playwright.js index 13ff69e2..9216c54d 100644 --- a/src/lib/frameworks/playwright.js +++ b/src/lib/frameworks/playwright.js @@ -76,9 +76,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', ); } } @@ -88,8 +88,7 @@ module.exports = (ast, file = '', source = '', opts = {}) => { return; } - const name = - path.parent.object.name || path.parent.object.property.name || path.parent.object.callee.object.name; + const name = path.parent.object.name || path.parent.object.property.name || path.parent.object.callee.object.name; if (name === 'test' || name === 'it') { // test or it @@ -124,8 +123,7 @@ module.exports = (ast, file = '', source = '', opts = {}) => { return; } - const name = - path.parent.object.name || path.parent.object.property.name || path.parent.object.callee.object.name; + const name = path.parent.object.name || path.parent.object.property.name || path.parent.object.callee.object.name; if (name === 'test' || name === 'it') { // test or it @@ -189,10 +187,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); diff --git a/src/lib/utils.js b/src/lib/utils.js index 0a602f62..d2eece2f 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -40,16 +40,16 @@ function getQuasiArgument(path) { for (const node of nodes) { if (node.type === 'MemberExpression') { if (node.property && node.property.type === 'Identifier') { - quasiValue += ' ${' + node.property.name + '} '; // eslint-disable-line prefer-template + quasiValue += '${' + node.property.name + '}'; // eslint-disable-line prefer-template continue; } } if (node.type === 'Identifier') { - quasiValue += ' ${' + node.name + '} '; // eslint-disable-line prefer-template + quasiValue += '${' + node.name + '}'; // eslint-disable-line prefer-template continue; } if (!node.value) continue; - if (node.value.raw) quasiValue += node.value.raw.trim(); + if (node.value.raw) quasiValue += node.value.raw; } return quasiValue; diff --git a/tests/playwright_test.js b/tests/playwright_test.js index 6ac9baf3..47756f87 100644 --- a/tests/playwright_test.js +++ b/tests/playwright_test.js @@ -167,6 +167,22 @@ describe('playwright parser', () => { expect(tests[4].tags).to.have.all.members(['smoke', 'regression', 'windows']); }); }); + it('should parse playwright-ts tests with params', () => { + source = fs.readFileSync('./example/playwright/params.ts').toString(); + const program = tsParser.parse(source, { + sourceType: 'unambiguous', + loc: true, + range: true, + tokens: true, + }); + ast = { + program, + type: 'File', + }; + const tests = playwrightParser(ast, '', source); + + expect(tests[0].name).to.equal('check ${i} on "${pageUrl}" page'); + }); it('should parse playwright-js tests with annotation', () => { source = fs.readFileSync('./example/playwright/annotations.js').toString();