Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eslint-vitest-rule-tester

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-vitest-rule-tester

ESLint rule tester with Vitest

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21K
increased by5.49%
Maintainers
1
Weekly downloads
 
Created
Source

eslint-vitest-rule-tester

npm version npm downloads bundle JSDocs License

ESLint rule tester with Vitest.

This module requires ESLint v9.0+.

Install

npm i -D eslint-vitest-rule-tester

Usage

Classical Usage

Simliar style to ESLint's TestRunner (test cases with implicit test suites)

import { run } from 'eslint-vitest-rule-tester'

run('rule-name', rule, {
  valid: [
    // test cases
  ],
  invalid: [
    // test cases
  ],
}, {
  // the default flat configs
  configs: {
    languageOptions: {
      parserOptions: {
        ecmaVersion: 2020,
        sourceType: 'module',
      },
    },
  }
})
Feature Extensions
output

output field can be a function to do custom assertion. This would also be compatible with snapshot testing.

import { run } from 'eslint-vitest-rule-tester'
import { expect } from 'vitest'

run('rule-name', rule, {
  invalid: [
    {
      input: 'let foo = 1',
      output(output) {
        expect(output)
          .toMatchInlineSnapshot(`"const foo = 1;"`)
      },
    },
  ],
})
errors

errors field can be a function to do custom assertion, same as output.

import { run } from 'eslint-vitest-rule-tester'
import { expect } from 'vitest'

run('rule-name', rule, {
  invalid: [
    {
      input: 'let foo = 1',
      errors(errors) {
        expect(errors.map(e => e.messageId))
          .toMatchInlineSnapshot(`["error-message-id"]`)
      },
    },
  ],
})
onResult hook

onResult field can be an function to do custom assertion with the entire result object.

import { run } from 'eslint-vitest-rule-tester'
import { expect } from 'vitest'

run('rule-name', rule, {
  invalid: [
    'let foo = 1',
  ],
  onResult(testCase, result) {
    if (testCase.type === 'invalid')
      expect(result).toMatchSnapshot()
    // here you can't use `toMatchInlineSnapshot` because it's not in the test case
  },
})

Explicit Test Suites

import { createRuleTester } from 'eslint-vitest-rule-tester'
import { describe, expect, it } from 'vitest'

describe('rule-name', () => {
  const { valid, invalid } = createRuleTester({
    name: 'rule-name',
    rule,
    configs: {
      // flat config options
      languageOptions: {
        parserOptions: {
          ecmaVersion: 2020,
          sourceType: 'module',
        },
      },
    }
  })

  it('valid case 1', () => {
    valid('const foo = 1')
  })

  it('invalid case 1 with snapshot', () => {
    const result = invalid({
      input: 'const foo = 1',
      errors: ['error-message-id'],
    })

    expect(result.output).toMatchSnapshot()
  })
})

Sponsors

License

MIT License © 2024-PRESENT Anthony Fu

FAQs

Package last updated on 06 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc