0

I was using jest for my web-app unit testing,but got an tough case. I import a component in a test.spec.js file , this component imports a http request function from another file , this http request function use the var I defined in the vuex state,and this state data use the window object, which cause the test file error.Here's some codes.

test.spec.js: imports a component called Info.vue

import Info from '@/pages/user/Info.vue'

Info.vue: imports a http request function requestData from get-api-data.js

import { requestData } from '@/service/get-api-data'

get-api-data.js: imports the store(vuex) data import store from '@/store'

store have those vars which involve the window object:

env: {
    apiUrl: window._env.apiUrl,
    name: window._env.user,
    password: window._env.apiKey
},

and the test suite fails to run. here's the error:
enter image description here

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Jerry
  • 170
  • 8
  • where do you set `window._env` variable? – Max Sinev Aug 08 '18 at 09:30
  • I put it in a single file using (function(){})() and include it in index.html. – Jerry Aug 08 '18 at 09:41
  • so you don't have `_env` variable in window, try to import this file to your test file – Max Sinev Aug 08 '18 at 09:44
  • yes, I manage to make it , but got another issue , ```Cannot read property 'toLowerCase' of undefined``` – Jerry Aug 08 '18 at 09:50
  • where is this error? please provide more info, i don't see any code where `toLowerCase` used – Max Sinev Aug 08 '18 at 09:59
  • the error is from Jest. I use the mint-ui as the ui lib for my project and I import `MessageBox` in a component and when jest parse this component and read the MessageBox import , it crashed and showed : ` TypeError: Cannot read property 'toLowerCase' of undefined` and point to `import { MessageBox, Indicator } from 'mint-ui'` – Jerry Aug 09 '18 at 04:09

0 Answers0