Jest遇到意外的令牌Express

时间:2019-12-25 15:35:25

标签: node.js express mongoose jestjs jwt

在Jest中使用json网络令牌时遇到问题
它给我一个错误“ Jest遇到意外令牌”
我认为Jest Configuration存在问题,但我无法弄清楚它是什么

使用

  1. 表达
  2. nodejs
  3. 猫鼬

那是我的 package.json

{
  "name": "task-manager",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "env-cmd ./config/dev.env nodemon src/index.js",
    "test": "env-cmd ./config/test.env jest --watch"
  },
  "jest": {
    "testEnvironment": "node"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@sendgrid/mail": "^6.4.0",
    "bcryptjs": "^2.4.3",
    "express": "^4.17.1",
    "jsonwebtoken": "^8.5.1",
    "mongodb": "^3.4.0",
    "mongoose": "^5.8.1",
    "mongoose-hidden": "^1.8.1",
    "multer": "^1.4.2",
    "sharp": "^0.23.4",
    "validator": "^10.9.0"
  },
  "devDependencies": {
    "env-cmd": "^8.0.2",
    "jest": "^24.9.0",
    "nodemon": "^2.0.2",
    "supertest": "^3.4.1"
  }
}

user.test.js

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

//get userID
const userOneId = new mongoose.Types.ObjectId()
//seed user
const userOne = {
    _id : userOneId,
    name : 'Mo Salah',
    email : 'mosalah@liver.com',
    password : 'niceplayer11',
    tokens : [{
        token : jwt.sign({_id:userOneId},process.env.TOKEN_SECRET_KEY)
    }]
}


//beforeEach :: before execute test delete all users and store seed user
beforeEach(async ()=>{
    await User.deleteMany()
    await new User(userOne).save()
})


//test signUp user
test('siginUp user', async ()=>{
    await request(app).post('/users').send({
        name : 'Sama',
        email : 'sama@example.com',
        password : 'mypass22'
    }).expect(201)
})

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

//test login no user
test('login NoUser',async ()=>{
    await request(app).post('/users/login').send({
        email : 'lolo@ahmed.com',
        password : 'dsdff'
    }).expect(400)
})

// auth user get profile
test('auth user profile',async ()=>{
    await request(app)
    .get('users/me')
    .set('Authorization', `Bearer ${userOne.tokens[0].token}`)
    .send()
    .expect(200)
})

除使用令牌的测试外,所有测试均正常运行
任何建议将不胜感激

0 个答案:

没有答案