jest tests are running beforeAll

喜欢而已 提交于 2019-12-24 22:52:04

问题


Scenario[UPDATED]

I'm trying to connect to mongodb before running test cases and if I'm not wrong I can use beforeAll which is included in Jest where I can connect to my DB before running test cases, I am also testing my REST api with it

Test

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 ();
});

DB

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);
    });
  }
};

ROUTE

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

I have console logs in Test,in my GET route and in beforeAll, here's the output of npm run test

● 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

So It's clear that it's coming in Test first, If I change my endpoint code to this all test case works fine.

/**
   * 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')
})

After updating beforeAll it is now giving me another error/excpetion

 Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

来源:https://stackoverflow.com/questions/50075889/jest-tests-are-running-beforeall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!