-
Notifications
You must be signed in to change notification settings - Fork 429
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
Additional resolver is not working on jest #141
Comments
I won't have time to dig into this but did you try providing the config to Jest?
See
|
Going to close this as it's not bug related. This has been opened to cover Jest related notes #146. If you do happen to figure it out might be worth noting. |
@timarney I'm having the same difficulty finding a way to add For context, I'm following https://www.youtube.com/watch?v=gt8x6uODvEQ and have successfully run babel-loader in the webpack part of the
This results in babel-plugin-emotion not being loaded into Jest tests. When running test, this results:
What's the right way of doing this? (For a Typescript CRA specifically) |
@timarney Yes, I've attempted using How can I use To elaborate on why {
"test":{
},
"include":"/Users/stephenkoo/code/src/gitlab.com/stephenkoo/kanso/src",
"use":[
{
"loader":"/Users/stephenkoo/code/src/gitlab.com/stephenkoo/kanso/node_modules/ts-loader/index.js",
"options":{
"transpileOnly":true
}
}
]
}, to {
"test":{
},
"include":"/Users/stephenkoo/code/src/gitlab.com/stephenkoo/kanso/src",
"use":[
+ {
+ "loader":"/Users/stephenkoo/code/src/gitlab.com/stephenkoo/kanso/node_modules/babel-loader/lib/index.js"
+ },
{
"loader":"/Users/stephenkoo/code/src/gitlab.com/stephenkoo/kanso/node_modules/ts-loader/index.js",
"options":{
"transpileOnly":true
}
}
]
}, |
@stephenkoo Jest doesn't use Webpack at all, so trying to modify Webpack rules for it will not work. The config it is editing is a Jest Config. There are no loaders to modify for the Jest config, as it doesn't use Webpack loaders. Assuming that you've used create-react-app-typescript, the Setting up your jest function is documented here. The default Jest config given to your config-overrides jest function permits you to specify babel plugins through either .babelrc or in a babel section in your package.json file using the normal babel configuration options. If all you need to do for the Jest testing is to add babel plugins, you don't even need to specify the jest function - just create your babel configuration and it'll be automatically used. As an example, I have a project (also in typescript) where I use dynamic imports for code splitting. Node doesn't understand their syntax by default, so I need to use babel-plugin-dynamic-import-node. I wanted to restrict this to the testing environment only, as webpack's loaders handle it automatically. I also needed to transform some modules from es2015 to commonjs so that node would understand them. To do this, all that was required was to add the following section to my package.json file: {
...the rest of your package.json file...,
"babel": {
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs",
"dynamic-import-node"
]
}
}
}
} |
@dawnmist Thanks - I gave it a shot, but it's not working for
"babel": {
"env": {
"production": {
"plugins": [["emotion", { "hoist": true }]]
},
"development": {
"plugins": [["emotion", { "sourceMap": true, "autoLabel": true }]]
},
"test": {
"plugins": [["emotion", { "hoist": true }]]
}
}
} Result: Both Here's my full package.json: {
"lint-staged": {
"*.{ts,tsx}": [
"stylelint --fix",
"prettier --config .prettierrc --write",
"tslint --fix'",
"yarn run test:staged",
"git add"
],
"*.{json,md}": [
"prettier --config .prettierrc --write",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"scripts": {
"start": "react-app-rewired start --scripts-version react-scripts-ts",
"build": "react-app-rewired build --scripts-version react-scripts-ts",
"test": "react-app-rewired test --scripts-version react-scripts-ts --env=jsdom",
"test:staged": "cross-env CI=true react-app-rewired test --scripts-version react-scripts-ts --env=jsdom --findRelatedTests",
"eject": "react-app-rewired --scripts-version react-scripts-ts eject",
"lint:ts": "tslint 'src/**/*.{ts,tsx}'",
"lint:styles": "stylelint 'src/***/*.{ts,tsx}'"
},
"dependencies": {
"babel-loader": "^7.1.4",
"babel-plugin-emotion": "^9.2.0",
"babel-preset-env": "^1.7.0",
"emotion": "^9.2.3",
"react": "^16.4.0",
"react-app-rewired": "^1.5.2",
"react-dom": "^16.4.0",
"react-emotion": "^9.2.3",
"react-scripts-ts": "2.16.0"
},
"devDependencies": {
"@types/jest": "^23.0.2",
"@types/node": "^10.3.2",
"@types/react": "^16.3.17",
"@types/react-dom": "^16.0.6",
"cross-env": "^5.2.0",
"husky": "^1.0.0-rc.8",
"lint-staged": "^7.2.0",
"prettier": "^1.13.5",
"stylelint": "^9.2.1",
"stylelint-config-standard": "^18.2.0",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.3.1",
"typescript": "^2.9.1"
},
"babel": {
"env": {
"production": {
"plugins": [["emotion", { "hoist": true }]]
},
"development": {
"plugins": [["emotion", { "sourceMap": true, "autoLabel": true }]]
},
"test": {
"plugins": [["emotion", { "hoist": true }]]
}
}
}
}
|
@stephenkoo Have you got an example repo that I can take a look at? (And what I had said only applied to |
@dawnmist Definitely! https://gitlab.com/stephenkoo/kanso/tree/custom-setup Thanks for having a look. |
@stephenkoo I'm still tracing through what ts-jest/babel are doing with the test, but I found this bug report while looking up details about babel-plugin-emotion. I suspect that it may be related, but simply changing the module type to compile to doesn't fix it. Their sample repo may help though, as they have a "working" and "not working" example to trace through. |
@stephenkoo It looks like there is actually a known incompatibility between ts-jest and emotion. When the ts source is transpiled prior to running babel on it, it loses the information that the babel-plugin-emotion needs for it to be able to do its job. Take a look here: kulshekhar/ts-jest#480 |
@stephenkoo This syntax in App.tsx works with no other changes to your sample project: const StyledDiv = styled('div')`
color: pink;
flex: 1;
`; Not sure whether you're happy to switch to using it, but it is a relatively minor change (from |
@dawnmist Ah, yes - thanks for finding the root cause of the issue! I thought it must've been me not setting rewired up properly. You’re right - Sadly it looks like I'll have to choose either TypeScript or the optimisation benefits of the emotion plugin due to the issue you linked to. |
Hello,
Thank you for this project. I'm using the following config override to be able to import packages outside of project folder. But jest could not find the package that I'm loading from that path. Is that normal, should I also provide the same resolver to jest via
resolver
config?react@15.6.1
react-app-rewired@1.3.5
config-overrides.js
Error:
The text was updated successfully, but these errors were encountered: