test: add mock db test
This commit is contained in:
45
test/helper/db-mock.helper.ts
Normal file
45
test/helper/db-mock.helper.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server'
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
let mongod: MongoMemoryServer
|
||||
|
||||
/**
|
||||
|
||||
* Connect to mock memory db.
|
||||
*/
|
||||
const connect = async () => {
|
||||
mongod = await MongoMemoryServer.create()
|
||||
const uri = mongod.getUri()
|
||||
|
||||
await mongoose.connect(uri, {
|
||||
autoIndex: true,
|
||||
maxPoolSize: 10,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Close db connection
|
||||
*/
|
||||
const closeDatabase = async () => {
|
||||
await mongoose.connection.dropDatabase()
|
||||
await mongoose.connection.close()
|
||||
await mongod.stop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete db collections
|
||||
*/
|
||||
const clearDatabase = async () => {
|
||||
const collections = mongoose.connection.collections
|
||||
|
||||
for (const key in collections) {
|
||||
const collection = collections[key]
|
||||
await collection.deleteMany({})
|
||||
}
|
||||
}
|
||||
|
||||
export const dbHelper = {
|
||||
connect,
|
||||
close: closeDatabase,
|
||||
clear: clearDatabase,
|
||||
}
|
||||
@@ -77,29 +77,6 @@ describe.only('test /snippets', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// test('POST /snippets, should return 201', async () => {
|
||||
// mockingoose(model).toReturn(
|
||||
// {
|
||||
// ...mockPayload1,
|
||||
// },
|
||||
// 'create',
|
||||
// )
|
||||
// await app
|
||||
// .inject({
|
||||
// method: 'POST',
|
||||
// url: '/snippets',
|
||||
// payload: mockPayload1,
|
||||
// })
|
||||
// .then(async (res) => {
|
||||
// const json = res.json()
|
||||
// expect(res.statusCode).toBe(201)
|
||||
// expect(json).toBeDefined()
|
||||
// expect(json.name).toBe('Snippet_1')
|
||||
// // set mockingoose
|
||||
|
||||
// })
|
||||
// })
|
||||
|
||||
test('POST /snippets, re-create same of name should return 400', async () => {
|
||||
await app
|
||||
.inject({
|
||||
|
||||
81
test/src/modules/snippet/snippet.service.spec.ts
Normal file
81
test/src/modules/snippet/snippet.service.spec.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common'
|
||||
import { Test } from '@nestjs/testing'
|
||||
import { getModelForClass } from '@typegoose/typegoose'
|
||||
import { getModelToken } from 'nestjs-typegoose'
|
||||
import { dbHelper } from 'test/helper/db-mock.helper'
|
||||
import { SnippetModel, SnippetType } from '~/modules/snippet/snippet.model'
|
||||
import { SnippetService } from '~/modules/snippet/snippet.service'
|
||||
|
||||
describe.only('test Snippet Service', () => {
|
||||
let service: SnippetService
|
||||
|
||||
beforeAll(async () => {
|
||||
await dbHelper.connect()
|
||||
const moduleRef = Test.createTestingModule({
|
||||
providers: [
|
||||
SnippetService,
|
||||
{
|
||||
provide: getModelToken('SnippetModel'),
|
||||
useValue: getModelForClass(SnippetModel),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const app = await moduleRef.compile()
|
||||
await app.init()
|
||||
service = app.get(SnippetService)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await dbHelper.close()
|
||||
})
|
||||
|
||||
const snippet = {
|
||||
name: 'test',
|
||||
raw: '{"foo": "bar"}',
|
||||
type: SnippetType.JSON,
|
||||
private: false,
|
||||
reference: 'root',
|
||||
}
|
||||
let id = ''
|
||||
it('should create one', async () => {
|
||||
const res = await service.create(snippet)
|
||||
|
||||
expect(res).toMatchObject(snippet)
|
||||
expect(res.id).toBeDefined()
|
||||
|
||||
id = res.id
|
||||
})
|
||||
|
||||
it('should not allow duplicate create', async () => {
|
||||
await expect(service.create(snippet)).rejects.toThrow(BadRequestException)
|
||||
})
|
||||
|
||||
test('get only data snippet', async () => {
|
||||
const res = await service.getSnippetByName(snippet.name, snippet.reference)
|
||||
expect(res.name).toBe(snippet.name)
|
||||
expect(res.data).toBeDefined()
|
||||
})
|
||||
|
||||
test('get full snippet', async () => {
|
||||
const res = await service.getSnippetById(id)
|
||||
expect(res.name).toBe(snippet.name)
|
||||
expect(res.data).toBeDefined()
|
||||
})
|
||||
|
||||
test('modify', async () => {
|
||||
const newSnippet = {
|
||||
name: 'test',
|
||||
raw: '{"foo": "b"}',
|
||||
type: SnippetType.JSON,
|
||||
private: true,
|
||||
reference: 'root',
|
||||
}
|
||||
const res = await service.update(id, newSnippet)
|
||||
expect(res.raw).toBe(newSnippet.raw)
|
||||
})
|
||||
test('delete', async () => {
|
||||
await service.delete(id)
|
||||
await expect(service.getSnippetById(id)).rejects.toThrow(NotFoundException)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user