I'm using Jest for unit test in a vuejs2 project but got stuck in mocking howler.js, a library imported in my component.
Suppose I have a component named Player.vue
<template>
<div class="player">
<button class="player-button" @click="play">Player</button>
</div>
</template>
<script>
import { Howl } from 'howler';
export default {
name: 'audioplayer',
methods: {
play() {
console.log('player button clicked');
new Howl({
src: [ 'whatever.wav' ],
}).play();
}
}
}
</script>
then I have its test file named Player.spec.js
. Test code was written based on the answer here, but the test failed since called
wasn't set as true. It seems that mocked constructor won't be called when running this test.
import Player from './Player';
import Vue from 'vue';
describe('Player', () => {
let called = false;
jest.mock('howler', () => ({
Howl({ src }) {
this.play = () => {
called = true;
console.log(`playing ${src[0]} now`);
};
},
}));
test('should work', () => {
const Constructor = Vue.extend(Player);
const vm = new Constructor().$mount();
vm.$el.querySelector('.player-button').click();
expect(called).toBeTruthy(); // => will fail
})
})
Though I'm using Vuejs here, I considered it as a more general question related to the usage of Jest's mock
API, but I'm not able to get further.