简体   繁体   English

使用 SuperTest 和 Jest 测试 TypeScript Apollo 服务器:'request.post' 不是 function

[英]Using SuperTest and Jest to test TypeScript Apollo Server: 'request.post' is not a function

I'm trying to use SuperTest to test an Apollo Server following the first answer to this Stack Overflow question , among other examples I've found.在我发现的其他示例中,我正在尝试使用 SuperTest 来测试 Apollo Server,这是对这个 Stack Overflow question的第一个答案。

My code in its entirety is我的全部代码是

//     /__test__/index.test.ts

import * as request from 'supertest';

    let postData = {
        query: `query allArticles{
                    allArticles{
                        id
                    }
                }`,
        operationName: 'allArticles'
    };

    test('basic', async () => {
        try {
            const response = request
                .post('/graphql')
                .send(postData)
                .expect(200); // status code that you expect to be returned
            console.log('response', response);
        } catch (error) {
            console.log(`error ${error.toString()}`);
        }
    });

However when I run this with Jest但是,当我用 Jest 运行它时

"test": "jest --detectOpenHandles --colors"

I get我明白了

 PASS  __test__/index.test.ts
  ● Console

    console.log
    error TypeError: request.post is not a function

      at __test__/index.test.ts:20:11

For what it's worth, I don't think it's "passing" the test, as it doesn't matter what I put in the expect .对于它的价值,我不认为它“通过”了测试,因为我在expect中放了什么并不重要。

If I change my code to follow the Stack Overflow exactly (passing the GraphQL endpoint directly to request如果我更改我的代码以完全遵循堆栈溢出(直接通过 GraphQL 端点请求

  test('basic', async () => {
            try {
                const response = request('/graphql')
                    .post('/graphql')
                    .send(postData)
                    .expect(200); // status code that you expect to be returned
                console.log('response', response);
            } catch (error) {
                console.log(`error ${error.toString()}`);
            }
        });

I get我明白了

PASS  __test__/index.test.ts
  ● Console

    console.log
      error TypeError: request is not a function

      at __test__/index.test.ts:20:11

I'm using ts-jest , and running under Node 12.14我正在使用ts-jest ,并在 Node 12.14 下运行

My tsconfig.json is我的tsconfig.json

{
  "compilerOptions": {
    "target": "ES6",
    "lib": [
      "esnext",
      "dom"
    ],
    "skipLibCheck": true,
    "outDir": "dist",
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "sourceMap": true,
    "alwaysStrict": true
  },
  "exclude": [
    "node_modules",
    "**/*.test.ts",
    "**/*.mock.ts"
  ]
}

and my jest.config is我的jest.config

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node'
};

Any clues appreciated!任何线索表示赞赏!

supertest has no export, which is why need to change your import to supertest没有导出,这就是为什么需要将导入更改为

import {default as request} from 'supertest';

request is now the exported factory function which you can invoke: request现在是导出的工厂 function 您可以调用它:

const response = request('/graphql')
                    .post('/graphql')
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM