-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using globalSetup and globalTeardown with TypeScript #10178
Comments
I have the same problem, but I think this sandboxing behavior is intended. The only cleaner solution I found to this is using |
You're right, I also define the database connection within the @impulse Could you elaborate what you mean with this statement:
|
From the docs
So this also affects tests that don't require a TypeORM connection. With /**
* @jest-environment ./path/to/typeorm-environment.ts
*/ |
Thank you for the clarification, that looks pretty neat! This would also be useful to remove the argument |
I think this sadly won't be possible
|
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..
This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days. |
This issue was closed because it has been stalled for 7 days with no activity. Please open a new issue if the issue is still relevant, linking to this one. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Description
I'm having a similar problem as in #5164. Consider the following working test code with
typeorm
: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
andglobalTeardown.ts
:Omitting the line
require('ts-node/register')
from both files throws this error:Keeping them in throws:
Version
Config
Thank you for pointing out my mistakes. As I'm new I tried googling but couldn't really find an answer if this is me not understanding the tool or a bug in the tool.
Found a similar issue here with a PR and a similar question on StackOverflow..
Workaround
The only workaround I have found thus far is to add the following code to every test file:
The text was updated successfully, but these errors were encountered: