Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global functions in TypeScript for jest testing

I'm using create-react-app-typescript and want to create a function that will be available in every test file similar to jest's globals.

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

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.

like image 800
ssylviageo Avatar asked Jun 28 '18 20:06

ssylviageo


1 Answers

global.wrapIntl = createIntlWrapper('en', enMessages);

it's the node's equivalent to window.someGlobalVariable = 'something'

like image 176
Herman Starikov Avatar answered Sep 17 '22 16:09

Herman Starikov