craco-antd: Does not work with jest
Use craco test results in the error:
Test suite failed to run
/Users/xxx/sources/proj/node_modules/antd/es/card/style/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import '../../style/index.less';
^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
10 | AutoComplete,
11 | message,
> 12 | Select
| ^
13 | } from "antd";
14 | import axios from "axios";
Problem resolved when I changed back to react-scripts test.
BTW, craco start works fine.
Env
“craco-antd”: “^1.9.3”, “@craco/craco”: “^3.2.3”, “antd”: “^3.11.6”, “react-scripts”: “^2.1.1”
// craco.config.js
const CracoAntDesignPlugin = require("craco-antd");
module.exports = {
plugins: [
{
plugin: CracoAntDesignPlugin,
options: {
customizeTheme: {
"@primary-color": "#8440bd"
}
}
}
]
};
Thank you!
About this issue
- Original URL
- State: open
- Created 5 years ago
- Comments: 21 (6 by maintainers)
This can be resolved by setting jest to transform any modules that need it, by adding it to the
craco.config.jsjest section:There might be a better solution, but this works for me, as it was just an extension of a fix I needed for the same issue with
monaco-editor. I’m not sure if it can be fixed by default, as it relies on a config override callback, but it can at least be documented.At this stage I’m just doing initial setup of antd, so caveat that I haven’t actually seriously tested this beyond making sure my current simple tests pass! It’s also a fairly blunt fix, as it means antd and deps needs to be transformed as part of tests, which can add a good 10+ seconds to the initial cycle.
yea, just cloned your poc and tried some scenarios
react-scripts testpasses withimport Button from 'antd/lib/button'craco testpasses withimport Button from 'antd/lib/button'react-scripts testpasses withimport { Button } from 'antd'craco testfails withimport { Button } from 'antd'Curious why only function syntax works, not the object syntax?
Hi @ndbroadbent
I am pretty new to all those config.
I did some research last night, and try out
Unfortunately, none of them working.
I end up fallback to
craco-lessand write my own config. Everything is okay untill I addedbabel-plugin-importto the custom babel config,jestno longer works.My temporary workaround is opt-out from
babel-plugin-importintestenvironment. At this moment, I don’t have time to look further to find out the root cause.Note: the downside is we can’t test styling from
lessanymore.It works with
libusers, but projects withesare experiencing the issue now…This is a breaking change in the newest PR. There should be an option to use
libores.