You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While writing this issue I'm not able to find any proper examples of this. But the idea is the file set as globalSetupFile exports a function that will run before all the test files, and the same goes for globalTeardownFile but then the function will run after all the test files.
However, there has been an issue to get this to work with ts-jest (cause is an implementation detail in jest) See this issue: kulshekhar/ts-jest#411
However according to this comment: kulshekhar/ts-jest#411 (comment) this implementation detail should now have been fixed in Jest, but it's a bit unclear if it's now supposed to work with ts-jest or not.
I've tried to use it, without success, so I decided to hack a temporary solution. Which is the reason for globalSetupFile being commented out in jest.setup.js
The way I hacked it was to just add the setup to jest.setup.js instead, and then atempted to use node globals instead of jest globals which casuse some issues.
What we want to do is to move to globalSetupFile & globalTeardown files, however, below you can see a list of the issues that my hack causes - all of which should be soved once we move to the global solution.
No access to globals
1.1.
In the setup we do things that we need access to in teardown, for example we setup the server:
If we have access to globals we can do this in setup instead and avoid doing it in every file.
Test fails
Sometimes the test completely fails (run them again and they work) I susspect this has to do with the self invoke of function that I do in the jest.setup.js to mimick globalSetupFile.
Jest has a (kind of) new feature that allows for global setup and teardown of tests.
These features are so new that it seems like they are not in the documentation yet (at least I couldn't find them)
These are setup with the globalSetupFile and globalTeardownFile options:
rest-api-backend/jest.config.js
Lines 17 to 18 in 7fbd989
While writing this issue I'm not able to find any proper examples of this. But the idea is the file set as globalSetupFile exports a function that will run before all the test files, and the same goes for globalTeardownFile but then the function will run after all the test files.
However, there has been an issue to get this to work with ts-jest (cause is an implementation detail in jest) See this issue: kulshekhar/ts-jest#411
However according to this comment: kulshekhar/ts-jest#411 (comment) this implementation detail should now have been fixed in Jest, but it's a bit unclear if it's now supposed to work with ts-jest or not.
I've tried to use it, without success, so I decided to hack a temporary solution. Which is the reason for globalSetupFile being commented out in jest.setup.js
The way I hacked it was to just add the setup to jest.setup.js instead, and then atempted to use node globals instead of jest globals which casuse some issues.
What we want to do is to move to globalSetupFile & globalTeardown files, however, below you can see a list of the issues that my hack causes - all of which should be soved once we move to the global solution.
No access to globals
1.1.
In the setup we do things that we need access to in teardown, for example we setup the server:
rest-api-backend/jest.setup.ts
Line 15 in 7fbd989
That we then need access to in teardown to close the connection to avoid memory leaks during tests.
rest-api-backend/jest.teardown.ts
Line 10 in 7fbd989
I tried to hack it by using node global:
rest-api-backend/jest.setup.ts
Line 6 in 7fbd989
My hack does however not work and cause a error:
1.2.
Right now we start the webserver in every test, because we need to access it in every test (and right now globals does not work).
rest-api-backend/src/routes/User/index.test.ts
Line 13 in 7fbd989
If we have access to globals we can do this in setup instead and avoid doing it in every file.
Test fails
Sometimes the test completely fails (run them again and they work) I susspect this has to do with the self invoke of function that I do in the jest.setup.js to mimick globalSetupFile.
rest-api-backend/jest.setup.ts
Lines 43 to 45 in 7fbd989
The text was updated successfully, but these errors were encountered: