The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Adebayo Oluwadamilola (3) [Avatar] Offline
#1
// ItemList.vue
<script>
import Item from '../components/Item.vue'

export default {
components: {
Item
},
data () {
return {
displayItems: window.items
}
},

beforeMount() {
this.$bar.start()
}
}
</script>

//ItemList.spec.js
import { shallowMount } from '@vue/test-utils'
import ItemList from '../ItemList.vue'
import Item from '../../components/Item.vue'

describe('ItemList.vue', () => {
test('renders an Item with data for each item in window.items', () => {
window.items = [{}, {}, {}]
const wrapper = shallowMount(ItemList)
const items = wrapper.findAll(Item)
expect(items).toHaveLength(window.items.length)
items.wrappers.forEach((wrapper, i) => {
expect(wrapper.props().item).toBe(window.items[i])
})
})

test('calls $bar start on load', () => {
const $bar = {
start: jest.fn(),
finish: () => {}
}
shallowMount(ItemList, {mocks: { $bar }})
expect($bar.start).toHaveBeenCalledTimes(1)
})
})

// main.js
import Vue from 'vue'
import App from './App.vue'
import ProgressBar from './components/ProgressBar.vue'

Vue.config.productionTip = false

const bar = new Vue(ProgressBar).$mount() // #A
Vue.prototype.$bar = bar // #B
document.body.appendChild(bar.$el) // #C

new Vue({ // #D
el: '#app',
render: h => h(App)
})



But i keep getting the error [Vue warn]: Error in config.errorHandler: "TypeError: Cannot read property 'start' of undefined"

Please what am i missing ?
Adebayo Oluwadamilola (3) [Avatar] Offline
#2
I saw your response on the previously asked question, but that does not help me much
Adebayo Oluwadamilola (3) [Avatar] Offline
#3
Edd Yerburgh (42) [Avatar] Offline
#4
The issue is that you are not passing a mock in the first unit test. If you continue reading the book there is a section that shows you how to rewrite the test passing in $bar as an instance property using the mocks option.