Installations
npm install typeorm-test-transactions
Releases
Unable to fetch releases
Developer
entrostat
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
14.17.3
NPM Version
6.14.13
Statistics
63 Stars
234 Commits
5 Forks
2 Watching
4 Branches
2 Contributors
Updated on 04 Nov 2024
Languages
TypeScript (90.43%)
Shell (6.42%)
JavaScript (2.18%)
Dockerfile (0.96%)
Total Downloads
Cumulative downloads
Total Downloads
533,508
Last day
-26.2%
211
Compared to previous day
Last week
69.2%
3,598
Compared to previous week
Last month
118.7%
9,717
Compared to previous month
Last year
-14.1%
118,410
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
TypeORM Test Transactions
Have you wanted to run tests on a project that uses TypeORM directly on the database and in parallel? A lot of the time we can't do this because artefacts and data from other tests can affect the result of our current tests. What usually happens, in this case, is that our tests become quite complicated when database entities are involved because we need to track exact entities. Our count()
and other aggregations must have where
clauses so that we don't see results from other tests that have already completed.
This library introduces a way to wrap tests in a transaction and automatically roll back the commits when the test ends. By doing this, you are able to run multiple tests concurrently and their data will not be seen by others.
You may argue that we should mock out the entities and not use a database at all. This is a valid point, but sometimes we want to test database constraints and the effects they can have on application logic.
Credit
Before I start, I did not add much to get this to work. The major reason why this is possible is because of the work done by odavid in his typeorm-transactional-cls-hooked library. Thanks for the great work odavid!
Limitations
It has come to my attention that this library is not consistent if you use the TypeORM
entity manager. So you'll want to use typeorm-transactional-cls-hooked
to add the @Transactional
decorator above function calls and those transactions will work correctly. What a colleague has found is that, when you use a TypeORM
entity manager, they are independent of each other and data may still write to the database.
Testing
Currently, I have created a small test suite that tests that there is a rollback in the transaction wrapper. The goal is to test as many database versions as possible, currently I have:
- MySQL 5.7
- MySQL 8.0
- MariaDB 10
- Postgres 9
- Postgres 10
- Postgres 11
- Postgres 12
- Postgres 13
The test badge represents tests that run over these databases:
Getting Started
In order to use this project you need to install the package,
1npm install --save typeorm-test-transactions 2 3# Not removing from the typeorm-transactional-cls-hooked 4# dependency separation. If you don't have the below 5# libraries then you'll need to install them as well 6# See https://github.com/odavid/typeorm-transactional-cls-hooked 7 8npm install --save typeorm reflect-metadata
When running your tests (I'm using jest
and nestjs
as the example), you'll want to wrap your test functions in the runInTransaction
function.
1import { 2 runInTransaction, 3 initialiseTestTransactions, 4} from 'typeorm-test-transactions'; 5import { DatabaseModule } from '@modules/database/database.module'; 6import { Test } from '@nestjs/testing'; 7 8initialiseTestTransactions(); 9 10describe('Feature1Test', () => { 11 beforeEach(async () => { 12 const module = await Test.createTestingModule({ 13 imports: [DatabaseModule], 14 }).compile(); 15 }); 16 17 describe('creation of 2 users', () => { 18 it( 19 'should allow me to create multiple users if the email address is different but name is the same', 20 runInTransaction(async () => { 21 await User.create({ 22 email: 'email1@test.com', 23 name: 'Name', 24 }).save(); 25 26 await User.create({ 27 email: 'email2@test.com', 28 name: 'Name', 29 }).save(); 30 31 expect(await User.count()).toEqual(2); 32 }), 33 ); 34 }); 35 36 describe('creation of one of the users in previous step', () => { 37 it( 38 'should allow me to create a user that is the same as the one in the previous step', 39 runInTransaction(async () => { 40 await User.create({ 41 email: 'email1@test.com', 42 name: 'Name', 43 }).save(); 44 45 expect(await User.count()).toEqual(1); 46 }), 47 ); 48 }); 49});
IMPORTANT! The example above wraps the entire test in a transaction and returns a function that jest
executes. If you'd like to wrap parts of a test in a transaction, you need to call the function and await
the result:
1import { 2 runInTransaction, 3 initialiseTestTransactions, 4} from 'typeorm-test-transactions'; 5import { DatabaseModule } from '@modules/database/database.module'; 6import { Test } from '@nestjs/testing'; 7 8initialiseTestTransactions(); 9 10describe('Feature1Test', () => { 11 beforeEach(async () => { 12 const module = await Test.createTestingModule({ 13 imports: [DatabaseModule], 14 }).compile(); 15 }); 16 17 describe('creation of the same user in different sequential transactions', () => { 18 it( 19 'allows me to create the same user twice if they were each in a transaction', 20 async () => { 21 await runInTransaction(async () => { 22 await User.create({ 23 email: 'email1@test.com', 24 name: 'Name', 25 }).save(); 26 27 expect(await User.count()).toEqual(1); 28 })(); 29 await runInTransaction(async () => { 30 await User.create({ 31 email: 'email1@test.com', 32 name: 'Name', 33 }).save(); 34 35 expect(await User.count()).toEqual(1); 36 })(); 37 } 38 ); 39 }); 40});
Troubleshooting
- There are some cases when the transactions don't roll back. So far, I've found the reason for that to be that the
typeorm
connection was started before this package was initialised. - I have seen an issue opened typeorm-transactional-cls-hooked initialization #30 where there may be a problem around where you perform the initialisation. Once I get feedback, I'll update the documentation here.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/unit-tests.yaml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'develop'
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/unit-tests.yaml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/entrostat/typeorm-test-transactions/unit-tests.yaml/develop?enable=pin
- Warn: containerImage not pinned by hash: devops/testing/backend/Dockerfile:1: pin your Docker image by updating node:lts to node:lts@sha256:5c76d05034644fa8ecc9c2aa84e0a83cd981d0ef13af5455b87b9adf5b216561
- Warn: npmCommand not pinned by hash: devops/testing/backend/Dockerfile:10
- Info: 0 out of 1 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 containerImage dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 3 are checked with a SAST tool
Reason
15 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-fx4w-v43j-vc45
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-776f-qx25-q3cc
Score
2.5
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to typeorm-test-transactions
typeorm
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.
typeorm-transactional
A Transactional Method Decorator for typeorm that uses cls-hooked to handle and propagate transactions between different repositories and service methods. Inpired by Spring Trasnactional Annotation and Sequelize CLS
allan-typeorm-test-transactions
A transactional wrapper for tests that use TypeORM that automatically rolls back the transaction at the end of the test.
@n8n/typeorm
Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.