LoginSignup
0
0

More than 5 years have passed since last update.

mochaによるテスト、mongodb初期化

Last updated at Posted at 2018-08-02

前提:下記インストール済み

  • nuxt.js v1.0
  • mocha v5.2
  • mongodb v3.1
  • mongoose v5.2

目的:テスト前にmongoDB初期化

Install package

> npm install mocha --save-dev
> npm install supertest --save-dev
> npm install expect --save-dev

package.json にコマンド追加

package.json
"scripts": {
    "test-api": "export NODE_ENV=test || SET \"NODE_ENV=test\" && mocha api/**/**.test.js"
  },

実行コマンド

>npm run test-api

Seedの作成と初期化関数(populatePlaces)の呼び出し

api/tests/seed/seed.js

seed.js
const { ObjectID } = require("mongodb");
const { Place } = require("./../../models/place");
const places=[{
    _id : new ObjectID() ,
    place_id : '001',
    place_name : 'Software Park',
    location:{ type:'Point', coordinates:[ 100.529730,13.904549,]}
},{
    _id : new ObjectID() ,
    place_id : '002',
    place_name : 'CentralPlaza Changwattana',
    location:{ type:'Point', coordinates:[100.527834,13.903676 ]}
}]

const  populatePlaces = async function()  {
    this.timeout(10000); // change timeout 10sec
    await Place.remove({});
    await Place.insertMany(places);
}

module.exports = {
    populatePlaces,
}

server.test.js
const { populatePlaces } = require("./seed/seed");

//placesコレクションの初期化
beforeEach(populatePlaces);

// === 以下テストケース ===

追記予定

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0