Giter Club home page Giter Club logo

vue-test-declarative's Introduction

Introduction

Vue unit testing EZ-mode.

Installation

vue add @vue/unit-jest
vue add @vue/unit-mocha
yarn add --dev vue-test-declarative babel-register babel-polyfill browser-env

Usage

  1. Create a file HelloWorld.vuetest in tests/declarative:
<tests for="@/components/HelloWorld.vue">
  <test name="Contains welcome message">
    <expect text to-match="Welcome" />
  </test>
</tests>
  1. Run npm run test:declarative

More details are available in the API Docs.

Examples

Props

For this example, we'll test the HelloWorld component from the default vue-cli template that we've all seen when starting a new project.

Create a HelloWorld.vuetest file in the tests/declarative directory with these contents:

<tests for="@/components/HelloWorld.vue">
  <test name="Render message correctly" :props="props">
    <expect text to-match="Success!" />
  </test>
</tests>

<script>
let context = {
  props: {
    msg: 'Success!',
  }
};
</script>

This simple test expects for the component text to match "Welcome!" in the rendered HelloWorld component. This component uses a prop named msg to display a welcome message, so we pass one in using our script section.

See all tags and options in the API docs.

Interactions

Here is a test for the official TodoMVC example that shows how interactions (set/trigger) work:

<test name="add a todo">
  <set selector=".new-todo" value="First" />
  <trigger selector=".new-todo" event="keyup.enter" />
  <expect text-of=".todo-list li" to-match="First" />
  <expect text-of=".todo-count" to-match="1 item left" />
</test>

Run tests

npm run test:declarative

This command will generate and run mocha tests for all .vuetest files in your test path (defaults to tests/declarative).

npm run test:declarative -- --keep

This command is the same as above but will not delete the generated mocha tests after running.

vuetest.setup.js

Sometimes your tests will require you to import and register components you are using (like vuetify or element-ui), or run other setup before a test. If you need this functionality, create a vuetest.setup.js file in the tests/declarative directory that defines a variable called localVue. This will be used instead of the default Vue instance when running your tests.

import ElementUI from 'element-ui';
import { createLocalVue } from '@vue/test-utils';

let localVue = createLocalVue();
localVue.use(ElementUI);

Any additional javascript required by your tests can be added to this file, and it will be executed before your tests are run.

In general, any test/component-specific javascript should go into your .vuetest <script> section, while anything global to all tests should go into your vuetest.setup.js file.

Configuration

Create a vuetest.config.json file in your project root. This file may contain options to configure vue-test-declarative. The following options are supported:

testsPath

vue-test-declarative defaults to looking for tests in tests/declarative. Use this config setting if you want to place your .vuetest and vuetest.setup.js files somewhere else.

webpackConfigPath

vue-test-declarative tries to find your webpack config automatically if you are using a vue-cli template. If your webpack.config.js file is in another location, set its path here.

Documentation

๐Ÿ‘‰ API Docs

๐Ÿ‘‰ TodoMVC Example Test Suite

Tips

๐Ÿ’ก Set the syntax highlighting in your editor to vue, vue-html, or xml when working with .vuetest files.

vue-test-declarative's People

Contributors

johnsusek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

vue-test-declarative's Issues

Rename <it> to <test>

I consider a 2 character, non-descriptive, variable names an anti-pattern. This becomes abundantly clear when you look at this from a semantic mark up perspective.

What is a "<footer>"? It is a container that goes at the end of the page.

What is a "<nav>"? It is a container for navigational items.

What is a "<test>"? It is a container for a test.

What is a "<it>"? I have no idea? Is it a container for a scary clown?


Well named functions describe what they do, and in the best scenarios can infer what arguments they accept and even what will be returned. For example: getUserAvatar(). Guess what it does. It gets a user's avatar. What do you think it takes as an argument? Probably a user ID. What do you think it returns? Probably a URL to that user's avatar.

What does an it() take in? What does an it() return? ยฏ\_(ใƒ„)_/ยฏ


If you want to also support "it", I don't care, but you should use "test" as the default and recommended approach, similar to Jest.

Wrapper methods

Values to match against

  • attributes
  • classes
  • emitted
  • emittedByOrder
  • html
  • name
  • props
  • text

Actions

  • destroy
  • find
  • findAll
  • setChecked
  • setData
  • setMethods
  • setProps
  • setSelected
  • setValue
  • trigger

Assertions

  • contains
  • exists
  • is
  • isEmpty
  • isVisible
  • isVueInstance

Syntax highlighting for vuetest format

Going to be somewhat similar to .vue files, but easier because tests is just xml and the script tag is just javascript

  • vscode (plugin?)
  • github (alias for .vue)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.