while learning to test api with jest, I encountered this function:
const checkHasLogin = async (ctx, next) => {
if (ctx.session.userName) {
ctx.body = {
success: true,
msg: ctx.session.userName
}
} else {
ctx.body = {
success: false,
msg: ""
}
}
}
where ctx.session
is:
// app.js
const session = require("koa-session")
const CONFIG = {
key: "koa:todo",
maxAge: 86400000,
overwrite: true,
httpOnly: true,
signed: true,
rolling: false,
renew: false
}
app.use(session(CONFIG, app))
I"d like to ask if there is any way to simulate this ctx.session
.
//
test("user login if session.userName is exist", async () => {
const res = await request(app.callback())
.get("/api/login/hasLogin")
expect(res.body.success).toBe(true)
})