vue test utils TypeError:this。$ moment(...)。format不是函数

问题描述 投票:0回答:1

我正在使用moment.js并且在收到此错误时无法模拟格式函数:

TypeError:this。$ moment(...)。format不是函数

请问任何人指出我正确的方向吗?

import { mount, createLocalVue, } from '@vue/test-utils'
import { expect } from 'chai'
import sinon from 'sinon'
import Vuex from 'vuex'
import authorisation from '../../src/components/settings/Authorisation.vue'

const localVue = createLocalVue()
localVue.use(Vuex)
const renewAuthStub = sinon.stub()
localVue.filter('translate', () => {})

describe ('settings authorisation', () => {
  let wrapper
  let store
  let state
  let getters
  let actions

    beforeEach(() => {
      state = {
        authorisation: {
          authorised: '2018-03-18',
          expiry: '03-04-2019',
          canBeExtended: true
        }
      }
      getters = {
        fetchAuth: () => '',
        isLoading: () => false,
        authorisation: () => state.authorisation,
        highlightDates: () => {},
        disabledDates: () => {},
        requestLoading: () => false
      }
      actions = {
        fetchAuth: () => {}
      }
      store = new Vuex.Store({
        state,
        getters,
        actions
      })
      wrapper = mount(authorisation, {
        localVue,
        store,
        mocks: {
          $moment: () => sinon.stub().resolves()
        }
      })
    })

    it('lets user know to reactivate when not authorised', () => {
momentjs sinon vue-test-utils
1个回答
0
投票

所以我意识到我正在使用udateLocale('en',英语),因此我需要在我的测试中使用它,因此需要添加以下内容:

import { mount, createLocalVue, } from '@vue/test-utils'
import { expect } from 'chai'
import sinon from 'sinon'
import Moment from 'moment'
import English from '../../src/i18n/moment/en'
import Vuex from 'vuex'
import Vue from 'vue'
import authorisation from '../../src/components/settings/Authorisation.vue'

const localVue = createLocalVue()
localVue.use(Vuex)
const renewAuthStub = sinon.stub()
localVue.filter('translate', () => {})
Vue.prototype.$moment = Moment
Moment.updateLocale('en', English)

describe ('settings authorisation', () => {
© www.soinside.com 2019 - 2024. All rights reserved.