0

I'm new to BDD and for some reason my code always seem to be passing although I haven't yet written any code. Can somebody please explain why this is happening?

Project setup:

I have a project folder with package.json and a test section with the following declared: ".node_modules/.bin/jasmine-node" and a folder called spec with the following code file:

    var request = require("request");

    describe("Web Server Test", function() {
        it("GET /", function(done) {
            request.get("http://localhost/", function(error, request, body) {
                expect(body).toContain("Hello, World!");
            });
            done();
        });
    });

This is the output I get:

C:\Users\\OneDrive\Documents\Websites\Projects\Node\project>npm test spec/app_spec.js

Project@0.0.0 test C:\Users\\OneDrive\Documents\Websites\Projects\Node\project jasmine-node "spec/app_spec.js"

.

Finished in 0.031 seconds 1 test, 0 assertions, 0 failures, 0 skipped

Soviut
  • 88,194
  • 49
  • 192
  • 260

1 Answers1

1

the done callback must be called inside request callback...

it("GET /", function(done) {
  request.get("http://localhost/", function(error, request, body) {
    expect(body).toContain("Hello, World!");

    // THIS IS ASYNC
    done();
  });
});
Soviut
  • 88,194
  • 49
  • 192
  • 260
Hitmands
  • 13,491
  • 4
  • 34
  • 69
  • I'm not getting any output now – Aiden Pearce Sep 03 '16 at 06:58
  • I changed your snippet to a standard code block. Snippets are for running examples only. – Soviut Sep 03 '16 at 06:58
  • @AidenPearce `console.log(error)` to see what the request is doing. You can also set these tests to timeout on async calls that take too long. – Soviut Sep 03 '16 at 07:00
  • @Soviut thanks. If you don't get any response it means that your request takes more time than jasmine allows (http://stackoverflow.com/questions/9867601/how-do-i-change-the-timeout-on-a-jasmine-node-async-spec#34291826)... or, at least, your test is failing... try to catch errors – Hitmands Sep 03 '16 at 07:01
  • @AidenPearce http://jasmine.github.io/edge/introduction.html#section-Asynchronous_Support you can add a number in milliseconds to set a timeout in your `it()` declaration. `it('whatever', function(done) { done(); }, 1000);` – Soviut Sep 03 '16 at 07:02
  • I added the line "console.log(error);" directly after the expect line and I still get no output. – Aiden Pearce Sep 03 '16 at 07:03
  • @Soviut I've added 5000 as the timeout and still no output. – Aiden Pearce Sep 03 '16 at 07:05
  • Have a look at the related question in my comment, it explains how to properly set long time out in jasmine using a before each – Hitmands Sep 03 '16 at 07:16