Skip to content

Commit

Permalink
feat: add @vitest/eslint-plugin when using vitest
Browse files Browse the repository at this point in the history
The plugin is now officillay recommended, so let's add it to the eslint config when selecting both eslint and vitest.
  • Loading branch information
cexbrayat committed Aug 14, 2024
1 parent 4612639 commit 892cba1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
20 changes: 20 additions & 0 deletions __test__/renderEslint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getAdditionalConfigAndDependencies } from '../utils/renderEslint'
describe('renderEslint', () => {
it('should get additional dependencies and config with no test flags', () => {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest: false,
needsCypress: false,
needsCypressCT: false,
needsPlaywright: false
Expand All @@ -12,8 +13,25 @@ describe('renderEslint', () => {
expect(additionalDependencies).toStrictEqual({})
})

it('should get additional dependencies and config with for vitest', () => {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest: true,
needsCypress: false,
needsCypressCT: false,
needsPlaywright: false
})
expect(additionalConfig.overrides[0].files).toStrictEqual([
'src/**/*.{test,spec}.{js,ts,jsx,tsx}'
])
expect(additionalConfig.overrides[0].extends).toStrictEqual([
'plugin:@vitest/eslint-plugin/recommended-legacy'
])
expect(additionalDependencies['@vitest/eslint-plugin']).not.toBeUndefined()
})

it('should get additional dependencies and config with for cypress', () => {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest: false,
needsCypress: true,
needsCypressCT: false,
needsPlaywright: false
Expand All @@ -28,6 +46,7 @@ describe('renderEslint', () => {

it('should get additional dependencies and config with for cypress with component testing', () => {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest: false,
needsCypress: true,
needsCypressCT: true,
needsPlaywright: false
Expand All @@ -43,6 +62,7 @@ describe('renderEslint', () => {

it('should get additional dependencies and config with for playwright', () => {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest: false,
needsCypress: false,
needsCypressCT: false,
needsPlaywright: true
Expand Down
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ async function init() {
if (needsEslint) {
renderEslint(root, {
needsTypeScript,
needsVitest,
needsCypress,
needsCypressCT,
needsPrettier,
Expand Down
1 change: 1 addition & 0 deletions template/eslint/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"devDependencies": {
"@vitest/eslint-plugin": "1.0.2",
"eslint-plugin-cypress": "^3.4.0",
"eslint-plugin-playwright": "^1.6.2"
}
Expand Down
15 changes: 14 additions & 1 deletion utils/renderEslint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const eslintDeps = eslintTemplatePackage.devDependencies

export default function renderEslint(
rootDir,
{ needsTypeScript, needsCypress, needsCypressCT, needsPrettier, needsPlaywright }
{ needsTypeScript, needsVitest, needsCypress, needsCypressCT, needsPrettier, needsPlaywright }
) {
const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
needsVitest,
needsCypress,
needsCypressCT,
needsPlaywright
Expand Down Expand Up @@ -64,13 +65,25 @@ export default function renderEslint(

// visible for testing
export function getAdditionalConfigAndDependencies({
needsVitest,
needsCypress,
needsCypressCT,
needsPlaywright
}) {
const additionalConfig: Linter.Config = {}
const additionalDependencies = {}

if (needsVitest) {
additionalConfig.overrides = [
{
files: ['src/**/*.{test,spec}.{js,ts,jsx,tsx}'],
extends: ['plugin:@vitest/eslint-plugin/recommended-legacy']
}
]

additionalDependencies['@vitest/eslint-plugin'] = eslintDeps['@vitest/eslint-plugin']
}

if (needsCypress) {
additionalConfig.overrides = [
{
Expand Down

0 comments on commit 892cba1

Please sign in to comment.