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

how can I use esbuild --inject flag with esbuild-jest #70

Open
daniloab opened this issue Feb 25, 2022 · 3 comments
Open

how can I use esbuild --inject flag with esbuild-jest #70

daniloab opened this issue Feb 25, 2022 · 3 comments

Comments

@daniloab
Copy link

daniloab commented Feb 25, 2022

Im having problem when running a test with a react js component inside of it. Since Im at on version 17 I do not import the react anymore.

Then, when running test complaint about this:

React is not defined

By now, I want to try fix it using the --inject flag to inject a shim.js file containing the react js import as explained here https://github.com/evanw/esbuild/blob/03a33e6e007467d99989ecf82fad61bd928a71aa/CHANGELOG.md#0717

Screen Shot 2022-02-25 at 15 48 58

Beyond this there is another way to fix this problem? this issue is related to this

I share here the problem I'm facing it https://twitter.com/daniloab_/status/1497258702662823936

@daniloab
Copy link
Author

daniloab commented Mar 7, 2022

I tried to do this but my tests keep breaking #61 (comment)

@ignaciomartinelias
Copy link

To solve this you could add React as a global variable in your setup file that runs after the testing environment is set.
First you will need to go to your jest.config.js file and see if you have any configuration for this setup (you should look for the property setupFilesAfterEnv and see if its set to any path). The file in this path will run before every test, so its a good place to have global mocks or imports ;). Here you can import React and attach it to the global object.

So...

config/jest.js

import * as React from 'react'

global.React = React

...  // the rest of your configuration 

jest.config.js

module.exports = {
    ...yourConfig,
    setupFilesAfterEnv: ["config/jest.js"]
}

@breitembach
Copy link

My MR resolve this problem
MR

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

No branches or pull requests

3 participants