You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/testing-css-in-js.md
+8-8
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
title: Testing CSS-in-JS
3
3
---
4
4
5
-
Popular CSS-in-JS libraries like [styled-components](https://github.com/styled-components/styled-components) or [emotion](https://github.com/emotion-js/emotion) can also be tested with the help of [jest-styled-components](https://github.com/styled-components/jest-styled-components) or [jest-emotion](https://github.com/emotion-js/emotion/tree/master/packages/jest-emotion) respectively. These packages improve Jest's built-in snapshot testing experience and are a great way to help avoid unintended changes to your website's UI. Please refer to your package's documentation to see if it also offers testing capabilities.
5
+
Popular CSS-in-JS libraries like [styled-components](https://github.com/styled-components/styled-components) or [emotion](https://github.com/emotion-js/emotion) can also be tested with the help of [jest-styled-components](https://github.com/styled-components/jest-styled-components) or [@emotion/jest](https://github.com/emotion-js/emotion/tree/master/packages/jest) respectively. These packages improve Jest's built-in snapshot testing experience and are a great way to help avoid unintended changes to your website's UI. Please refer to your package's documentation to see if it also offers testing capabilities.
6
6
7
-
_Snapshot serializers_ like `jest-styled-components` or `jest-emotion` modify the standard output to a more meaningful and readable snapshot, e.g. by removing unnecessary information or displaying data in another format. Which ultimately leads to more comparable and effective snapshot tests.
7
+
_Snapshot serializers_ like `jest-styled-components` or `@emotion/jest` modify the standard output to a more meaningful and readable snapshot, e.g. by removing unnecessary information or displaying data in another format. Which ultimately leads to more comparable and effective snapshot tests.
8
8
9
9
By default snapshots of your styled components show the generated class names (which you didn't set) and no styling information. When changing the styles you'll only see the diff of some cryptic class names (hashes). That's why you should use the above mentioned _snapshot serializers_. They remove the hashes and format the CSS in style elements.
10
10
@@ -13,10 +13,10 @@ For this example you'll use emotion. The testing utilities of emotion and glamor
As [Gatsby's emotion plugin](/packages/gatsby-plugin-emotion/) is using `babel-plugin-emotion` under the hood you'll also need to install it so that Jest can use it.
19
+
As [Gatsby's emotion plugin](/packages/gatsby-plugin-emotion/) is using `@emotion/babel-plugin` under the hood you'll also need to install it so that Jest can use it.
20
20
21
21
If you followed along with the [Unit testing guide](/docs/unit-testing) you'll have the file `jest-preprocess.js` at the root of your project. Open that file and add the plugin:
In order to tell Jest to use the serializer you'll need to create the file `setup-test-env.js` which will be run automatically before every test. Create the file `setup-test-env.js` at the root of your project. Insert this code into it:
If your styled component depends on `theme` via `ThemeProvider` you'll have two options:
90
90
91
91
- Wrap all your components with the `ThemeProvider`
92
-
- Use API helpers (have a look at the library's documentation, e.g. [styled-components](https://github.com/styled-components/jest-styled-components#theming) or [emotion](https://github.com/emotion-js/emotion/tree/master/packages/emotion-theming#createbroadcast-function))
92
+
- Use API helpers (have a look at the library's documentation, e.g. [styled-components](https://github.com/styled-components/jest-styled-components#theming) or [emotion](https://emotion.sh/docs/theming#api))
93
93
94
94
And this is where snapshots tests really shine. If you change, e.g. the primary color in your theme file you'll see which components get affected by this change. This way you can catch unintended changes to the style of your components.
Can't resolve '@emotion/core' in '/Users/you/tmp/gatsby-site/.cache' // highlight-line
53
+
Can't resolve '@emotion/react' in '/Users/you/tmp/gatsby-site/.cache' // highlight-line
54
54
55
55
File: .cache/develop-static-entry.js
56
56
```
57
57
58
-
This error is a result of Gatsby having failed to find `@emotion/core` because `gatsby-plugin-emotion` has been installed and added to the `gatsby-config`, without installing the emotion library. Install it like this:
58
+
This error is a result of Gatsby having failed to find `@emotion/react` because `gatsby-plugin-emotion` has been installed and added to the `gatsby-config`, without installing the emotion library. Install it like this:
59
59
60
60
```shell
61
-
npm install @emotion/core
61
+
npm install @emotion/react
62
62
```
63
63
64
-
Or replace `@emotion/core` with the name of the library that is missing. Installing the plugin and any necessary libraries as well as adding the plugin to your `gatsby-config` should resolve this error.
64
+
Or replace `@emotion/react` with the name of the library that is missing. Installing the plugin and any necessary libraries as well as adding the plugin to your `gatsby-config` should resolve this error.
| sourceMap | boolean | Tells the plugin to inject source maps for use in browser dev tools in development. |`true`||
47
-
| autoLabel |boolean| Automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to. You can read more about this option in [`babel-plugin-emotion`'s docs](https://emotion.sh/docs/babel-plugin-emotion#autolabel)|`process.env.NODE_ENV !== 'production'`||
47
+
| autoLabel |`'dev-only'|'always' | 'never'` |Automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to. You can read more about this option in [`@emotion/babel-plugin`'s docs](https://emotion.sh/docs/@emotion/babel-plugin#autolabel)|`dev-only`||
48
48
| labelFormat | string | Only works when `autoLabel` is set to true. It allows you to define the format of the resulting label. The format is defined via string where variable parts are enclosed in square brackets []. For example `labelFormat: "my-classname--[local]"`, where `[local]` will be replaced with the name of the variable the result is assigned to. | "[local]" ||
49
49
| cssPropOptimization | boolean | Assumes that you are using something to make `@emotion/react`’s jsx function work for all jsx. If you are not doing so and you do not want such optimizations to occur, disable this option. |`true`||
`This option tells the plugin to inject source maps for use in browser dev tools in development.`
18
18
),
19
-
autoLabel: Joi.boolean()
20
-
.default(process.env.NODE_ENV!==`production`)
19
+
autoLabel: Joi.string()
20
+
.default(`dev-only`)
21
21
.description(
22
-
`This option automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to. You can read more about this option in babel-plugin-emotion's docs: https://emotion.sh/docs/babel-plugin-emotion#autolabel`
22
+
`This option automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to. You can read more about this option in @emotion/babel-plugin's docs: https://emotion.sh/docs/@emotion/babel-plugin#autolabel`
0 commit comments