Timeout when attempting to connect to mongo from jest unit tests
Jest-jasmine is different from Jasmine.
The syntax you used actually belongs to Jasmine 2.0+, not Jest-jasmine. So if you want to continue to use jest, you have to look into jest docs to find the answer.
Even more, "beforeAll" is not a standard jest injected variables, see jest api.
I got your code run with Jasmine 2.3.4, it runs just fine. I tried Promise/pit in jest to finish this job but failed.
First, install jasmine.
npm install -g jasminemkdir jasmine-test cd jasmine-testjasmine initjasmine examplestouch spec/mongodbspec.js
Here is my directory structure:
fenqideMacBook-Pro:jasmine-test fenqi$ ls -R.:spec/./spec:mongodbspec.js support/./spec/support:jasmine.json
Then, edit spec/mongodbspec.js, I just add one line " 'use strict'; " to your code.
'use strict';const mongoose = require('mongoose');describe('MyTest', () => { beforeAll((done) => { mongoose.connect('mongodb://127.0.0.1:27017/test'); let db = mongoose.connection; db.on('error', (err) => { done.fail(err); }); db.once('open', () => { done(); }); }); it('has some property', () => { // should pass but actually never gets run expect(1).toBe(1); });});
Last, run 'jasmine' :
fenqideMacBook-Pro:jasmine-test fenqi$ jasmineStarted.1 spec, 0 failuresFinished in 0.023 seconds