开玩笑测试正在运行之前

时间:2018-04-28 11:11:02

标签: javascript node.js express jest

方案 [增订]

我在运行测试用例之前尝试连接到mongodb,如果我没有错,我可以使用在Jest中包含的beforeAll,我可以在运行测试用例之前连接到我的数据库,我也是用它测试我的REST api

测试

def show
  @final = final_params
  puts "Final Params are set to: #{@final.inspect}" # or logger debug if puts doesn't output anything to your log
  logger.debug  "Final Params are set to: #{@final.inspect}"
  @final 
end

DB

const request = require ('supertest');
const app = require ('../../app');
const db = require ('../../db.js');
const url = 'mongodb://localhost:27017';

//UPDATED beforeALL (thanks to @andreas-köberle)
beforeAll ((done) => {
    db.connect (url, (err) => {
        if (err) {
            console.log ('Unable to connect',err)
            process.exit(1)
          } else {
            console.log('success')
          }
    });
});


test('should response the GET method', async () => {
    console.log('DADAD');
    const res = await request (app).get ('/expense'); // I've set /expense in app (app.use('/expense,'expenseRoute)
    return expect(res.statusCode).toBe (200);
});

afterAll ( () => {
  db.close ();
});

ROUTE

const MongoClient = require ('mongodb').MongoClient;
const dbName = 'expenseTest';

let state = {
  db: null,
};

exports.connect = (url, done) => {
  if (state.db) return done ();

  MongoClient.connect (url, (err, client) => {
    const db = client.db(dbName);
    state.db = db;
    done ();
  });
};

exports.get = () => {
  return state.db;
};

exports.close = done => {
  if (state.db) {
    state.db.close ((err, res) => {
      state.db = null;
      done (err);
    });
  }
};

我在测试中有控制台日志,在我的GET路线和之前,这里是npm run test的输出

const express = require ('express')
const router = express.Router ()

const MongoClient = require ('mongodb').MongoClient
const assert = require ('assert')
let db = require ('../db')

/**
   * Returns the expense
   */
router.get ('/', (req, res) => {

  console.log(db.get());
  let expenseCollection = db.get ().collection ('expenseTrack')

  expenseCollection.find({}).toArray((err, docs) => {    
    res.status(200).send(docs)
  })
  //res.status(200).send('hello')
})

/**
   * Stores the expense in db
   */
router.post ('/', (req, res) => {
  let expenseCollection = db.get ().collection ('expenseTrack')
  expenseCollection.insert (req.body, (err, result) => {
    if (err) console.log (err)
    else res.status (200).send (result.ops)
  })
})

module.exports = router

很明显,它首先进入测试,如果我将我的端点代码更改为此,则所有测试用例都可以正常工作。

● Console

    console.log test/express/startupTest.test.js:18
      DADAD
    console.log routes/Expense.js:13
      null
    console.log test/express/startupTest.test.js:11
      Succesfully

在更新之前,所有它现在给我另一个错误/删除

/**
   * Returns the expense
   */
router.get ('/', (req, res) => {

  // console.log(db.get());
  // let expenseCollection = db.get ().collection ('expenseTrack')

  // expenseCollection.find({}).toArray((err, docs) => {    
  //   res.status(200).send(docs)
  // })
  res.status(200).send('hello')
})

0 个答案:

没有答案