-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vitest/require-local-test-context-for-concurrent-snapshots): add…
… rule (#315)
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
docs/rules/require-local-test-context-for-concurrent-snapshots.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Require local Test Context for concurrent snapshot tests (`vitest/require-local-test-context-for-concurrent-snapshots`) | ||
|
||
💼 This rule is enabled in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
test.concurrent('myLogic', () => { | ||
expect(true).toMatchSnapshot(); | ||
}) | ||
|
||
describe.concurrent('something', () => { | ||
test('myLogic', () => { | ||
expect(true).toMatchInlineSnapshot(); | ||
}) | ||
}) | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
test.concurrent('myLogic', ({ expect }) => { | ||
expect(true).toMatchSnapshot(); | ||
}) | ||
|
||
test.concurrent('myLogic', (context) => { | ||
context.expect(true).toMatchSnapshot(); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/rules/require-local-test-context-for-concurrent-snapshots.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"; | ||
import { createEslintRule, getNodeName, isSupportedAccessor } from "../utils"; | ||
import { isTypeOfVitestFnCall } from "../utils/parseVitestFnCall"; | ||
|
||
export const RULE_NAME = "require-local-test-context-for-concurrent-snapshots"; | ||
|
||
export default createEslintRule({ | ||
name: RULE_NAME, | ||
meta: { | ||
docs: { | ||
description: "Require local Test Context for concurrent snapshot tests", | ||
recommended: "error", | ||
}, | ||
messages: { | ||
requireLocalTestContext: "Use local Test Context instead", | ||
}, | ||
type: "problem", | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
const isNotAnAssertion = !isTypeOfVitestFnCall(node, context, ['expect']) | ||
if (isNotAnAssertion) return; | ||
|
||
const isNotASnapshotAssertion = ![ | ||
'toMatchSnapshot', | ||
'toMatchInlineSnapshot' | ||
].includes(node.callee.property.name); | ||
|
||
if (isNotASnapshotAssertion) return; | ||
|
||
const isInsideSequentialDescribeOrTest = !context.getAncestors().some((ancestor) => { | ||
if (ancestor.type !== AST_NODE_TYPES.CallExpression) return false; | ||
|
||
const isNotInsideDescribeOrTest = !isTypeOfVitestFnCall(ancestor, context, ["describe", "test"]); | ||
if (isNotInsideDescribeOrTest) return false; | ||
|
||
const isTestRunningConcurrently = | ||
ancestor.callee.type === AST_NODE_TYPES.MemberExpression && | ||
isSupportedAccessor(ancestor.callee.property, "concurrent"); | ||
|
||
return isTestRunningConcurrently | ||
}); | ||
|
||
if (isInsideSequentialDescribeOrTest) return; | ||
|
||
context.report({ | ||
node, | ||
messageId: "requireLocalTestContext" | ||
}) | ||
}, | ||
}; | ||
}, | ||
}); |
58 changes: 58 additions & 0 deletions
58
tests/require-local-test-context-for-concurrent-snapshots.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import rule, { RULE_NAME } from '../src/rules/require-local-test-context-for-concurrent-snapshots' | ||
import { ruleTester } from "./ruleTester"; | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
'it("something", () => { expect(true).toBe(true) })', | ||
'it.concurrent("something", () => { expect(true).toBe(true) })', | ||
'it("something", () => { expect(1).toMatchSnapshot() })', | ||
'it.concurrent("something", ({ expect }) => { expect(1).toMatchSnapshot() })', | ||
'it.concurrent("something", ({ expect }) => { expect(1).toMatchInlineSnapshot("1") })', | ||
'describe.concurrent("something", () => { it("something", () => { expect(true).toBe(true) }) })', | ||
'describe.concurrent("something", () => { it("something", ({ expect }) => { expect(1).toMatchSnapshot() }) })', | ||
'describe.concurrent("something", () => { it("something", ({ expect }) => { expect(1).toMatchInlineSnapshot() }) })', | ||
'describe("something", () => { it("something", ({ expect }) => { expect(1).toMatchInlineSnapshot() }) })', | ||
'describe("something", () => { it("something", (context) => { expect(1).toMatchInlineSnapshot() }) })', | ||
'describe("something", () => { it("something", (context) => { context.expect(1).toMatchInlineSnapshot() }) })', | ||
'describe("something", () => { it("something", (context) => { expect(1).toMatchInlineSnapshot() }) })', | ||
'it.concurrent("something", (context) => { context.expect(1).toMatchSnapshot() })', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'it.concurrent("should fail", () => { expect(true).toMatchSnapshot() })', | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: 'it.concurrent("should fail", () => { expect(true).toMatchInlineSnapshot("true") })', | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: 'describe.concurrent("failing", () => { it("should fail", () => { expect(true).toMatchSnapshot() }) })', | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: 'describe.concurrent("failing", () => { it("should fail", () => { expect(true).toMatchInlineSnapshot("true") }) })', | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: 'it.concurrent("something", (context) => { expect(true).toMatchSnapshot() })', | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: `it.concurrent("something", () => { | ||
expect(true).toMatchSnapshot(); | ||
expect(true).toMatchSnapshot(); | ||
})`, | ||
errors: [{ messageId: 'requireLocalTestContext' }, { messageId: 'requireLocalTestContext' }] | ||
}, | ||
{ | ||
code: `it.concurrent("something", () => { | ||
expect(true).toBe(true); | ||
expect(true).toMatchSnapshot(); | ||
})`, | ||
errors: [{ messageId: 'requireLocalTestContext' }] | ||
}, | ||
], | ||
}) |