npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lazy-koala/vite-plugin-auto-testid

v1.0.0

Published

Auto-add data-testid and role attributes to Vue SFC template elements with event handlers

Readme

vite-plugin-auto-testid

Vite 插件,自动为 Vue SFC 模板中含有事件绑定的元素添加 data-testidrole 属性,提升自动化测试可行性与无障碍访问性。

功能

  • AST 级别解析模板,非正则匹配,准确可靠
  • 自动检测 @click / v-on:click 等事件绑定
  • 为可交互元素添加 data-testid 属性(值由组件名 + 事件处理函数名组成)
  • 为缺少显式 role 的非语义化可交互元素自动添加 role 属性
  • 支持自定义 role 与事件的映射规则
  • 支持生产环境自动禁用

安装

npm install @lazy-koala/vite-plugin-auto-testid -D

注意:本插件依赖 @vue/compiler-sfc@vue/compiler-dom,项目需已安装 vue

使用

// vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import autoTestId from '@lazy-koala/vite-plugin-auto-testid'

export default defineConfig({
  plugins: [
    vue(),
    autoTestId()
  ]
})

效果

输入

<template>
  <img @click="handleConfirm" />
  <button @click="handleConfirm">确认</button>
  <div @click="count++">{{ count }}</div>
</template>

输出

<template>
  <img role="button" data-testid="mycomponent_handleconfirm" @click="handleConfirm" />
  <button data-testid="mycomponent_handleconfirm" @click="handleConfirm">确认</button>
  <div role="button" data-testid="mycomponent-click-1" @click="count++">{{ count }}</div>
</template>

配置

import autoTestId from '@lazy-koala/vite-plugin-auto-testid'

autoTestId({
  // 指定禁用插件的环境模式,默认 ['production']
  disableEnv: ['production'],

  // role 与事件映射规则
  // key: role 属性值, value: 触发事件名数组(不带 @ 前缀)
  // 默认 { button: ['click'] }
  roleRules: {
    button: ['click'],
    link: ['click'],
    submitter: ['submit']
  },

  // 自定义测试属性名,默认 'data-testid'
  testIdAttribute: 'data-testid',
})

testid 生成规则

| 场景 | 规则 | 示例 | |---|---|---| | 简单函数名 @click="handleClick" | {组件小写}_{函数小写} | mybutton_handleclick | | 内联表达式 @click="count++" | {组件小写}-click-{序号} | mybutton-click-1 | | 已有 data-testid | 跳过,不覆盖 | - |

role 添加规则

  • 非语义化可交互标签(divimgspan 等)→ 根据 roleRules 添加 role
  • 原生语义标签(buttonainput 等)已具备隐式 role → 跳过
  • 已有显式 role 属性 → 不覆盖

License

MIT