Skip to content
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

Jest global setup & teardown #61

Open
JohanG2012 opened this issue Jul 28, 2019 · 1 comment
Open

Jest global setup & teardown #61

JohanG2012 opened this issue Jul 28, 2019 · 1 comment
Labels

Comments

@JohanG2012
Copy link
Collaborator

JohanG2012 commented Jul 28, 2019

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:

// globalSetup: './jest.init.ts',
globalTeardown: './jest.teardown.ts',

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:

globalAny.mongoServer = mongoServer as any;

That we then need access to in teardown to close the connection to avoid memory leaks during tests.

await globalAny.mongoServer.stop();

I tried to hack it by using node global:

const globalAny: any = global;

My hack does however not work and cause a error:
image

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).

server = app.listen();

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.
image

(async () => {
await setup();
})();

@JohanG2012
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant