如何写上的NodeJS PostgreSQL数据库单元测试

问题描述 投票:0回答:1

我试图写的测试我的PostgreSQL的API。

它似乎在测试不打正确的数据库响应或者也许我的配置是不恰当的测试。

我希望测试来检查我的令牌,并从身体当作REQ但测试甚至不认识的请求体(req.body);

请我需要指导。

我的文件如下。 test.js

/* global describe it */
import chai, { expect, assert } from 'chai';

import chaiHttp from 'chai-http';

import jwt from 'jsonwebtoken';

import server from '../app/app';

import parties from '../db/dummy';

process.env.NODE_ENV = 'test';

chai.should();
chai.use(chaiHttp);

const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJ0b3NpbkB5YWhvby5jb20iLCJpc19hZG1pbiI6dHJ1ZSwiaWF0IjoxNTQ5Mjc3MzAxLCJleHAiOjE1NDkzNjM3MDF9.ZJl147IVpw3TakPnR4uw_y6ZQZ6-Heoup5bdBBPs0iE';
const decoded = jwt.verify(token, process.env.SECRET_KEY);

describe('POST/api/v1/parties', () => {
  it('should return 201 for new party', (done) => {
    const newParty = {
      name: 'APGC',
      hqAddress: '1 Iyana Iba ',
      logoUrl: 'www.logo.net.png',
      decoded,
    };
    chai.request(server)
      .post('/api/v1/parties')
      .type('form')
      .send(newParty)
      .end((err, res) => {
        expect(decoded).be.a('string');
        expect(res.body).to.have.property('data');
        assert.isOk(res.body);
      });
    done();
  });
 });

我的package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "nyc mocha --compilers js:@babel/register server/test/*.spec.js --exit",
    "start:dev": "npm run build && npm run serve",
    "start": "nodemon --exec babel-node server/app/app.js",
    "heroku": "node server/app/app.js",
    "build": "babel server -d lib",
    "serve": "node lib/app/app.js",
    "coverage": "nyc report --reporter=text-lcov | coveralls",
    "migration": "psql -U postgres -p 5433 -f server/model/table.sql",
    "seed": "babel-node server/model/seeding.js",
    "data:dev": "npm run migration && npm run seed"
  },
  "author": "darot",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "body-parser": "^1.18.3",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "express-jwt": "^5.3.1",
    "jsonwebtoken": "^8.4.0",
    "morgan": "^1.9.1",
    "pg": "^7.8.0"
  },
  "devDependencies": {
    "@babel/cli": "^7.2.3",
    "@babel/core": "^7.2.2",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.3.1",
    "@babel/register": "^7.0.0",
    "babel-eslint": "^10.0.1",
    "babel-jest": "^24.0.0",
    "babel-loader": "^8.0.5",
    "babel-preset-env": "^1.7.0",
    "babel-register": "^6.26.0",
    "chai": "^4.2.0",
    "chai-http": "^4.2.1",
    "codecov": "^3.1.0",
    "coveralls": "^3.0.2",
    "cross-env": "^5.2.0",
    "eslint": "^5.12.1",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.15.0",
    "istanbul": "^0.4.5",
    "mocha": "^5.2.0",
    "nodemon": "^1.18.9",
    "nyc": "^13.1.0",
    "travis": "^0.1.1"
  }
}

我的数据库配置

import pg from 'pg';

import dotenv from 'dotenv';

dotenv.config();

process.env.NODE_ENV = 'test';

let connection;
const string = {
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME,
  password: process.env.DB_PASS,
  port: process.env.DB_PORT,
  max: 10,
  idleTimeoutMillis: 3000,
};
const stringTest = {
  user: process.env.DB_USER,
  host: process.env.DB_HOST,
  database: process.env.DB_NAME2,
  password: process.env.DB_PASS,
  port: process.env.DB_PORT,
  max: 10,
  idleTimeoutMillis: 3000,
};
if (process.env.NODE_ENV === 'production') {
  connection = {
    connectionString: process.env.DATABASE_URL,
    ssl: true,
  };
} else if (process.env.NODE_ENV === 'test') {
  connection = stringTest;
} else {
  connection = string;
}
const pool = new pg.Pool(connection);

export default pool;
javascript node.js postgresql express nodejs-stream
1个回答
0
投票

我能够在测试套件.set('Authorization', token) POST方法下设置的授权来解决这个

© www.soinside.com 2019 - 2024. All rights reserved.