supertest-graphql icon indicating copy to clipboard operation
supertest-graphql copied to clipboard

TypeError: request is not a function

Open ecker00 opened this issue 3 years ago • 0 comments

I'm running a pure TypeScript ESM setup on Node 16.14.2, running test with node --experimental-vm-modules node_modules/.bin/jest. So close this issue if not relevant and maybe just my setup, but thought I should share my findings.

If I import supertest-graphql as described, it is an object and not a callable function:

import request from 'supertest-graphql';
import gql from 'graphql-tag';

console.log(request); // See console output bellow

it('should respond hello world', async () => {
  const { data }: any = await request(app)
    //                         ↑  TypeError: request is not a function
    .query(gql` query { hello }`)
    .expectNoErrors();

  expect(data.hello).toEqual('Hello world');
});

Console log output:

{
  supertestWs: [Function (anonymous)] { end: [Function: end] },
  SuperTestGraphQL: [class SuperTestGraphQL],
  LEGACY_WEBSOCKET_PROTOCOL: [Getter],
  SuperTestExecutionNextResult: [Getter],
  SuperTestExecutionStreamingResult: [Getter],
  SuperTestExecutionStreamingResultPool: [Getter],
  default: [Function: supertest]
}

Hacky soluton

But if I "unwrap" the import to my own variable everything works fine.

import requestObj from 'supertest-graphql';

// @ts-ignore
const request: typeof requestObj = requestObj.default;

I've not seen this behaviour with other node modules, so not sure if it's related to this module or a different problem.

ecker00 avatar Apr 02 '22 21:04 ecker00