javascriptnode.jsjasmine-node

Jasmine-node expect not executed


I started testing today with Jasmine, which is new to me. Since I am working with a GraphQL-server, developed with NodeJS, I am using jasmine-node.

The following is my first spec I write myself, and I thought it was working:

const request = require('request');
const config = require('../sources/config');

const base_url = `http://localhost:${config.graphqlPort}/${config.graphqlRoute}`;

describe("GraphQLServer.", () => {
it("Should expect something.", () => {
    expect(1).not.toEqual(2);
    expect(1).toEqual(1);
    expect(true).toBe(true);
    expect(false).not.toBe(true);
});

describe('When request is made.', () => {
    it('Returns statuscode 200.', () => {
        request.get(base_url, (error, response, body) => {
            console.log('Error: ', error);
            console.log(base_url);
            console.log('StatusCode: ', response && response.statusCode);
            console.log('Body:', body);
            expect(true).toBe(true);
            expect(true).toBe(false);
            expect(response.statusCode).toBe(200);
            expect(response.statusCode).toBe(400);
            done();
            });
        });
    });
});

This gives the following result:

    ..

Finished in 0.018 seconds
2 tests, 4 assertions, 0 failures, 0 skipped


Error:  null
http://localhost:8000/graphql
StatusCode:  400
Body: {"errors":[{"message":"Must provide query string."}]}

A lot of this is exactly what I expect. I do a request without a query-string, so the errors in body are expected.

The problem is: the 4 useless (obvious) expects inside it("Should expect something") are working just fine. Those are the 4 assertions in the result. The other 4 expect's inside it("Returns statuscode 200.") are simply not in the result (they don't fail and they don't pass), but the console.log's are.

Every example I find online seems to do exactly what I do right here, yet I don't manage to get this to work.


Solution

  • I forget the exact syntax in Jasmine to suggest a fix, but I believe I know the answer: This is a classic case of testing with promises. The result is coming back after the test is done, so your expect calls are not happening in a context where they can affect the test anymore.

    One difference I noticed between your code and the jasmine-node docs is that the function being passed to it should take done as a parameter, but you have () => instead of (done) =>. That might cause the behaviour you're seeing.