Javascript 编写单元测试时createUser不是函数错误?
在编写函数的单元测试用例时,我遇到了以下错误Javascript 编写单元测试时createUser不是函数错误?,javascript,reactjs,jestjs,enzyme,Javascript,Reactjs,Jestjs,Enzyme,在编写函数的单元测试用例时,我遇到了以下错误 (0 , _service.createUser) is not a function TypeError: (0 , _service.createUser) is not a function at _callee$ (https://pbz00.csb.app/src/service.test.js:43:44) at tryCatch (https://pbz00.csb.app/node_modules/regenerat
(0 , _service.createUser) is not a function
TypeError: (0 , _service.createUser) is not a function
at _callee$ (https://pbz00.csb.app/src/service.test.js:43:44)
at tryCatch (https://pbz00.csb.app/node_modules/regenerator-runtime/runtime.js:62:40)
at Generator.invoke [as _invoke] (https://pbz00.csb.app/node_modules/regenerator-runtime/runtime.js:288:22)
at Generator.prototype.<computed> [as next] (https://pbz00.csb.app/node_modules/regenerator-runtime/runtime.js:114:21)
我想检查它的返回值true
或undefined
这是我的密码
已更新
import { createUser } from "./service";
import axios from "axios";
describe("service test", () => {
it("check abc value", async () => {
jest.spyOn(axios, "post").mockImplementation(() =>
Promise.resolve({
data: {
greeting: "hello there",
status: {
code: "200"
}
}
})
);
console.log("createUser", createUser);
let abc = await createUser();
console.log("after createUser", abc);
expect(abc).toBe(true);
});
});
你把它输入错了。应该是:
从“/service”导入{createUser};
要模拟axios模块,您可以使用:
jest
.spyOn(axios, 'post')
.mockImplementation(() => Promise.resolve({
data: {
greeting: "hello there",
status: {
code: "200"
}
}
}))
您也可以模拟整个模块:但它不会返回真或假..如何模拟我的请求?您知道如何检查
abc
变量值或返回值吗?这可以回答您的问题:谢谢回答。。!!请参阅更新。现在找不到axios更新代码沙盒
import { createUser } from "./service";
describe("service test", () => {
it("check abc value", async () => {
jest.spyOn(axios, "post").mockImplementation(() =>
Promise.resolve({
data: {
greeting: "hello there",
status: {
code: "200"
}
}
})
);
console.log("createUser", createUser);
let abc = await createUser();
console.log("after createUser", abc);
expect(abc).toBe(true);
});
});
import { createUser } from "./service";
import axios from "axios";
describe("service test", () => {
it("check abc value", async () => {
jest.spyOn(axios, "post").mockImplementation(() =>
Promise.resolve({
data: {
greeting: "hello there",
status: {
code: "200"
}
}
})
);
console.log("createUser", createUser);
let abc = await createUser();
console.log("after createUser", abc);
expect(abc).toBe(true);
});
});
jest
.spyOn(axios, 'post')
.mockImplementation(() => Promise.resolve({
data: {
greeting: "hello there",
status: {
code: "200"
}
}
}))