Javascript 找不到模块'@玩笑/全球';在vue测试中使用jest
我正试图从项目中的vue文档编写一个非常简单的测试。 我可以在我的项目中使用jest运行测试,但当我尝试使用jest模拟axios请求时,我出现以下错误:Javascript 找不到模块'@玩笑/全球';在vue测试中使用jest,javascript,vue.js,testing,jestjs,Javascript,Vue.js,Testing,Jestjs,我正试图从项目中的vue文档编写一个非常简单的测试。 我可以在我的项目中使用jest运行测试,但当我尝试使用jest模拟axios请求时,我出现以下错误: FAIL tests/unit/test.spec.js ● 测试套件无法运行 Cannot find module '@jest/globals' from 'test.spec.js' 14 | }) at Resolver.resolveModule (node_modules/jest-resolve/build
FAIL tests/unit/test.spec.js
● 测试套件无法运行
Cannot find module '@jest/globals' from 'test.spec.js'
14 | })
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17)
at _getJestObj (tests/unit/test.spec.js:16:7)
at Object.<anonymous> (tests/unit/test.spec.js:3:1)
任何帮助都将不胜感激:)
谢谢大家 假设您也在使用
babel jest
,请确保将jest
和babel jest
的两个版本都设置为26
。我也有同样的问题,这是因为软件包版本不同步。在我的例子中,这个错误发生在babel中-jest@26.0.1. 降级到巴别塔之后-jest@21.2.0,问题消失。谢谢您的帮助。事实上,我从我的jest.config.js文件中删除了“moduleDirectories”:[“node_modules”,“src”],“modulePath”:[“/node_modules”],所有这些都像一个符咒一样工作:)
<template>
<button @click="fetchResults">{{ value }}</button>
</template>
<script>
import axios from 'axios'
export default {
data() {
return {
value: null
}
},
methods: {
async fetchResults() {
const response = await axios.get('mock/service')
this.value = response.data
}
}
}
</script>
import { shallowMount } from '@vue/test-utils'
import Foo from './Foo'
jest.mock('axios', () => ({
get: Promise.resolve('value')
}))
it('fetches async when a button is clicked', done => {
const wrapper = shallowMount(Foo)
wrapper.find('button').trigger('click')
wrapper.vm.$nextTick(() => {
expect(wrapper.text()).toBe('value')
done()
})
}