Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Global test functions in setupTests.ts #352

Closed
ssylvia opened this issue Jun 28, 2018 · 1 comment
Closed

Global test functions in setupTests.ts #352

ssylvia opened this issue Jun 28, 2018 · 1 comment

Comments

@ssylvia
Copy link

ssylvia commented Jun 28, 2018

Is it possible to write a function in the src/setupTest.ts file that will be available in every test file?

For example, I want to write a react-intl helper that I can use when testing components. I have the following code:

// src/setupTests.ts
import { createIntlWrapper } from 'test-utils/react/react-intl';
import enMessages from './assets/locales/en.json';

const wrapIntl = createIntlWrapper('en', enMessages);

The createIntlWrapper returns a function which mimic this helper function.

When I need to test components that have react-intl components, I want to be able to wrap the JSX with wrapIntl(<SampleComponent />) without importing the code above in every file.

@DorianGrey
Copy link
Collaborator

Would you mind explaining what exactly is the problem with importing that helper function in the files you use it? Suppose that's what modules are for, aren't they?

That said - strictly speaking, you can do that, I just wouldn't recommend it.
In the test setup file, you're allowed to modify the global window object, i.e. you might attach your function to it. But again, that's not recommended - you should just import it from the module whereever needed. You won't even hit any reasonable performance gap by doing so.

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

No branches or pull requests

2 participants