Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nestjs test db

/* src/utils/testing-helpers/createMemDB.js */
import { createConnection, EntitySchema } from 'typeorm'
type Entity = Function | string | EntitySchema<any>

export async function createMemDB(entities: Entity[]) {
  return createConnection({
    // name, // let TypeORM manage the connections
    type: 'sqlite',
    database: ':memory:',
    entities,
    dropSchema: true,
    synchronize: true,
    logging: false
  })
}
Comment

nestjs test db

// ../test/db.ts
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { EntitySchema } from 'typeorm';

type Entity = Function | string | EntitySchema<any>;

export const createTestConfiguration = (
  entities: Entity[],
): TypeOrmModuleOptions => ({
  type: 'sqlite',
  database: ':memory:',
  entities,
  dropSchema: true,
  synchronize: true,
  logging: false,
});
Comment

nestjs test db

// books.service.test.ts
import { Test, TestingModule } from '@nestjs/testing';
import { HttpModule, HttpService } from '@nestjs/common';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
import { Repository } from 'typeorm';

import { BooksService } from './books.service';
import { Book } from './book.entity';
import { createTestConfiguration } from '../../test/db';

describe('BooksService', () => {
  let module: TestingModule;
  let service: BooksService;
  let httpService: HttpService;
  let repository: Repository<Book>;

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [
        HttpModule,
        TypeOrmModule.forRoot(createTestConfiguration([Book])),
        TypeOrmModule.forFeature([Book]),
      ],
      providers: [BooksService],
    }).compile();

    httpService = module.get<HttpService>(HttpService);
    service = module.get<BooksService>(BooksService);
    repository = module.get<Repository<Book>>(getRepositoryToken(Book));
  });

  afterAll(() => {
    module.close();
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
Comment

PREVIOUS NEXT
Code Example
Javascript :: How to Delete Comment from Post on Node, express and Mongoose and Ajax 
Javascript :: jquery to javascript converter online 
Javascript :: return <Text using if condition react native 
Javascript :: document.elementFromPoint 
Javascript :: Quentin Michael Allums age 
Javascript :: tomtom map in vuejs 
Javascript :: how to program in javascript and jquery on a page 
Javascript :: kjkjl 
Javascript :: how to check expo package compatibility 
Javascript :: jsetracker 
Javascript :: feathersjs mysql example 
Javascript :: generate tabuada java script 
Javascript :: extract values from a column in json format python 
Javascript :: no styles are appearing in angular calendar 
Javascript :: discord.js bot credits command 
Javascript :: local = 1 
Javascript :: email collapsible section 
Javascript :: what is steal.js 
Javascript :: listen to document height changes 
Javascript :: get current page rows in tabulator js 
Javascript :: array of in javascript 
Javascript :: UI-router accessing parent state from child 
Javascript :: get every other item in an array 
Javascript :: javascript factor chain 
Javascript :: can you wrap redux provider within react.strictmode 
Javascript :: straforma in stringa js 
Javascript :: change items per page pagination angularjs 
Javascript :: js hangman with repeated characters 
Javascript :: chai expect array without order 
Javascript :: vuejs filter array by dates 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =