-
Notifications
You must be signed in to change notification settings - Fork 18
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
Support named exports #8
Comments
@joaovieira, same problem here. Did you solve that? |
My work around for this is to mock jest.mock('./styles.scss', () => ({
__esModule: true,
default: {
class1: 'class1',
class2: 'class2',
},
})) Of course, having this automated by this plugin would be great. :) |
@gabsprates @john-d-pelingo sorry for not answering here, what I did was to define my own css mock with the modified identity-obj-proxy as mentioned above: // .jest/identity-obj-proxy-esm.js
// Modified identity-obj-proxy.
// This works around the fact we use ES named exports for styles, e.g.:
// import * as styles from './styles.scss'.
// https://github.com/keyanzhang/identity-obj-proxy/issues/8
module.exports = new Proxy(
{},
{
get: function getter(target, key) {
if (key === '__esModule') {
// True instead of false to pretend we're an ES module.
return true;
}
return key;
},
},
); // jest.config.js
module.exports = {
...
moduleNameMApper: {
'\\.(jpg|jpeg|png|gif|webp|svg)$': 'identity-obj-proxy',
'\\.(css|scss)$': '<rootDir>/.jest/identity-obj-proxy-esm.js',
}
}; |
I was literally about to post that later today. Seems like it works like a charm. 👏 |
@joaovieira Thank you so much! It works perfectly 🎉 👏 |
I use something like: const proxy = new Proxy(
{},
{
get: function getter(target, key) {
switch (key) {
case "__esModule":
return true;
case "default":
return proxy;
default:
return key;
}
}
}
);
module.exports = proxy; To handle some re export quirks. |
Hello guys, Thank you all to share your solution. There is a very more simple solution to make work Jest + Typescript + Css Module in 2020. Typescript compilation Test snapshot You don't need to modify 1/ Set the 2/ Add the definition file created for {
"extends": "../tsconfig",
"files": [
"../app/types/css-module.d.ts"
]
} This file allow the Typescript compiler to understand the import of the Css file and proceed to the compilation before the test. Test Result
I hope that help! |
This fixed it for me, thanks! |
@joaovieira thank you! 🙏 |
@nicolashemonic Only solution that worked for me. Thanks! |
When using CSS modules with named exports (e.g. you're using Typescript and typings-for-css-modules-loader), they look like:
And the consumer uses as:
This mock does not play nice. I've found that returning
true
for the__esModule
check here does work - essentially saying that our mocked module is an ES module and the keys are the named exports.Any ideas if and how this project could accommodate that? E.g. using
identity-obj-proxy/esm
?The text was updated successfully, but these errors were encountered: