Development
-It run correct for webpack but cannot run in jest test snapshot

My webpack config
...
{
test: /\.(ts|tsx)$/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
[
'@babel/preset-env',
{
useBuiltIns: 'entry',
corejs: '3',
targets: {
esmodules: true,
chrome: '58',
ie: '11',
},
},
],
'linaria/babel',
],
},
},
{
loader: 'linaria/loader',
options: {
sourceMap: process.env.NODE_ENV !== 'production',
},
},
{ loader: 'ts-loader', options: { happyPackMode: true, transpileOnly: true } },
require.resolve('react-docgen-typescript-loader'),
],
},
{
test: /\.stories\.tsx?$/,
loaders: [
{
loader: require.resolve('@storybook/addon-storysource/loader'),
options: { parser: 'typescript' }
}
],
enforce: 'pre'
},
{
test: /\.scss$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: process.env.NODE_ENV !== 'production',
},
},
{
loader: 'css-loader',
options: {
sourceMap: process.env.NODE_ENV !== 'production',
},
},
'sass-loader'
],
include: path.resolve(__dirname, '../')
},
...
Hi, Could you provide a repository where we can reproduce this bug? We are not able to reproduce all bugs by ourselves. Thanks :)
@TMaszko My PR here https://github.com/reapit/foundations/pull/1303/files. This is public repo. So for the work around way is add jest.mock('linaria') to the test
Setup the Babel plugin like the error says https://github.com/callstack/linaria#setup
Add the Linaria preset to your babel.config.js
@satya164 I tried to set up babel.config.js and the error still appear. So that is the reason why I raise bug issue
@satya164 pls see in https://github.com/reapit/foundations/pull/1303/files#3
Make sure Jest is actually using your babel config. You have babel config at the root, but it's a monorepo, and jest probably doesn't read that babel config.
@satya164 I also have config for the repo which I use linaria by extends from root config

@tanphamhaiduong This could help https://jestjs.io/docs/en/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
There is 2 ways to solve this problem
...
transform: {
"^.+\\.[jt]sx?$": "babel-jest",
}
...
...
jest.mock('linaria', () => {
css: jest.fn(() => ''),
cx: jest.fn(() => ''),
})
...
There is 2 ways to solve this problem
- Use the babel-jest like the link above. So for the jest config you need to add this one
... transform: { "^.+\\.[jt]sx?$": "babel-jest", } ...
- If you use ts-jest for jest test, you should mock linaria in setupTest.ts
... jest.mock('linaria', () => { css: jest.fn(() => ''), cx: jest.fn(() => ''), }) ...
What about mocking styled from lineria/react? I have a Gatsby build and am running into the same problem as @viczhuravlev here https://github.com/callstack/linaria/issues/636#issue-650266792. Attempting to slide by with some mocks, but not sure how to handle all cases...
jest.mock("linaria/react", () => ({
styled: () => jest.fn(),
}));
// WORKS
styled(Link)`
display: none;
`;
// NOT WORKING
styled.header`
display: none;
`;
not sure how to handle all cases...
@jakeleboeuf this isn't a great solution but it does handle all the cases:
jest.mock('linaria/react', () => {
function styled(tag) {
return jest.fn(() => `mock-styled.${tag}`);
}
return {
styled: new Proxy(styled, {
get(o, prop) {
return o(prop);
},
}),
};
});
Withts-jest you can also active the babelConfig option which also solves the issue.
// jest.config.js
module.exports = {
// [...]
globals: {
'ts-jest': {
babelConfig: true
}
}
};