9

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.

Joshua
  • 3,055
  • 3
  • 22
  • 37
ssylviageo
  • 195
  • 1
  • 8

1 Answers1

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

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

Herman Starikov
  • 2,636
  • 15
  • 26
  • 2
    I tried that and received the following error `Property 'wrapIntl' does not exist on type 'Global'.` I tried adding `// @ts-ignore` temporarily but it was still undefined in other test files. – ssylviageo Jul 02 '18 at 12:37
  • Ah, I see, then before that line try `declare let global:any;`. Or drop the `global.` part and add `declare let wrapIntl: any;`. `declare` is the way to create global variables in typescript. – Herman Starikov Jul 02 '18 at 20:21
  • great! should be accepted answer @ssylviageo! – Andresch Serj Jan 15 '21 at 11:06