Skip to content

Commit

Permalink
fix(no-identical-title): support suite an alias of describe block (#392)
Browse files Browse the repository at this point in the history
* fix(no-identical-title): support suite an alias of describe block

* feat(scripts): added suite to possible vitest chains

* chore(scripts): added missing chains

* chore(tests): improve tests
  • Loading branch information
veritem committed Mar 12, 2024
1 parent 3b7123e commit 85fca7b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1,074 deletions.
72 changes: 46 additions & 26 deletions scripts/chain-permutations.ts
@@ -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
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
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

0 comments on commit 85fca7b

Please sign in to comment.