Search code examples
node.jskoasupertest

Object is not a Function - SuperTest


I'm not sure why it's throwing this error and what exactly it's saying is not a function.

    'use strict';

  var koa = require("koa");
    var app = koa();
    var chai = require('chai');
    var expect = chai.expect;

    var request = require('supertest');

    describe('Feature: Car REST Endpoint', function () {

        context('Scenario: GET a Car', function () {

            var url = 'http://localhost/search/cars';

            describe('Given: the resource is accessed at the resource url' + url, function () {

                describe('Then: we receive a successful response', function(){

                    it('status code should be 200', function (done){
                        request(app)
                            .get(url)
                            .expect(200,done);
                    });
                });

it says it's the line .expect(200,done) but I might be wrong.

I also tried this with no luck:

 request(app)
                    .get(url)
                    .expect(200)
                    .end(function(err, res){
                        if (err) return done(err);
                        done()
                    });

I also tried var request = require('supertest').agent(koa);

enter image description here


Solution

  • Your problem is that you're passing http://localhost to #get(url). Change url to just /search/cars. I've got a full repro (using express)

    var request = require('supertest'),
    express = require('express');
    
    var app = express();
    
    app.get('/user', function(req, res) {
      res.send(200, {
        name: 'tobi'
      });
    });
    
    request(app)
      .get('http://localhost/user')
      .expect(200)
      .end(function(err) {
        if (err) throw err;
        console.log('Success!');
      });
    

    Output:

        if (res.status !== status) {
               ^
    TypeError: Cannot read property 'status' of undefined
        at Test.assert (C:\workspace\choose-your-own\node_modules\supertest\lib\test.js:202:12)
        at Server.assert (C:\workspace\choose-your-own\node_modules\supertest\lib\test.js:131:12)
        at Server.g (events.js:199:16)
        at Server.emit (events.js:104:17)
        at net.js:1392:10
        at process._tickCallback (node.js:355:11)
    

    When I use /user, I just get:

    Success!