Skip to content

Commit

Permalink
test: add tests for more eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 10, 2024
1 parent 9b313ee commit d39a708
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 15 deletions.
71 changes: 71 additions & 0 deletions packages/eslint-plugin/src/rules/blocklist.test.ts
@@ -0,0 +1,71 @@
import * as vueParser from 'vue-eslint-parser'
import { $ as html, run } from 'eslint-vitest-rule-tester'
import rule from './blocklist'

run({
name: 'blocklist',
rule,
languageOptions: {
parser: vueParser,
},

valid: [
html`
<template>
<div class="m1 mx1 mr-1"></div>
</template>
`,
],
invalid: [
{
code: html`
<template>
<div class="border"></div>
</template>
`,
errors: [
{
messageId: 'in-blocklist',
data: {
name: 'border',
reason: '',
},
},
],
},
{
description: 'with message',
code: html`
<template>
<div class="bg-red-500"></div>
</template>
`,
errors: [
{
messageId: 'in-blocklist',
data: {
name: 'bg-red-500',
reason: ': Use bg-red-600 instead',
},
},
],
},
{
description: 'dynamic blocklist with message',
code: html`
<template>
<div class="text-red"></div>
</template>
`,
errors: [
{
messageId: 'in-blocklist',
data: {
name: 'text-red',
reason: ': Use color-* instead',
},
},
],
},
],
})
39 changes: 39 additions & 0 deletions packages/eslint-plugin/src/rules/order.test.ts
@@ -0,0 +1,39 @@
import * as vueParser from 'vue-eslint-parser'
import { $ as html, run } from 'eslint-vitest-rule-tester'
import { expect } from 'vitest'
import rule from './order'

run({
name: 'order',
rule,
languageOptions: {
parser: vueParser,
},

valid: [
html`
<template>
<div class="m1 mx1 mr-1"></div>
</template>
`,
],
invalid: [
{
code: html`
<template>
<div class="mx1 m1 mr-1"></div>
</template>
`,
output: output => expect(output).toMatchInlineSnapshot(`
"<template>
<div class="m1 mx1 mr-1"></div>
</template>"
`),
errors: [
{
messageId: 'invalid-order',
},
],
},
],
})
16 changes: 4 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions test/__snapshots__/postcss.test.ts.snap
Expand Up @@ -2,8 +2,6 @@

exports[`postcss > @apply > basic 1`] = `"div{--un-bg-opacity:1;background-color:rgb(248 113 113 / var(--un-bg-opacity));}.dark div>:focus:hover{font-size:20px;}div:hover{--un-text-opacity:1;color:rgb(255 255 255 / var(--un-text-opacity));}"`;

exports[`postcss > @apply > media 1`] = `"div{}@media (min-width: 640px){div{--un-bg-opacity:1;background-color:rgb(248 113 113 / var(--un-bg-opacity));}}@media (min-width: 768px){div{--un-bg-opacity:1;background-color:rgb(96 165 250 / var(--un-bg-opacity));}}@media (min-width: 1024px){div{--un-bg-opacity:1;background-color:rgb(244 114 182 / var(--un-bg-opacity));}}@media (min-width: 1280px){div{--un-bg-opacity:1;background-color:rgb(255 255 255 / var(--un-bg-opacity));}}"`;

exports[`postcss > @screen 1`] = `
"@media (min-width: 768px) and (max-width: 1023.9px) {
div{--un-bg-opacity:1;background-color:rgb(248 113 113 / var(--un-bg-opacity));}
Expand Down
15 changes: 15 additions & 0 deletions uno.config.ts
@@ -0,0 +1,15 @@
// This file is for testing

// eslint-disable-next-line no-restricted-imports
import { defineConfig, presetUno } from 'unocss'

export default defineConfig({
presets: [
presetUno(),
],
blocklist: [
'border',
['bg-red-500', { message: 'Use bg-red-600 instead' }],
[i => i.startsWith('text-'), { message: 'Use color-* instead' }],
],
})
1 change: 0 additions & 1 deletion vitest.config.ts
Expand Up @@ -13,6 +13,5 @@ export default defineConfig({
name: 'unit',
setupFiles: ['./test/setup.ts'],
exclude: [...defaultExclude, '**/svelte-scoped/**'],
globals: true,
},
})

0 comments on commit d39a708

Please sign in to comment.