This repository was archived by the owner on Oct 5, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 58
This repository was archived by the owner on Oct 5, 2022. It is now read-only.
Double export brakes jest test #60
Copy link
Copy link
Closed
Labels
Description
Description
Hello! i faced a strange issue while testing my application. i've got wrapping component named as 'ContextMenu' which contains imported 'vue-context' and renders menu items in a loop. Everything works fine, but jest test fails because vue-context module is exported twice. Here is the error:
\node_modules\vue-context\src\js\index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default } from './vue-context';
SyntaxError: Unexpected token 'export'
Full description of the error - http://prntscr.com/tcirgu
As i understand the problem is in the module here
export { default } from './vue-context';
export { default as VueContext } from './vue-context';
If needed, the full code of the component:
<template>
<vue-context
ref="menu"
@close="clear"
class="c-context-menu position-fixed bg-white shadow-light rounded-sm border-0"
>
<div
v-for="(m, index) in filteredData"
:key="index"
@click.prevent="m.handler(item.id)"
class="c-context-menu__item position-relative pl-4 pr-4 pt-3 pb-3 cursor-pointer"
>
<i :class="`c-context-menu__item-icon mr-2 ${m.icon}`" />
{{ m.title }}
</div>
</vue-context>
</template>
<script>
import VueContext from 'vue-context'
export default {
name: 'ContextMenu',
components: { VueContext },
data() {
return {
item: {},
data: []
}
},
mounted() {
this.$bus.$on('contextMenuOpen', this.open)
},
computed: {
filteredData() {
if (Object.prototype.hasOwnProperty.call(this.item, 'deleted_at')) {
if (this.item.deleted_at === null) {
return this.data.filter(m => m.title !== 'Восстановить')
}
return this.data.filter(m => m.title !== 'В архив')
}
return this.data
}
},
methods: {
open(event, item, data) {
console.log(event, item, data)
this.data = data
this.item = { ...item }
this.$refs.menu.open(event)
},
close() {
this.$refs.menu.close()
this.clear()
},
clear() {
this.item = {}
this.data = []
}
},
beforeDestroy() {
this.$bus.$off('contextMenuOpen')
}
}
</script>
And the minimal jest test code to reproduce the issue:
import {
mount,
createLocalVue,
enableAutoDestroy
} from '@vue/test-utils'
import ContextMenu from '@/components/ContextMenu' // wrapping component for 'vue-context'
let wrapper
let testHandler
let spyOnMenuOpen
let spyOnMenuClear
const localVue = createLocalVue()
describe('ContextMenu', () => {
enableAutoDestroy(afterEach)
beforeEach(() => {
wrapper = mount(ContextMenu, {
mocks: {
$bus: {
$on: jest.fn(),
$off: jest.fn(),
$emit: jest.fn()
}
},
localVue
})
testHandler = jest.fn()
spyOnMenuOpen = jest.spyOn(wrapper.vm.$refs.menu, 'open')
spyOnMenuClear = jest.spyOn(wrapper.vm, 'clear')
wrapper.vm.open({}, { id: 1, name: 'test' }, [
{
icon: 'el-icon-edit',
title: 'test1',
handler: testHandler
},
{
icon: 'el-icon-s-cooperation',
title: 'test2',
handler: testHandler
}
])
})
it('Render menu items', (done) => {
wrapper.vm.$nextTick(() => {
expect(wrapper.find('.c-context-menu__item').length).toBe(2)
})
})
})
Steps to Reproduce
- Create simpe jest test
- Run test
- Before test start, you can see the error
Version
5.2.0
nicolidin