Skip to content

feat: add @vitest/eslint-plugin when using vitest #559

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

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: add @vitest/eslint-plugin when using vitest
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
commit 14ed7aa2295da1fa32e84ae16fe71d32d0ffdc5c
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/legacy-recommended'
])
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/legacy-recommended']
}
]

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

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