Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

How to use global setup for setting up typeorm database and seeding it? #73

Closed
mithi opened this issue Oct 30, 2020 · 0 comments · Fixed by #74
Closed

How to use global setup for setting up typeorm database and seeding it? #73

mithi opened this issue Oct 30, 2020 · 0 comments · Fixed by #74

Comments

@mithi
Copy link
Owner

mithi commented Oct 30, 2020

I want to run a global set up (creating the database and seeding it) before executing any test files.
I don't want to run them in every test file, I only need to run them once. I can't seem to find a way to do this.

Related issues

Guide / Sample for integration Testing [Jest] #5308

typeorm/typeorm#5308

So current problem is only how to pass connection properly so typeorm only uses 1 connection on multiple jest workers.
I'm having the same issue. When the connection is created in jest globalSetup it cannot be found within the tests. It's like the tests are running in a sandbox mode.

Using globalSetup and globalTeardown with TypeScript

jestjs/jest#10178

The code to create a connection and close it should be executed before all tests and after all tests are done, that's why we've added it to globalSetup.ts and globalTeardown.ts:

Organizing tests

https://homoly.me/posts/organizing-tests-with-jest-projects

Current test performance

Screen Shot 2020-10-30 at 6 05 10 PM

Screen Shot 2020-10-30 at 6 05 46 PM

@mithi mithi linked a pull request Oct 31, 2020 that will close this issue
@mithi mithi closed this as completed in #74 Oct 31, 2020
mithi added a commit that referenced this issue Oct 31, 2020
It is better to have two separate test environments (two test databases),
one for testing resolvers (which is closer to an integration test), and the other for testing everything else.

When testing graphql resolvers, seeding the database should only be done once as this is a slow operation. 
Currently, using 'globalSetup` and `globalTeardown` with jest doesn't work for setting up the `typeorm` database.
The tests are not able to find the connection. 
See:
#73
typeorm/typeorm#5308
jestjs/jest#10178

It is also not efficient to remove all of the entries for this database in order to start with a clean slate for
testing the models and testing the seeding operations. So what we do here is we have two test databases. 
1. a prepopulated frozen test database (called `test_db`) to be used by the graphql resolvers
2. an empty database (called `empty_test_db`) which we can safely mutate (like inserting and deleting operation) to test seeding functions, among others

So this pull request updates all the scripts and other files for the current tests to work

I have also refactored some of the test scripts to be more readable and also added 
a simple test for testing the `abilityById` graphql query..
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant