用Jest(和mockgoose)testingNode.js API

这里有两个问题:

1)Jest是testingNode.js(express)API的好select吗?

2)我试图用Jock与Mockgoose ,但我不知道如何build立连接和运行后的testing。 这是我来之前的最后一次尝试:

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 。 这个错误对我有点意义,但我不知道如何解决它。 有什么build议么?

输出:

 Test suite failed to run Your test suite must contain at least one test. 

答案很晚,但我希望这会有所帮助。 如果你注意,你的描述块里面没有testingfunction。

testing函数实际上是在callback函数里面传递来描述的,由于箭头函数的callback,堆栈很复杂。

这个例子的代码会产生同样的问题。

 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的连接是asynchronous的,所以当jest第一次扫描函数在描述内部find“tests”时,它就会失败,因为没有。 它可能看起来不像,但这正是你在做的。
我认为在这种情况下,你的解决scheme有点太聪明了(到了不起作用的地步),把它分解成简单的陈述可能有助于指出这个问题