Code Monkey home page Code Monkey logo

node-supertest-koa-agent's Introduction

supertest-koa-agent

Converts a Koa app into a supertest compatible agent instance.

Build Status Code Climate js-standard-style

npm install supertest-koa-agent --save-dev
npm stats

npm NPM downloads Dependency Status

API Example

var agent = require('supertest-koa-agent');
var Koa = require('koa')

var app = new Koa()

agent(app)

API

agent(app)

arguments
  • app: (Application) Koa application instance.
returns
  • (Object) Supertest agent instance.

Contributing

SEE: contributing.md

Licenses

GitHub license

node-supertest-koa-agent's People

Contributors

eizzo avatar wilmoore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-supertest-koa-agent's Issues

Publish latest version

Hey, great project! I can see, that supertest dependency has been fixed in #1 Could you please publish those changes to npm?

Unable to use this in simple tests

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)

failed with "app.address is not a function"

Hey,

I'm using Koa + AVA + Supertest for my server, and I'm getting this:

$ ava tests/server/**/*.test.js

   1 passed
   1 failed


   1. server can post to create new config entry and fetch it
   failed with "app.address is not a function"
      Test.serverAddress (D:/Users/Vincent/Documents/Developement/cassette/node_modules/supertest/lib/test.js:55:18)
    new Test (D:/Users/Vincent/Documents/Developement/cassette/node_modules/supertest/lib/test.js:36:12)
    Object.obj.(anonymous function) [as post] (D:/Users/Vincent/Documents/Developement/cassette/node_modules/supertest/index.js:25:14)
    Object.out.(anonymous function) [as post] (D:/Users/Vincent/Documents/Developement/cassette/node_modules/supertest-as-promised/index.js:49:36)
    _callee$ (D:/Users/Vincent/Documents/Developement/cassette/tests/server/unit/config.test.js:47:28)
    tryCatch (D:/Users/Vincent/Documents/Developement/cassette/node_modules/regenerator-runtime/runtime.js:62:40)
    GeneratorFunctionPrototype.invoke [as _invoke] (D:/Users/Vincent/Documents/Developement/cassette/node_modules/regenerator-runtime/runtime.js:336:22)
    GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (D:/Users/Vincent/Documents/Developement/cassette/node_modules/regenerator-runtime/runtime.js:95:21)
    step (D:/Users/Vincent/Documents/Developement/cassette/tests/server/unit/config.test.js:29:191)
    D:/Users/Vincent/Documents/Developement/cassette/tests/server/unit/config.test.js:29:437
    Test.<anonymous> (D:/Users/Vincent/Documents/Developement/cassette/tests/server/unit/config.test.js:29:99)
    Test.fn (D:/Users/Vincent/Documents/Developement/cassette/tests/server/unit/config.test.js:89:17)
    _combinedTickCallback (internal/process/next_tick.js:67:7)
    process._tickCallback (internal/process/next_tick.js:98:9)

Here are my files:

src/server/server.js

import chalk from 'chalk';
import config from '../../config';

import koa from 'koa';
import koaRouter from 'koa-router';

let app = koa();
let router = koaRouter();


app.use(function *(next) {
  let start = new Date();

  yield next;
  var ms = new Date() - start;
  console.log(`${chalk.green(this.method)} ${chalk.dim(this.url)} - ${chalk.blue(ms)}ms`);
});

app.use(router.routes());
app.use(router.allowedMethods());

// app.listen(3030);


export default app;

tests/server/config.js

import agent from 'supertest-koa-agent';
import config from '../../../config';
import nock from 'nock';
import supertest from 'supertest-as-promised'
import test from 'ava';
import app from '../../../src/server/server.js';

const request = supertest(agent(app));

test('rootDir is a temp dir different from baseDir', t => {
  t.not(config.rootDir, config.baseDir);
});


test('server can post to create new config entry and fetch it', async t => {
  t.plan(4);

  const res = await request.post('http://localhost:3030/v1/config/').send({
    key: 'foo',
    value: '42'
  }).expect(200);
  t.is(res.status, 201);
  t.deepEqual(res.body, {
    success: true,
    status: 201,
    data: {
      key: 'foo',
      value: '42'
    }
  });
  const res2 = await request.get('http://localhost:3030/v1/config/foo').expect(200);
  t.is(res2.status, 200);
  t.deepEqual(res2.body, {
    success: true,
    status: 200,
    data: {
      key: 'foo',
      value: '42'
    }
  });
});

Any idea? Thanks in advance :)

deprecated warning

not sure if this is still actively maintained or not but getting some deprecation warnings and thought that an update of supertest would be good

npm WARN deprecated [email protected]: use String.prototype.padStart()
npm WARN deprecated [email protected]: Please note that v5.0.1+ of superagent removes User-Agent header by default, therefore you may need to add it yourself (e.g. GitHub blocks requests without a User-Agent header). This notice will go away with v5.0.2+ once it is released.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.