[英]2 different instance of axios mock having returning an error of axios.default is not a function
In my function named eventController.在我的 function 中命名为 eventController。 An axios call has implemented in two ways The 1st one is on this code
axios 调用以两种方式实现第一种是在此代码上
axios.post(
`${url}/API1`,
{
data:'data for API1'
}
)
Then this is the 2nd那么这是第2
await axios({
method: 'POST',
url: `${url}/API2`,
data: {
data:'data for API2'
}
})
2 different axios calls in one function/controller一个函数/控制器中有 2 个不同的 axios 调用
I'm assigned in implementing a unit test for this using jest in typescript我被分配在 typescript 中使用 jest 对此进行单元测试
So what I did so far is this所以我到目前为止所做的是
jest.mock('axios', () => ({
post: jest
.fn()
.mockResolvedValueOnce({ status: 200 })
}));
it("should work", async () => {
const req = {body: {
data: 'data for API'
}}
const response = await eventController(req);
expect(response.status).toBe(200);
});
I mock the 1st axios call with.post method but when doing it w/ the second one using the default axios module I have received this error我模拟了第一个 axios 调用 with.post 方法,但是当使用默认的 axios 模块进行第二个调用时,我收到了这个错误
TypeError: (0 , axios_1.default) is not a function
I've tried to implement this way also but both don't seem to work This:我也尝试过以这种方式实现,但两者似乎都不起作用:
jest.mock('axios', () => ({
__esModule: true,
default: jest.fn(),
post: jest
.fn()
.mockResolvedValueOnce({ status: 200 })
}));
and this:和这个:
jest.mock('axios', () => ({
__esModule: true,
default: {
post: jest
.fn()
.mockResolvedValueOnce({ status: 200 })
},
}));
Hoping someone can help me on this one.希望有人可以帮助我解决这个问题。 This is the versions of the library I'm currently using
这是我当前使用的库的版本
Just use jest.mock(moduleName, factory, options)
and without passing the factory
argument, let the jest mocks a module with an auto-mocked version.只需使用
jest.mock(moduleName, factory, options)
并且不传递factory
参数,让 jest 模拟具有自动模拟版本的模块。 And, there is no __mocks__
directory.而且,没有
__mocks__
目录。
You need to handle the TS types for axios
function and axios.get()
method after mocking, use type cast to do this.您需要在
axios
function 和axios.get()
方法的 TS 类型,使用类型转换来执行此操作。
Note: I use a simple string as the mock resolved value which does not match the AxiosResponse
interface.注意:我使用一个简单的字符串作为模拟解析值,它与
AxiosResponse
接口不匹配。
Eg例如
main.ts
: main.ts
:
import axios from 'axios';
const url = 'http://localhost:3000';
export async function main() {
const res1 = await axios.post(`${url}/API1`, {
data: 'data for API1',
});
console.log('res1: ', res1);
const res2 = await axios({
method: 'POST',
url: `${url}/API2`,
data: {
data: 'data for API2',
},
});
console.log('res2: ', res2);
}
main.test.ts
: main.test.ts
:
import { main } from './main';
import axios from 'axios';
jest.mock('axios');
const axiosMock = axios as jest.MockedFunction<typeof axios>;
const axiosPostMock = axios.post as jest.MockedFunction<typeof axios.post>;
test('should pass', async () => {
expect(jest.isMockFunction(axios)).toBe(true);
expect(jest.isMockFunction(axios.post)).toBe(true);
axiosPostMock.mockResolvedValueOnce('fake data 1');
axiosMock.mockResolvedValueOnce('fake data 2' as any);
await main();
});
Test result:测试结果:
PASS stackoverflow/76407602/main.test.ts (38.236 s)
✓ should pass (48 ms)
console.log
res1: fake data 1
at stackoverflow/76407602/main.ts:8:11
console.log
res2: fake data 2
at stackoverflow/76407602/main.ts:16:11
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 41.384 s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.