我有这个行动的反应
export function fetchPosts() {
const request = axios.get(`${WORDPRESS_URL}`);
return {
type: FETCH_POSTS,
payload: request
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何测试axios?Jest在那里有这个用例的异步代码,他们使用模拟函数,但我不知道我是否可以用axios做到这一点?参考:https://facebook.github.io/jest/docs/tutorial-async.html
到目前为止,我已经做了这个测试,它正在返回正确的类型
it('should dispatch actions with the correct type', () => {
store.dispatch(fetchPosts());
let action = store.getActions();
expect(action[0].type).toBe(FETCH_POSTS);
});
Run Code Online (Sandbox Code Playgroud)
我不知道如何传递模拟数据并测试它返回但是,有没有人有任何想法?
先感谢您
我开始使用 Jest 和 Supertest(针对端点)测试我的应用程序。测试工作顺利,但Jest 在运行测试后检测到 2 个打开的句柄,这会阻止 Jest 干净退出。
\n这个打开的句柄是由我的测试文件中调用的外部异步函数生成的。我正在使用外部函数从 Auth0 API 请求 JWT 令牌;但对 Auth0 的请求还在其响应中提供了传递端点中间件的关键信息(有关此的更多信息如下)。这里要记住两件事:
\nuser具有关键信息的对象。Auth0 将此对象设置在主体响应之外的同一级别,但不在其中。该信息是传递端点中间件的关键。getToken测试文件中的该函数(称为 )生成的。import app from "../app";\nimport mongoose from "mongoose";\nimport supertest from "supertest";\nimport { getToken } from "../helpers";\nimport dotenv from "dotenv";\nimport * as config from "../config";\n\ndotenv.config();\n\nconst api = supertest(app);\n\nlet authToken: any;\nlet db: any;\n\nbeforeAll(async() => {\n try {\n mongoose.connect(config.MONGODB_URI, {\n useNewUrlParser: true,\n useUnifiedTopology: true,\n useCreateIndex: …Run Code Online (Sandbox Code Playgroud)