Search code examples
typescriptsupertest

TypeScript doesn't recognize an object in Request response from supertest?


I'm writing a test for a very simple graphql API in TypeScript using Supertest. I'm getting this error: Property 'data' does not exist on type 'Response', and why should it? I haven't told it what to expect there. But how can I? data is immediately followed by should.

enter image description here

Here is my short test file.

import chai from "chai";
import { expect } from "chai";
import * as request from "supertest";
import app from "../src";

describe("graphQl test", () => {

    it("should respond with hello world", async (done) => {

        const returned = await request(app)
            .post("/graphql")
            .send({ query: "{ hello }"})
            .expect(200)
            .end((err, res) => {
                if (err) { return done(err); }
                res.data.should.have.property("hello");
                done();
            });

        expect(returned.data.hello).equal("Hello world!");
    });
});

Is there a simple solution to this? If not, I'm imagining I have to create some kind of interface, but how would I create an interface and communicate that interface to TypeScript in this scenario? I'm searching but I have yet to see an example of this.


Solution

  • I had all of my typings installed. In this case, I think the problem became solved in the process of getting Mocha to work with ts-node. ts-node didn't work with my global typescript so I had to install that locally. Then I needed to add a separate tsconfig.json file for my tests and VSCode stopped giving me those errors and compilation worked fine.