how to create mongoose connection when testing with gulp mocha -
i want make gulp task run tests mocha 1 single command. (my test need connection db mongoose)
then got error: { [error: trying open unclosed connection.] state: 1 }
after research, found post: mongoose trying open unclosed connection
it said should use createconnection()
instead of 'connect()`.
here question now:
how use before
, after
test, , how call done
after db connected before go through each test cases?
following gulp task:
gulp.task('mocha', function () { var testfile = argv['f']; var fullpaths; if (testfile) { fullpaths = paths.server + 'tests/' + testfile; } else { fullpaths = serverunittestfiles; } var dbconnectionmodulepath = paths.server + 'tests/db-connection.js'; var dbconnection = gulp.src(dbconnectionmodulepath, { read: false }); var tests = gulp.src(fullpaths, { read: false }); return series(dbconnection, tests) .pipe(mocha({ reporter: 'nyan' })); });
and here db-connection.js module:
'use strict'; var mongoose = require('mongoose'), connection = mongoose.connection, dbname = 'learnmongo'; before(function (done) { connection.on('error', console.error); connection.once('open', function () { done(); }); mongoose.connect('mongodb://localhost:27017/' + dbname); }); after(function (done) { connection.close(done); });
Comments
Post a Comment