forked from CatsMiaow/nestjs-project-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.service.spec.ts
47 lines (38 loc) · 1.3 KB
/
crud.service.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import type { DataSourceOptions } from 'typeorm';
import { Sampletable1 } from '#entity/sampledb1';
import { configuration } from '../../config';
import { CrudService } from './crud.service';
let moduleRef: TestingModule | undefined;
let crud: CrudService;
let idx: number;
beforeAll(async () => {
moduleRef = await Test.createTestingModule({
imports: [
TypeOrmModule.forRoot({ ...(<DataSourceOptions>(await configuration()).db) }),
TypeOrmModule.forFeature([Sampletable1]),
],
providers: [CrudService],
}).compile();
crud = moduleRef.get(CrudService);
});
test('create', async () => {
const result = await crud.create({ title: 'FooBar', content: 'Hello World', tags: ['new'] });
expect(result).toHaveProperty('id');
idx = result.id;
});
test('read', async () => {
expect(await crud.read(idx)).toBeInstanceOf(Sampletable1);
});
test('update', async () => {
expect(await crud.update(idx, { title: 'Blahblahblah', tags: ['update'] })).toHaveProperty('affected');
});
test('delete', async () => {
const result = await crud.remove(idx);
expect(result).toHaveProperty('affected');
expect(result.affected).toBe(1);
});
afterAll(async () => {
await moduleRef?.close();
});