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

fix(no-identical-title): support suite an alias of describe block #392

Merged
merged 4 commits into from
Mar 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 46 additions & 26 deletions scripts/chain-permutations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// imported from https://github.com/veritem/eslint-plugin-vitest/pull/293
// This script generates all possible permutations for vitest methods
import { per } from 'percom'
import fs from 'node:fs'
import path from 'node:path'

const data = [
{
Expand Down Expand Up @@ -30,6 +32,13 @@ const data = [
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'concurrent', 'sequential', 'shuffle', 'todo'],
last: ['each']
},
{
names: ['suite'],
first: [],
conditions: ['skipIf', 'runIf'],
methods: ['skip', 'only', 'concurrent', 'sequential', 'shuffle', 'todo'],
last: ['each']
}
]

Expand Down Expand Up @@ -57,51 +66,62 @@ data.forEach((q) => {
),
...(i > 0
? q.first.flatMap((first) =>
q.conditions.flatMap((condition) =>
(per(q.methods, i - 1) || ['']).map((p) => [
first,
condition,
...p
])
)
q.conditions.flatMap((condition) =>
(per(q.methods, i - 1) || ['']).map((p) => [
first,
condition,
...p
])
)
)
: []),
...(i > 0
? q.first.flatMap((first) =>
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [first, ...p, last])
)
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [first, ...p, last])
)
)
: []),
...(i > 0
? q.conditions.flatMap((condition) =>
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [
condition,
...p,
last
])
)
)
: []),
...(i > 1
? q.first.flatMap((first) =>
q.conditions.flatMap((condition) =>
q.last.flatMap((last) =>
(per(q.methods, i - 1) || ['']).map((p) => [
(per(q.methods, i - 2) || ['']).map((p) => [
first,
condition,
...p,
last
])
)
)
: []),
...(i > 1
? q.first.flatMap((first) =>
q.conditions.flatMap((condition) =>
q.last.flatMap((last) =>
(per(q.methods, i - 2) || ['']).map((p) => [
first,
condition,
...p,
last
])
)
)
)
)
: [])
])
const allPerms = methodPerms.map((p) => [name, ...p].join('.'))
allPermutations.push(...allPerms)
})
})

console.log(allPermutations)
const extra_rules = ['xtest', 'xtest.each', 'xit', 'xit.each', 'fit', 'xdescribe', 'xdescribe.each', 'fdescribe']

const output = `export const ValidVitestFnCallChains = new Set([${allPermutations.concat(extra_rules).map(item => `'${item}'`)}])`

const new_path = path.resolve(__dirname, '../src/utils/validVitestFnCallChains.ts')

try {
fs.writeFileSync(new_path, output)
console.log(`done writing to ${new_path.split('/')[new_path.split('/').length - 1]}`)
} catch (err) {
console.log(`err: ${err.message}`)
}
4 changes: 2 additions & 2 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "",
"main": "chain-permutations.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"chains": "npx tsx chain-permutations.ts"
},
"keywords": [],
"author": "Verite Mugabo <mugaboverite@gmail.com> (https://veritemugabo.com/)",
"author": "Verite Mugabo <git@veritemugabo.com> (https://veritemugabo.com/)",
"license": "MIT",
"devDependencies": {
"percom": "^1.1.3"
Expand Down
7 changes: 3 additions & 4 deletions src/rules/no-identical-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ export default createEslintRule<Options, MESSAGE_ID>({
fixable: 'code',
schema: [],
messages: {
multipleTestTitle: 'Test is used multiple times in the same describe block',
multipleDescribeTitle: 'Describe is used multiple times in the same describe block'
multipleTestTitle: 'Test is used multiple times in the same describe(suite) block',
multipleDescribeTitle: 'Describe is used multiple times in the same describe(suite) block'
}
},
defaultOptions: [],
create(context) {
const stack = [newDescribeContext()]

return {
CallExpression(node) {
const currentStack = stack[stack.length - 1]
Expand All @@ -43,7 +42,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
if (!vitestFnCall)
return

if (vitestFnCall.name === 'describe')
if (vitestFnCall.name === 'describe' || vitestFnCall.name === 'suite')
stack.push(newDescribeContext())

if (vitestFnCall.members.find(s => isSupportedAccessor(s, 'each')))
Expand Down