【问题标题】:JestJs TestSuite is not creating a Database using a mongooseJestJs TestSuite 没有使用猫鼬创建数据库
【发布时间】:2020-09-24 15:30:48
【问题描述】:

我是第一次使用 jestjs,遇到以下问题。请查看 ISSUE 标签。

下面是代码

const request = require('supertest');
const { app } = require('../src/app');
const { User } = require('../src/models/user');

const userOne = {
  name: 'Mike',
  age: 45,
  email: 'Mike1@gmail.com',
  password: '5689@wWe',
};

beforeEach(async () => {
  await User.deleteMany();
  await new User(userOne).save();
});

// afterEach(() => {
//   console.log('after Each');
// });

test('Should signup a new user', async () => {
  await request(app).post('/users').send(userOne).expect(201);
});

test('Should signin a user', async () => {
  await request(app)
    .post('/users/login')
    .send({
      email: userOne.email,
      password: userOne.password,
    })
    .expect(200);
});

test('Should not login non existent user', async () => {
  await request(app)
    .post('/users/login')
    .send({
      email: userOne.email,
      password: '5689@wWe1',
    })
    .expect(400);
});

这是我的 package.json 文件

{
  "name": "mongodb-promises",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "env-cmd -f ./config/dev.env nodemon src/index.js",
    "test": "env-cmd -f ./config/dev.env jest --watch"
  },
  "jest": {
    "testEnvironment": "node"
  },
  "author": "Nagendra Babu",
  "license": "ISC",
  "dependencies": {
    "@sendgrid/mail": "^7.1.1",
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.5.7",
    "mongoose": "^5.9.16",
    "mongoose-auto-increment": "^5.0.1",
    "multer": "^1.4.2",
    "sharp": "^0.25.3",
    "validator": "^13.0.0"
  },
  "devDependencies": {
    "env-cmd": "^10.1.0",
    "jest": "^26.0.1",
    "supertest": "^4.0.2"
  }
}

test.env 文件

MONGODB_CONNECTION=mongodb://127.0.0.1:27017/task-manager-api-test-db

dev.env 文件

MONGODB_CONNECTION=mongodb://127.0.0.1:27017/task-manager-api

数据库图片 My current database

问题 1.当我运行上面的测试套件时,jestjs并没有在mongodb中创建新的数据库。 请帮我看看我做错了什么。

运行 npm run test 时终端中的测试结果 工作进程未能正常退出并被强制退出。这可能是由于不正确的拆卸导致测试泄漏造成的。尝试使用 --runInBand --detectOpenHandles 运行以查找泄漏。 测试套件:1 个失败,总共 1 个 测试:1 次失败,2 次通过,共 3 次 失败测试/user.test.js × 应该注册一个新用户(187 毫秒) √ 应登录用户(66 毫秒) √ 不应该登录不存在的用户(51 毫秒)

● 应该注册一个新用户

expected 201 "Created", got 400 "Bad Request"

  at Test._assertStatus (node_modules/supertest/lib/test.js:268:12)
  at Test._assertFunction (node_modules/supertest/lib/test.js:283:11)
  at Test.assert (node_modules/supertest/lib/test.js:173:18)
  at Server.localAssert (node_modules/supertest/lib/test.js:131:12)

测试套件:1 个失败,总共 1 个 测试:1 次失败,2 次通过,共 3 次 快照:共 0 个 时间:2.398 s,估计 3 s 运行与更改的文件相关的所有测试套件。

【问题讨论】:

  • 你是如何连接到数据库的?
  • 我正在连接到另一个文件中的数据库。 ``` const mongoose = require('mongoose');常量验证器 = 要求('验证器'); mongoose.connect( process.env.MONGODB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false, }); module.exports = {猫鼬,验证器}; ```
  • 当我运行 npm run dev 时它工作正常当我运行 npm run test 时失败

标签: node.js express mongoose jestjs supertest


【解决方案1】:

你也在听米德的课吗? 您实际上可以通过转到/users 之类的端点并记录错误输出来检查错误。 这里的'Bad Request'数据验证错误

这是我的参考代码:

const request = require('supertest')
const app = require('../src/app')
const User = require('../src/models/user')

const userOne = {
    name: 'Hello1',
    email: 'Hello@example.com',
    password: 'HelloWorld1'
}

beforeEach(async()=>{
    await User.deleteMany()
    await new User(userOne).save()
})

test('Should sign up a new user', async()=>{
    await request(app).post('/users').send({
        name: 'Abcdef',
        email: 'abcde@example.com',
        password: 'HelloWorld1'
    }).expect(201)
})

test('Should Login Existing User', async()=>{
    await request(app).post('/users/login').send({
        email: userOne.email,
        password: userOne.password
    }).expect(200)
})

test('Should not login nonexistant User', async()=>{
    await request(app).post('/users/login').send({
        email: userOne.email,
        password: '567abcd'
    }).expect(400)
})

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 2012-09-29
    • 2018-12-09
    • 1970-01-01
    • 2014-01-09
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多