Skip to content

Unable to use this in simple tests #15

@robross0606

Description

@robross0606

Trying to use this in a rather simple Jest test, and it produces very strange results:

const agent = require('supertest-koa-agent')
const Koa = require('koa')

it.only('Creates a Koa response', async done => {
  const responseText = 'TESTING!'
  const app = new Koa()
  app.use(async (ctx, next) => {
    await next()
    ctx.body = responseText
  })

  const testAgent = agent(app)
  const response = await testAgent.get('/').end(done)
  expect(response.text).toBe(responseText)
})

The resulting response has reponse.text containing "TESTING!TESTING!" and nothing in the response.body.

Similarly, attempting to use a structure for the body, results in an error:

const agent = require('supertest-koa-agent')
const Koa = require('koa')

it.only('Creates a Koa response', async done => {
  const responseText = 'TESTING!'
  const app = new Koa()
  app.use(async (ctx, next) => {
    await next()
    ctx.body = { responseText }
  })

  const testAgent = agent(app)
  const response = await testAgent.get('/').end(done)
  expect(response.text).toBe(responseText)
})

Yields:

    SyntaxError: Unexpected token { in JSON at position 27
        at JSON.parse (<anonymous>)

      at IncomingMessage.<anonymous> (node_modules/supertest-koa-agent/node_modules/superagent/lib/node/parsers/json.js:11:35)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions