How to simulate the ctx.session information in koa-session when testing with jest?

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)
})
Dec.31,2021

test('user login if session.userName is exist', async () => {
    const res = await request(app.callback())
      .get('/api/login/hasLogin')
      .set('cookie', 'koa:todo=eyJ1c2VyTmFtZSI6Imhpd29ybGQiLCJfZXhwaXJlIjoxNTQzNzM5NjU1ODkwLCJfbWF4QWdlIjo4NjQwMDAwMH0=; koa:todo.sig=Op3Ik5vbgl-IsFuGC8i_u6MRgyk')
    expect(res.body.success).toBe(true)
  })

create a test cookie, to solve.

Menu