[Vuejs]-How to properly test component logic in Vue.js

0👍

We have done unit test using vue-jest. Here are the steps.

  1. Use the following code to set up the crypto property globally. This will allow Jest to access window.crypto and won’t cause any issue.Create a crypto.mock file and paste following code.

    const crypto = require(‘crypto’)

     Object.defineProperty(global.self, 'crypto', {
       value: {
         getRandomValues: arr => crypto.randomBytes(arr.length)
       }
     })
    
  2. Now import this file in your test file component. Now You can use jasmine.Spy to spy on the elements.

     let spyFor: jasmine.Spy; spyFor = spyOn(function.hello, 'hello'); expect(spyFor).toHaveBeenCalledTimes(1);
    

In this way you can mock on the methods. Pls free to ask if you have anything more.

Leave a comment