# vue-test-attribute

> Vue 2 directive that adds data-test attribute to elements

Latest version **1.3.1** (published 2017-12-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install vue-test-attribute
pnpm add vue-test-attribute
yarn add vue-test-attribute
bun add vue-test-attribute
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.1 |
| Published | 2017-12-16 |
| First published | 2017-11-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Paul Salaets |
| Maintainers | psalaets |
| Keywords | vue, test, attribute |

## Links

- npm: https://www.npmjs.com/package/vue-test-attribute
- Repository: https://github.com/psalaets/vue-test-attribute
- Issues: https://github.com/psalaets/vue-test-attribute/issues
- npm.io page: https://npm.io/package/vue-test-attribute

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 1.3.1 (latest) — 2017-12-16
- 1.3.0 — 2017-12-02
- 1.2.0 — 2017-12-01
- 1.1.0 — 2017-11-25
- 1.0.1 — 2017-11-25
- 1.0.0 — 2017-11-25

## README

# vue-test-attribute

Vue 2 directive that adds `data-test` attribute to elements

## Install

```bash
npm install vue-test-attribute
```

## Usage

### 1. Bring it in to your app

```js
import Vue from 'vue';
import VueTestAttribute from 'vue-test-attribute';

Vue.use(VueTestAttribute);
```

### 2a. Use with string literal

```html
<div v-test="'address'"></div>
```

renders

```html
<div data-test="address"></div>
```

### 2b. Use with string value

```html
<div v-test="testId"></div>
```

```js
data() {
  return {
    testId: 'address'
  };
}
```

renders

```html
<div data-test="address"></div>
```

### 2c. Use with array value

```html
<div v-test="testIds"></div>
```

```js
data() {
  return {
    testIds: ['address', 'address-1']
  };
}
```

renders

```html
<div data-test="address address-1"></div>
```

### 2d. Use with `false` value

```html
<div v-test="testId"></div>
```

```js
data() {
  return {
    testId: false
  };
}
```

renders

```html
<div></div>
```

(`data-test` is not added to element)

### 3. Find element in a test

Using an [attribute selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors).

```js
const el = document.querySelector('[data-test~="address"]');

assert(...);
```

## Settings

You can pass an object of settings to `Vue.use()` to control this directive's behavior.

```js
const settings = {...};

Vue.use(VueTestAttribute, settings);
```

### settings.production

| Value     | Behavior                                                              |
|-----------|-----------------------------------------------------------------------|
| `false`   | `data-test` attributes will be added                                  |
| `true`    | `data-test` attributes will *not* be added                            |
| (not set) | Behavior is based on value of `process.env.NODE_ENV === "production"` |

## Server-side rendering

```js
import VueTestAttribute from 'vue-test-attribute';
import {createRenderer} from 'vue-server-renderer';

const renderer = createRenderer({
  directives: {
    // configure renderer with VueTestAttribute
    test: VueTestAttribute.ssr()
  }
});
```

`VueTestAttribute.ssr()` accepts an optional settings object. See above for details on settings.

## FAQ

### Q1: Why not just find elements by content/class/id/xpath in tests?

A1: https://blog.kentcdodds.com/making-your-ui-tests-resilient-to-change-d37a6ee37269

### Q2: Why not just use

```vue
<div :data-test="testId" />
```

A2: That will work fine but with this directive

- If you typo the directive name Vue will report an error
- It's easier to ensure uniformity in the `data-` attribute used
- Arrays are joined for you
- `data-test` attributes won't be added in production builds

## License

MIT

---
_Source: https://npm.io/package/vue-test-attribute · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
