Created
July 14, 2018 13:47
-
-
Save tomaash/b8096b42decea5f5564f7781515f2786 to your computer and use it in GitHub Desktop.
In memory mongoserver for jest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MongodbMemoryServer = require('mongodb-memory-server').default | |
const mongoose = require('mongoose') | |
const opts = { useMongoClient: true } // remove this option if you use mongoose 5 and above | |
const Fixtures = require('node-mongodb-fixtures') | |
const RecruiterTeam = require('../../models/recruiter/recruiterteam').RecruiterTeam | |
const sinon = require('sinon') | |
const firebase = require('firebase-admin') | |
function stubFirebase() { | |
sinon.stub(firebase, 'initializeApp', () => { }) | |
sinon.stub(firebase, 'auth', () => { return { createCustomToken: () => Promise.resolve('token') } }) | |
const ref = () => { return { set: () => { } } } | |
sinon.stub(firebase, 'database', () => { return { ref } }) | |
} | |
stubFirebase() | |
const api = require('../../api') | |
let mongoServer | |
let fixtures | |
// const fixtures = new Fixtures() | |
describe('sample test', () => { | |
beforeAll(async () => { | |
mongoServer = new MongodbMemoryServer() | |
fixtures = new Fixtures() | |
const mongoUri = await mongoServer.getConnectionString() | |
console.log(mongoUri) | |
await fixtures.connect(mongoUri) | |
await fixtures.load() | |
await mongoose.connect(mongoUri, opts, (err) => { | |
if (err) console.error(err) | |
}) | |
await api() | |
}) | |
afterAll(async () => { | |
await api.stop() | |
await mongoose.disconnect() | |
await fixtures.unload() | |
await fixtures.disconnect() | |
await mongoServer.stop() | |
}) | |
it('should work', () => { | |
expect(10).toMatchSnapshot() | |
}) | |
it('should work 2', () => { | |
expect(['blah', 123]).toMatchSnapshot() | |
}) | |
it('should work 3', () => { | |
expect({ foo: 'bar' }).toMatchSnapshot() | |
}) | |
it('should count new users', async () => { | |
const User = mongoose.model('User', new mongoose.Schema({ name: String })) | |
const cnt = await User.count() | |
expect(cnt).toEqual(0) | |
}) | |
it('should find first recruiterteam', async () => { | |
const teams = await RecruiterTeam.find() | |
expect(teams[0].name).toMatchSnapshot() | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment