Giter VIP home page Giter VIP logo

typeorm-transactional-tests's Introduction

TypeORM transactional tests

Travis Coverage Status

TypeORM does not provide builtin transactional tests. If your tests write to a non in-memory database, probably you have to truncate or erase all your tables for every test case.

This package allows the creation of transactional contexts during the test, starting a transaction in the begining of the test and rolling back at the end. This is a faster solution than truncate/delete, once nothing is really written to disk.

Install

npm install --save-dev typeorm-transactional-tests

Or with yarn:

npm install --save-dev typeorm-transactional-tests

Typeorm compatibility

Versions 1.x.x of this library is compatible with typeorm 0.2.x

Versions 2.x.x of this library is compatible with typeorm 0.3.x

Usage

Jest

To apply the transactional context with Jest, just start the context in an beforeEach block and finish it in an afterEach:

import {Connection, getConnection } from 'typeorm';
import { TransactionalTestContext } from 'typeorm-transactional-tests';

let connection: Connection;
let transactionalContext: TransactionalTestContext;

beforeEach(async () => {
    connection = getConnection();
    transactionalContext = new TransactionalTestContext(connection);
    await transactionalContext.start();    
});

afterEach(async () => {
    await transactionalContext.finish();
});

Also, it is possible to apply the context to all your tests using a global Jest setup file. Add a new file on your test folder:

import { TransactionalTestContext } from 'typeorm-transactional-tests'

// @ts-ignore
global.beforeEach(async () => await transactionalContext.start());

// @ts-ignore
global.afterEach(async () => await transactionalContext.finish());

And point the Jest configuration to it:

 "setupFilesAfterEnv": [
   "<rootDir>/test/support/transactionalContext.ts"
  ]

typeorm-transactional-tests's People

Contributors

viniciusjssouza avatar clintonb avatar dependabot[bot] avatar andrew-chen-wang avatar omerd-cyera avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.