Skip to content
This repository was archived by the owner on Oct 5, 2022. It is now read-only.
This repository was archived by the owner on Oct 5, 2022. It is now read-only.

Double export brakes jest test #60

@Ivan3008

Description

@Ivan3008

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

  1. Create simpe jest test
  2. Run test
  3. Before test start, you can see the error

Version

5.2.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions