nestjs test db

Solutions on MaxInterview for nestjs test db by the best coders in the world

showing results for - "nestjs test db"
Eya
05 Jun 2016
1// ../test/db.ts
2import { TypeOrmModuleOptions } from '@nestjs/typeorm';
3import { EntitySchema } from 'typeorm';
4
5type Entity = Function | string | EntitySchema<any>;
6
7export const createTestConfiguration = (
8  entities: Entity[],
9): TypeOrmModuleOptions => ({
10  type: 'sqlite',
11  database: ':memory:',
12  entities,
13  dropSchema: true,
14  synchronize: true,
15  logging: false,
16});
17
Davide
23 Nov 2018
1// books.service.test.ts
2import { Test, TestingModule } from '@nestjs/testing';
3import { HttpModule, HttpService } from '@nestjs/common';
4import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
5import { Repository } from 'typeorm';
6
7import { BooksService } from './books.service';
8import { Book } from './book.entity';
9import { createTestConfiguration } from '../../test/db';
10
11describe('BooksService', () => {
12  let module: TestingModule;
13  let service: BooksService;
14  let httpService: HttpService;
15  let repository: Repository<Book>;
16
17  beforeAll(async () => {
18    module = await Test.createTestingModule({
19      imports: [
20        HttpModule,
21        TypeOrmModule.forRoot(createTestConfiguration([Book])),
22        TypeOrmModule.forFeature([Book]),
23      ],
24      providers: [BooksService],
25    }).compile();
26
27    httpService = module.get<HttpService>(HttpService);
28    service = module.get<BooksService>(BooksService);
29    repository = module.get<Repository<Book>>(getRepositoryToken(Book));
30  });
31
32  afterAll(() => {
33    module.close();
34  });
35
36  it('should be defined', () => {
37    expect(service).toBeDefined();
38  });
39
Lana
10 Jun 2017
1/* src/utils/testing-helpers/createMemDB.js */
2import { createConnection, EntitySchema } from 'typeorm'
3type Entity = Function | string | EntitySchema<any>
4
5export async function createMemDB(entities: Entity[]) {
6  return createConnection({
7    // name, // let TypeORM manage the connections
8    type: 'sqlite',
9    database: ':memory:',
10    entities,
11    dropSchema: true,
12    synchronize: true,
13    logging: false
14  })
15}
16