Skip to content

#377 - Automatically support test and expect aliases with variable references #386

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions src/rules/expect-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,26 @@ runRuleTester('expect-expect', rule, {
code: 'test("should fail", () => {});',
errors: [{ messageId: 'noAssertions', type: 'Identifier' }],
},
{
code: javascript`
import { test as stuff } from '@playwright/test';
stuff("should fail", () => {});
`,
errors: [{ messageId: 'noAssertions', type: 'Identifier' }],
name: 'Imported alias for test without assertions',
},
{
code: 'test.skip("should fail", () => {});',
errors: [{ messageId: 'noAssertions', type: 'MemberExpression' }],
},
{
code: javascript`
import { test as stuff } from '@playwright/test';
stuff.skip("should fail", () => {});
`,
errors: [{ messageId: 'noAssertions', type: 'MemberExpression' }],
name: 'Imported alias for test without assertions',
},
{
code: javascript`
test('should fail', async ({ page }) => {
Expand Down Expand Up @@ -110,6 +126,34 @@ runRuleTester('expect-expect', rule, {
name: 'Custom assert class method',
options: [{ assertFunctionNames: ['assertCustomCondition'] }],
},
{
code: javascript`
import { test as stuff, expect as check } from '@playwright/test';
stuff('works', () => { check(1).toBe(1); });
`,
name: 'Imported aliases for test and expect',
},
{
code: javascript`
import { test as stuff } from '@playwright/test';
stuff('works', () => { stuff.expect(1).toBe(1); });
`,
name: 'Aliased test with property expect',
},
{
code: javascript`
import { test as stuff, expect } from '@playwright/test';
stuff('works', () => { expect(1).toBe(1); });
`,
name: 'Aliased test with direct expect',
},
{
code: javascript`
import { test, expect as check } from '@playwright/test';
test('works', () => { check(1).toBe(1); });
`,
name: 'Direct test with aliased expect',
},
{
code: 'it("should pass", () => expect(true).toBeDefined())',
name: 'Global alias - test',
Expand Down
19 changes: 19 additions & 0 deletions src/rules/no-commented-out-tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ runRuleTester('no-commented-out-tests', rule, {
},
},
},
// Duplicate alias coming from both settings and import aliases should not
// create redundant entries in the regex and should still be detected.
{
code: javascript`
import { test as it } from '@playwright/test';
// it("foo", () => {});
`,
errors: [{ column: 1, line: 2, messageId }],
settings: {
playwright: {
globalAliases: { test: ['it'] },
},
},
},
],
valid: [
'// foo("bar", function () {})',
Expand Down Expand Up @@ -143,5 +157,10 @@ runRuleTester('no-commented-out-tests', rule, {
},
},
},
// Imported alias for test used properly and not commented
javascript`
import { test as stuff, expect as check } from '@playwright/test';
stuff('foo', () => { check(1).toBe(1); });
`,
],
})
5 changes: 4 additions & 1 deletion src/rules/no-commented-out-tests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Rule } from 'eslint'
import * as ESTree from 'estree'
import { getImportedAliases } from '../utils/ast.js'
import { createRule } from '../utils/createRule.js'

function getTestNames(context: Rule.RuleContext) {
const aliases = context.settings.playwright?.globalAliases?.test ?? []
return ['test', ...aliases]
const importAliases = getImportedAliases(context, 'test')
const combined = ['test', ...aliases, ...importAliases]
return Array.from(new Set(combined))
}

function hasTests(context: Rule.RuleContext, node: ESTree.Comment) {
Expand Down
16 changes: 16 additions & 0 deletions src/rules/no-standalone-expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ runRuleTester('no-standalone-expect', rule, {
code: 'test.describe("a test", () => { expect(1).toBe(1); });',
errors: [{ column: 33, endColumn: 50, messageId }],
},
{
code: javascript`
import { expect as check } from '@playwright/test';
check(1).toBe(1);
`,
errors: [{ messageId }],
name: 'Imported expect alias outside of test',
},
{
code: javascript`
import { test as stuff, expect as check } from '@playwright/test';
stuff.describe('a test', () => { check(1).toBe(1); });
`,
errors: [{ messageId }],
name: 'Aliased expect inside aliased describe should be flagged',
},
{
code: 'test.describe("a test", () => { expect.soft(1).toBe(1); });',
errors: [{ column: 33, endColumn: 55, messageId }],
Expand Down
39 changes: 39 additions & 0 deletions src/utils/ast.test.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test isn't really helpful, let's remove it.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Rule } from 'eslint'
import { describe, expect, it } from 'vitest'
import { getImportedAliases } from './ast.js'

describe('getImportedAliases', () => {
it('supports ImportSpecifier.imported as Literal', () => {
// Intentionally construct a nonstandard/synthetic AST: in valid JS parsed by
// standard ESTree parsers, `ImportSpecifier.imported` is an Identifier.
// Here we use a Literal ('test') to ensure `getImportedAliases` gracefully
// handles such shapes and still treats `import { test as it }` as aliasing.
const program: any = {
body: [
{
source: {
raw: "'@playwright/test'",
type: 'Literal',
value: '@playwright/test',
},
specifiers: [
{
imported: { raw: "'test'", type: 'Literal', value: 'test' },
local: { name: 'it', type: 'Identifier' },
type: 'ImportSpecifier',
},
],
type: 'ImportDeclaration',
},
],
sourceType: 'module',
type: 'Program',
}

const context = {
sourceCode: { ast: program },
} as unknown as Rule.RuleContext
const aliases = getImportedAliases(context, 'test')
expect(aliases).toEqual(['it'])
})
})
47 changes: 47 additions & 0 deletions src/utils/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,50 @@ export function dereference(

return expr?.right ?? decl?.init
}

/**
* Returns local alias names imported from `@playwright/test` for a given named
* import.
*
* For example, for `import { test as foo } from '@playwright/test'`,
* `getImportedAliases(context, 'test')` will return `['foo']`.
*/
export function getImportedAliases(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't tell the full story. What if for example you want to do this:

const custom = test.extend()

which is very common in Playwright. With this method, that wouldn't be caught.

We shouldn't just use AST parsing, we should find any test import, and then track all of it's references including .extend assignments, and then those are the valid aliases.

context: Rule.RuleContext,
importedName: string,
): string[] {
const program = context.sourceCode.ast
const aliases = new Set<string>()

if (program.type !== 'Program') return []

for (const stmt of program.body) {
if (stmt.type !== 'ImportDeclaration') continue
if (
!isStringLiteral(stmt.source) ||
stmt.source.value !== '@playwright/test'
)
continue
if ((stmt as any).importKind === 'type') continue

for (const spec of stmt.specifiers) {
if (spec.type !== 'ImportSpecifier') continue
if ((spec as any).importKind === 'type') continue
const importedNode = spec.imported as ESTree.Identifier | ESTree.Literal
const imported =
importedNode.type === 'Identifier'
? importedNode.name
: typeof importedNode.value === 'string'
? importedNode.value
: undefined
if (imported !== importedName) continue

const localName = spec.local.name
if (localName !== imported) {
aliases.add(localName)
}
}
}

return [...aliases]
}
Loading