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

Importing libraries in setupTests.ts throws TypeScript errors #103

Closed
nareshbhatia opened this issue May 12, 2019 · 2 comments
Closed

Importing libraries in setupTests.ts throws TypeScript errors #103

nareshbhatia opened this issue May 12, 2019 · 2 comments
Labels
kind: support Asking for support with something or a specific use case solution: intended behavior This is not a bug and is expected behavior

Comments

@nareshbhatia
Copy link

Current Behavior

The following imports in setupTests.ts throw Typescript errors:

import 'react-testing-library/cleanup-after-each';

The above throws "ReferenceError: afterEach is not defined"

import 'jest-dom/extend-expect';

The above throws "ReferenceError: expect is not defined"

It is as if Jest does not recognize its own globals.

The same imports work correctly in test files.

Expected behavior

Library imports should work correctly in setup files.

Additional context

Here's my repo that shows this issue: https://github.com/nareshbhatia/react-base-tsdx

Currently the above imports are in the test files, so they work correctly. Just try yarn test.

Now uncomment the same imports in setupTests.ts (one at a time) and run yarn test. You will see the errors.

Your environment

Software Version(s)
TSDX 0.5.9
TypeScript 3.4.5
Browser N/A
npm/Yarn Yarn 1.15.2
Operating System MacOS
@hedgerh
Copy link
Contributor

hedgerh commented Jun 1, 2019

This is because the files passed to setupFiles in the jest config are ran before the test framework is installed in the environment. You should use setupFilesAfterEnv for any setup files that need to use globals like afterEach or expect.

Read more here: https://jestjs.io/docs/en/configuration#setupfilesafterenv-array

@nareshbhatia
Copy link
Author

Wow! Couldn't have figured this out without you. You are the best, @hedgerh!

@agilgur5 agilgur5 added kind: support Asking for support with something or a specific use case solution: intended behavior This is not a bug and is expected behavior labels Mar 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: support Asking for support with something or a specific use case solution: intended behavior This is not a bug and is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants