【问题标题】:Test Node.js API with Jest (and mockgoose)使用 Jest(和 mockgoose)测试 Node.js API
【发布时间】:2017-01-24 17:18:47
【问题描述】:

这里有两个问题:

1) Jest 是测试 Node.js(快速)API 的好选择吗?

2) 我正在尝试将 Jest 与 Mockgoose 一起使用,但我不知道如何建立连接并在之后运行测试。这是我在 SO 之前的最后一次尝试:

const Mongoose = require('mongoose').Mongoose
const mongoose = new Mongoose()
mongoose.Promise = require('bluebird')
const mockgoose = require('mockgoose')

const connectDB = (cb) => () => {
  return mockgoose(mongoose).then(() => {
    return mongoose.connect('mongodb://test/testingDB', err => {
      if (err) {
        console.log('err is', err)
        return process.exit()
      }
      return cb(() => {
        console.log('END') // this is logged
        mongoose.connection.close()
      })
    })
  })
}

describe('test api', connectDB((end) => {
  test('adds 1 + 2 to equal 3', () => {
    expect(1 + 2).toBe(3)
  })
  end()
}))

错误是Your test suite must contain at least one test。这个错误对我来说有点意义,但我不知道如何解决它。有什么建议吗?

输出:

Test suite failed to run

Your test suite must contain at least one test.

【问题讨论】:

    标签: node.js unit-testing mongoose jestjs


    【解决方案1】:

    答案很晚,但我希望它会有所帮助。 如果你注意的话,你的 describe 块里面没有测试函数。

    测试函数实际上是在回调中传递来描述的......有点,由于箭头函数回调,堆栈很复杂。

    这个示例代码会产生同样的问题..

    describe('tests',function(){
      function cb() {
        setTimeout(function(){
          it('this does not work',function(end){
            end();
          });
        },500);
      }
      cb();
    
      setTimeout(function(){
        it('also does not work',function(end){
          end();
        });
      },500);
    });
    

    由于与 mongo 的连接是异步的,当 jest 第一次扫描函数以在 describe 中查找“测试”时,它会失败,因为没有。 它可能看起来不像,但这正是您正在做的事情。
    我认为在这种情况下,您的解决方案有点太聪明了(以至于它不起作用),将其分解为更简单的语句可能有助于查明这个问题

    【讨论】:

      【解决方案2】:
          var mongoose = require('mongoose');
      // mongoose.connect('mongodb://localhost/animal', { useNewUrlParser: true });
      
      var db = mongoose.connection;
      db.on('error', console.error.bind(console, 'connection error:'));
      
          var kittySchema = new mongoose.Schema({
              name: String
            });
      
            var god = mongoose.model('god', kittySchema);
      
      
      
      module.exports = god;
      

      god.js 文件的代码

      describe("post api", () => {
        //life cycle hooks for unit testing
        beforeAll(() => {
          mongoose.connect(
            "mongodb://localhost/animal",
            { useNewUrlParser: true }
          );
        });
        //test the api functions
        test("post the data", async () => {
          console.log("inside the test data ");
      
          var silence = await new god({ name: "bigboss" }).save();
      
        });
        // disconnecting the mongoose connections
        afterAll(() => {
          // mongoose.connection.db.dropDatabase(function (err) {
          //     console.log('db dropped');
          //   //  process.exit(0);
          //   });
          mongoose.disconnect(done);
        });
      });
      

      Jest 测试代码..使用 jest 我们可以将名称存储在 db 中

      【讨论】:

        猜你喜欢
        • 2021-11-02
        • 2018-12-08
        • 2021-06-04
        • 2015-09-20
        • 2018-09-28
        • 1970-01-01
        • 1970-01-01
        • 2018-09-30
        • 2019-04-26
        相关资源
        最近更新 更多