Git Product home page Git Product logo

count-sheep's Introduction

Testing with Jest

Configuration

  • Make sure that Jest is ocnfigured in jest.config.js and other config files as neccessary
  • npm i
  • npm i vue-template-compiler vue-jest
  • Tell every file ending in .vue to run through jest
  • "^.+\.vue$": "vue-jest" -npm i @vue/test-utils -npm i jest-serializer-vue
  • Makes snapshots that are human readable -npm i @vue/cli-plugin-unit-jest -D -vue add unit-jest (If testing is not set up in project) -Add testing to already made vue project -npm i flush-promises
  • All test files must end in spec.js

What to test

Test the functionality vs. internals If you are testing a button that increments counter by 1 - test that when clicked, it did indeed increment counter by 1. Do not test, how the counter was incremented.

Writing tests

Mounting

When you mount a component, it returns a wrapper object. example: mount(TestComponent) if you console log that, it would return an wrapper object This give you a lot of methods that you can use for testing. All methods can be found in the vue testing docs under wrapper You can start each test with a new wrapper. You can add this in beforeEach inside of the describe function

Creating a Snapshot

You can create a snapshot and test (check docs) by expect(wrapper.html()).toMatchSnapshot this creates a html skeleton that is then tested by snapshot method

Passing props

You can pass props to the wrapper by using propsData (check docs) property

Accessing data with vm

You can access all data in a wrapper (which is derived from your component) by using wrapper.vm.nameOfDataYouWant an example would be wrapper.vm.marvelMovies - this would return the marvelMovies object that is inside our wrapper

Setting data for testing using setData

You can pull in vars from your component, then add data to them. then re run snapshot tests to see if they are operating correctly.

Adding serializer

You can add jest-serializer-vue to packages. This will make snapshots more human readabale. This also allows you to take the .html method off of wrapper when creating wrappers. code would could change from expect(wrapper.html()).toMatchSnapshot to expect(wrapper()).toMatchSnapshot

count-sheep's People

Contributors

keivanmojmali avatar

Watchers

 avatar

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.