0

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

PositiveGuy
  • 17,621
  • 26
  • 79
  • 138
  • 1
    Could you share the exact error message? What isn't a function? Could you try using console.log or debugging to grab what the object is being returned as? – Chris Anderson Jul 08 '15 at 07:44
  • I'm not even sure I know what object it's talking about. – PositiveGuy Jul 08 '15 at 08:00
  • looks like the response is undefined and is probably being checked in the expect(200) line when it tries to read the response's status code...which undefined would make sense...I want my test to simply fail, not throw a huge error like this. Of course that endpoint isn't there yet, I have to test drive it! but my BDD test should simply fail if the response is undefined, not throw a huge fit. – PositiveGuy Jul 08 '15 at 08:02
  • You can take it a step further and set the $DEBUG environment variable = `*`. That would show you more of what was going on. I can't say just looking at the call stack what would cause it to throw an error – Chris Anderson Jul 08 '15 at 08:05
  • I think I see your error. Can you try just setting URL to `/search/cars` – Chris Anderson Jul 08 '15 at 08:09
  • 1
    are you freakin kidding me! wtf! that was it. what the hell I've spent hours on this – PositiveGuy Jul 08 '15 at 08:16
  • Ain't that the way of it. When in doubt, reread the docs. That's what made me realize what you were doing wrong. :P – Chris Anderson Jul 08 '15 at 08:17
  • I have read so many docs, it's just not something I'd infer would cause such havoc – PositiveGuy Jul 08 '15 at 08:19
  • I mean IMO supertest should be smarter than this, I'm debugging supertest basically – PositiveGuy Jul 08 '15 at 08:20
  • Yeah, it'd be a cool PR to [Supertest](https://github.com/visionmedia/supertest) if you added a warning if you try to pass `^http://` to `#url` – Chris Anderson Jul 08 '15 at 08:21
  • yea after spending 5 hours on this error, I think I will. Thanks – PositiveGuy Jul 08 '15 at 08:22
  • [This](https://github.com/visionmedia/supertest/blob/master/index.js#L25) is the line you'd need to add to, btw. – Chris Anderson Jul 08 '15 at 08:25

1 Answers1

2

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!
Chris Anderson
  • 8,305
  • 2
  • 29
  • 37
  • and are you always handling the error in your end() in all your tests or are you just doing (200, done) for example? – PositiveGuy Jul 08 '15 at 08:22
  • 200, done works just fine. No need to keep things complicated. This line: `request = request('http://localhost:5555');` is what made it click for me. I remembered your localhost declaration and felt like it didn't fit. – Chris Anderson Jul 08 '15 at 08:26