admin管理员组文章数量:1332389
I try to mock axios
module inside my test file like this
// mycomponent.test.js
import axios from 'axios';
jest.mock('axios', () => ({
get: jest.fn(() => Promise.resolve({ data: 'data' })),
default: jest.fn(() => Promise.resolve({ data: 'data' })),
}));
But after i add jest.mock('axios')
into my test file, i got an error like this.
TypeError: (0 , _axios.default) is not a function
55 | this.props.updateGlobalLoading(true);
56 |
> 57 | axios({
| ^
58 | method: 'get',
59 | url: '/v1/api/portal-xml-list',
60 | }).then((res) => {
So how should i fix this, any thing that i missed to set for axios
mocking?
Thanks!
I try to mock axios
module inside my test file like this
// mycomponent.test.js
import axios from 'axios';
jest.mock('axios', () => ({
get: jest.fn(() => Promise.resolve({ data: 'data' })),
default: jest.fn(() => Promise.resolve({ data: 'data' })),
}));
But after i add jest.mock('axios')
into my test file, i got an error like this.
TypeError: (0 , _axios.default) is not a function
55 | this.props.updateGlobalLoading(true);
56 |
> 57 | axios({
| ^
58 | method: 'get',
59 | url: '/v1/api/portal-xml-list',
60 | }).then((res) => {
So how should i fix this, any thing that i missed to set for axios
mocking?
Thanks!
Share Improve this question edited May 15, 2019 at 17:36 Max 3921 gold badge2 silver badges11 bronze badges asked May 15, 2019 at 7:56 Varis DarasirikulVaris Darasirikul 4,17710 gold badges43 silver badges79 bronze badges3 Answers
Reset to default 31If you want to mock the default and named exports of a module (axios
in this case), the property __esModule
must be enabled in the return value:
jest.mock('axios', () => ({
__esModule: true,
get: jest.fn(() => Promise.resolve({ data: 'data' })),
default: jest.fn(() => Promise.resolve({ data: 'data' })),
}));
Alternatively, as it seems that you are only using the default export of axios
, you could mock the default export as:
jest.mock('axios', () => jest.fn(() => Promise.resolve({ data: 'data' })));
As a follow up to @mgarcia's answer, I kept getting the following error when using his/her version:
ReferenceError: The module factory of `jest.mock()` is not allowed to reference
any out-of-scope variables.
Invalid variable access: jest
The error was caused by the jest.fn()
method inside jest.mock()
. So I just removed it like so and was able to mock axios
successfully:
jest.mock('axios', () => ({
get: () => Promise.resolve({ data: 'data' }),
}));
I solved this problem with as unknown as jest.Mock
// package.json
"axios": "^1.3.4",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
jest.mock('axios', () => ({
__esModule: true,
default: jest.fn(),
}));
beforeEach(() => {
(axios as unknown as jest.Mock).mockReset();
});
本文标签:
版权声明:本文标题:javascript - TypeError: (0 , _axios.default) is not a function when use jest.mock('axios') inside a *.test.js fi 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738147174a2065978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论