์๋
4.12.0
๋ด ์์ ์ฑ(Express+mangoose)์ Mocha/Chai ํ ์คํธ๋ฅผ ์ถ๊ฐํ๋๋ฐ ์ฑ์ ์คํํ๋ ๋์ ๋ณด์ด์ง ์๋ ๊ฒ์ ๋ฐ๊ฒฌํ์ต๋๋ค.
๋ฌธ์
๋ด ๋จ์ ํ
์คํธ๋ฅผ ์คํํ ๋ ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ๋์ง ์๊ณ ๋ง๊ตฌ์ค๋ก ์ธํด ์ ํด ์ํ๊ฐ ๋ฉ๋๋ค(๋ถ๋ช
ํ).
์๋ ์ฝ๋ ์ค๋ํซ์๋ ๋๋ผ์ด ๊ฒ์ด ์์ต๋๋ค... ํ ์คํธ ๋ง์ง๋ง์ ์ผ๋ถ mongoose#disconnect๋ฅผ ์๋ํ์ง๋ง ์๋ฌด ๊ฒ๋ ๋ณ๊ฒฝ๋์ง ์์์ต๋๋ค.
ํ๋ก์ธ์ค ์ข ๋ฃ๋ฅผ ์ฐจ๋จํ๋ ๊ฒ์ด ๋ฌด์์ธ์ง ์กฐ์ธ์ ๋ถํ๋๋ฆฝ๋๋ค.
const mongoose = require('mongoose');
import { databaseConfig } from './config/database';
export function setupDatabase() {
mongoose.Promise = require('bluebird');
//mongoose.set("debug", true)
var connection = mongoose.connect(databaseConfig.database, { useMongoClient: true }, (err) => { console.log("Connect error: " + err) });
mongoose.connection.on('connected', () => {
console.log('Connected to database ' + databaseConfig.database);
});
mongoose.connection.on('error', (err) => {
console.log('Database error: ' + err);
});
return mongoose;
}
mongoose.connect
์ ์ฌ์ฉํ ๋๋ง๋ค ํญ์ ์ด๋ฌํ ๊ฒฝ๊ณ ๊ฐ ๋ํ๋ฉ๋๋ค.
DeprecationWarning:
open()
๋ mongoose >= 4.11.0์์ ๋ ์ด์ ์ฌ์ฉ๋์ง ์์ต๋๋ค. ๋์openUri()
๋ฅผ ์ฌ์ฉํ๊ฑฐ๋connect()
๋๋createConnection()
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐuseMongoClient
์ต์ ์ ์ค์ ํ์ญ์์ค.createConnection()
. http://mongoosejs.com/docs/connections.html#use -mongo-client ์ฐธ์กฐ
im on node: 8.7 , ๋ชฝ๊ตฌ์ค: 4.12.2
์ด ์๋,
const mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
import { databaseConfig } from './config/database';
async function setupDatabase() {
// mongoose.set('debug', true);
return await mongoose.connection.openUri(databaseConfig.database);
}
(async function () {
const conn = await setupDatabase();
// closing instance
await conn.close();
}());
๊ธ์, ๋ด ๋์ ์ด๊ฒ์ ๋ง๊ตฌ์ค ์๋ชป์ด ์๋์๋ค.
๋๋ mocha๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ --exit
๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐํ์ฌ process.exit๋ฅผ ๊ฐ์ ๋ก ํธ์ถํด์ผ ํ์ต๋๋ค.
์ด ๋งค๊ฐ๋ณ์๊ฐ ์์ผ๋ฉด mocha๋... ํ ์คํธ๊ฐ ๋๋ ๋ ์๋ฌด ๊ฒ๋ ํ์ง ์๊ณ ๊ธฐ๋ค๋ฆฝ๋๋ค.
ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ ์ข
๋ฃํ๋ ๊ฒ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ ๋ฟ์
๋๋ค. ๋ฌธ์ ๋ ์ฌ๋ฌ ์ฐ๊ฒฐ์ด ์ด๋ ค ์๊ณ mongoose.disconnect()
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฌ์ ํ ์์ผ์ด ์ด๋ ค ์๋ ๊ฒ์ฒ๋ผ ๋ณด์
๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
ํ๋ก์ธ์ค๋ฅผ ๊ฐ์ ์ข ๋ฃํ๋ ๊ฒ์ ํด๊ฒฐ ๋ฐฉ๋ฒ์ผ ๋ฟ์ ๋๋ค. ๋ฌธ์ ๋ ์ฌ๋ฌ ์ฐ๊ฒฐ์ด ์ด๋ ค ์๊ณ
mongoose.disconnect()
๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ์ฌ์ ํ ์์ผ์ด ์ด๋ ค ์๋ ๊ฒ์ฒ๋ผ ๋ณด์ ๋๋ค.