Jest и супер-тестирование ApI, возвращающее TypeError: app.address не является функцией - PullRequest
0 голосов
/ 17 января 2019

В настоящее время я делаю API с машинописью, узлом, экспрессом и тестированием с помощью шуток и супертестов. У меня не было проблем, когда я использовал Javascript, но недавно я изменил свой файл проекта с JS на TS, включая тестовые файлы, и когда я начинаю тестирование, я получаю сообщение об ошибке ниже во всех моих тестовых пакетах в части супер-запроса, и это один из мои тестовые наборы на моем терминале, когда я начинаю тестирование.

  TypeError: app.address is not a function

  37 |     it("should return 400 if email is invalid", async () => {
  38 |       const res = await request(server)
> 39 |         .post("/api/users/auth/register")
     |          ^
  40 |         .send({
  41 |           email: "nomail",
  42 |           password: "validpassword123",

Это мои тестовые файлы auth.test.ts :

import * as request from 'supertest';
import { User } from '../../../../src/models/User';
import * as mongoose from 'mongoose';
import getKeys from '../../../../src/config/keys';

describe("/api/users/auth", () => {
  let server;
  let accessToken = "Bearer accessToken";
  let email;
  let password;

  beforeAll(async () => {
    server = import('../../../../src/index')
    await mongoose.connect(getKeys().mongoURI);
  })

  afterAll(async () => {
    await server.close();
    await mongoose.disconnect();
  })

  it("should return 400 if email is invalid", async () => {
    const res = await request(server)
      .post("/api/users/auth/register")
      .send({
        email: "nomail",
        password: "validpassword123",
        name: "name"
      });

    expect(res.status).toBe(400);
    expect(res.body).toHaveProperty('errArray')
  });
}

и это мой файл src / index.ts, который является точкой входа.

import * as express from 'express';
import * as passport from 'passport';
import * as bodyParser from 'body-parser';
import * as path from 'path';
import * as session from 'express-session';
import getKeys from './config/keys';

const port = 3001 || process.env.PORT;
const server = app.listen(port, () =>
  console.log(`Server running on port ${port}`)
);


export default server;

Я пытался изменить экспорт и импорт синтаксиса сервера на весь синтаксис commonjs, установить и установить все относящиеся к этому зависимости, включая @ types / supertest, @ types / jest, ts-jest, вот мои настройки jest.config.js

module.exports = {
  verbose: true,
  testURL: 'http://localhost',
  testEnvironment: "node",
  roots: [
    "<rootDir>"
  ],
  transform: {
    "^.+\\.tsx?$": "ts-jest"
  },
  testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
  globals: {
    "ts-jest": {
      "tsConfigFile": "tsconfig.json"
    }
  },
  moduleFileExtensions: [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],

}

tsconfig.json

 {
  "compilerOptions": {
    "outDir": "./dist",
    "moduleResolution": "node",
    "sourceMap": true,
    "module": "commonjs",
    "allowJs": true,
    "target": "es5",
    "noUnusedParameters": false,
    "allowUnreachableCode": true,
    "allowUnusedLabels": true,
    "types": [
      "jest",
      "node",
      "express",
      "mongoose",
      "body-parser",
      "supertest"
    ],
    "lib": [
      "es2015"
    ]
  },
  "include": [
    "./src/**/*",
    "index.ts"
  ],
  "exclude": [
    "./node_modules",
    "**/*.spec.ts",
    "**/*.test.ts"
  ]
}

Сценарий и зависимость файла файла package.json

    "scripts": {
    "test": "jest --watchAll --runInBand",
    "coverage": "jest --coverage",
    "start": "ts-node src/index.ts",
    "server": "./node_modules/nodemon/bin/nodemon.js",
    "client": "npm start --prefix ../client",
    "dev": "concurrently \"npm run server\" \"npm run client\""
  },
"devDependencies": {
"@types/body-parser": "^1.17.0",
"@types/express": "^4.16.0",
"@types/jest": "^23.3.12",
"@types/mongoose": "^5.3.7",
"@types/node": "^10.12.18",
"@types/supertest": "^2.0.7",
"jest": "^23.6.0",
"nodemon": "^1.18.9",
"supertest": "^3.3.0",
"ts-jest": "^23.10.5",
"ts-node": "^7.0.1",
"typescript": "^3.2.2"

}

...