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

Using the "css" tag in runtime is not supported. Make sure you have set up the Babel plugin correctly. #617

Closed
duong-se opened this issue May 21, 2020 · 16 comments
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked bundler: webpack 📦 Issue is related to webpack bundler needs: complete repro 🖥️ Issue need to have complete repro provided

Comments

@duong-se
Copy link

Environment

Development

  • Linaria version: ^1.3.3
  • Bundler (+ version): ^1.3.3
  • Node.js version: v10.20.1
  • OS: MacOS

Description

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

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, '../')
    },
...

Reproducible Demo

http://g.recordit.co/f53GQ6OPWt.gif

@duong-se duong-se added bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided needs: triage 🏷 Issue needs to be checked and prioritized labels May 21, 2020
@github-actions github-actions bot added bundler: webpack 📦 Issue is related to webpack bundler and removed needs: triage 🏷 Issue needs to be checked and prioritized labels May 21, 2020
@TMaszko
Copy link
Contributor

TMaszko commented May 21, 2020

Hi, Could you provide a repository where we can reproduce this bug? We are not able to reproduce all bugs by ourselves. Thanks :)

@duong-se
Copy link
Author

@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

@satya164
Copy link
Member

Setup the Babel plugin like the error says https://github.com/callstack/linaria#setup

Add the Linaria preset to your babel.config.js

@duong-se
Copy link
Author

@satya164 I tried to set up babel.config.js and the error still appear. So that is the reason why I raise bug issue

@duong-se
Copy link
Author

duong-se commented May 21, 2020

@satya164
Copy link
Member

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.

@duong-se
Copy link
Author

@satya164 I also have config for the repo which I use linaria by extends from root config
image

@TMaszko
Copy link
Contributor

TMaszko commented May 21, 2020

@duong-se
Copy link
Author

There is 2 ways to solve this problem

  1. Use the babel-jest like the link above. So for the jest config you need to add this one
...
transform: {
  "^.+\\.[jt]sx?$": "babel-jest",
}
...
  1. If you use ts-jest for jest test, you should mock linaria in setupTest.ts
...
jest.mock('linaria', () => {
  css: jest.fn(() => ''),
  cx: jest.fn(() => ''),
})
...

@jakeleboeuf
Copy link

There is 2 ways to solve this problem

  1. Use the babel-jest like the link above. So for the jest config you need to add this one
...
transform: {
  "^.+\\.[jt]sx?$": "babel-jest",
}
...
  1. 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 #636 (comment). 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;
`;

@turadg
Copy link
Contributor

turadg commented Aug 26, 2020

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);
      },
    }),
  };
});

@fxOne
Copy link

fxOne commented Oct 30, 2020

With ts-jest you can also active the babelConfig option which also solves the issue.

// jest.config.js
module.exports = {
  // [...]
  globals: {
    'ts-jest': {
      babelConfig: true
    }
  }
};

@findawayer
Copy link

With ts-jest you can also active the babelConfig option which also solves the issue.

// jest.config.js
module.exports = {
  // [...]
  globals: {
    'ts-jest': {
      babelConfig: true
    }
  }
};

I like this approach because (1) I want to use ts-jest (2) mocking styled elements brings me <mock-styled.nodeName /> as result, while @fxOne's solution translates them into actual DOM elements; this makes selector methods like getByLabelText that @testing-library/jest-dom library be able to find them.

@JimmyLv
Copy link

JimmyLv commented Jan 5, 2022

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);
      },
    }),
  };
});

@turadg I found it does not work for wrapper styled(Comp)?

01-05-WebStorm-dingtalk-okr-client – jest config js 2

@glassdimlygr
Copy link

With ts-jest you can also active the babelConfig option which also solves the issue.

// jest.config.js
module.exports = {
  // [...]
  globals: {
    'ts-jest': {
      babelConfig: true
    }
  }
};

I did this, but this did not solve the issue for me.

@nvh95
Copy link

nvh95 commented Jun 14, 2022

This works for babel-jest

// jest.config.js
transform: {
    "^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "babel-jest",

Use linaria/babel instead of @linaria

module.exports = {
  presets: [
    ["@babel/preset-env", { targets: { node: "current" } }],
    ["@babel/preset-react", { runtime: "automatic" }],
    "linaria/babel",
  ],
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked bundler: webpack 📦 Issue is related to webpack bundler needs: complete repro 🖥️ Issue need to have complete repro provided
Projects
None yet
Development

No branches or pull requests

10 participants