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

Jest serializer new api #1850

Merged
merged 14 commits into from
May 21, 2020
Merged

Jest serializer new api #1850

merged 14 commits into from
May 21, 2020

Conversation

Andarist
Copy link
Member

It has been reported that snapshots show every style to be changed when snapshot & received don't match. After investigating this:

  • it's somewhat a regression in Jest 25 - I've reported it to the jest team
  • we have been ignoring config.indent option which somewhat comes helpful here because we were using the old serializers API

So I've decided to refactor those to use "new" serializers API (it got introduced in Jest 21) to eventually handle config.indent regardless of what happens to the reported ticket in Jest repo. Also, it was impossible to handle it (at least not in a straightforward way) using the old API

In the process, I've removed css dependency (a parser and stringifier) as we already depend on Stylis and we don't need two separate parsers. This has also actually "fixed"/improved printing of nested at rules.

@Andarist Andarist requested a review from emmatown April 23, 2020 21:42
@changeset-bot
Copy link

changeset-bot bot commented Apr 23, 2020

🦋 Changeset is good to go

Latest commit: 16fa4be

We got this.

This PR includes changesets to release 1 package
Name Type
@emotion/jest Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 23, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 16fa4be:

Sandbox Source
Emotion Configuration

@codecov
Copy link

codecov bot commented Apr 23, 2020

Codecov Report

Merging #1850 into stylis-v4 will increase coverage by 0.14%.
The diff coverage is 100.00%.

Impacted Files Coverage Δ
scripts/test-utils/legacy-env.js 100.00% <ø> (ø)
scripts/test-utils/next-env.js 100.00% <ø> (ø)
packages/css-prettifier/src/index.js 100.00% <100.00%> (ø)
packages/jest/src/enzyme.js 100.00% <100.00%> (ø)
packages/jest/src/matchers.js 96.77% <100.00%> (-0.11%) ⬇️
packages/jest/src/serializer.js 100.00% <100.00%> (+2.32%) ⬆️
packages/jest/src/utils.js 96.15% <100.00%> (ø)
packages/server/test/util.js 96.15% <100.00%> (+1.70%) ⬆️
test/pretty-css.js 100.00% <100.00%> (ø)

@@ -0,0 +1,29 @@
{
"name": "@emotion/css-prettifier",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super fond of this name - it seems super close to @emotion/css but yet it doesn't have to do anything directly with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine. For mostly internal packages, I don't think it matters much

import memoize from '@emotion/memoize'
import { compile, serialize, combine, tokenize } from '@emotion/stylis'

// adjusted https://github.com/thysultan/stylis.js/blob/68b9043427c177b95a0fd6a2a13f5b636bf80236/src/Serializer.js#L26-L34
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've started by forking the original stringify because I thought I would only have to change a thing or two... but I had to change a little bit more than that, somewhat to the point that it might not be worth it and maybe it would be better to just implement a specific to this use case serialize, rather than acting like its a Stylis plugin. OTOH - this works now and Im not sure if I want to refactor it.

@@ -427,11 +427,11 @@ exports[`css nested at rule 1`] = `
background-color: blue;
}

@supports (width: 100vw) {
@supports (width: 100vw) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an example of improved nested atrules printing

prettyStyles = css.stringify(css.parse(styles))
} catch (e) {
console.error(e)
throw new Error(`There was an error parsing the following css: "${styles}"`)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stylis doesnt throw on invalid input, it should just swallow it

@Andarist Andarist force-pushed the jest-serializer-new-api branch from 8cd2c67 to 92972d3 Compare April 23, 2020 22:26
@Andarist Andarist force-pushed the jest-serializer-new-api branch from 92972d3 to 89925b8 Compare April 23, 2020 22:34
# Conflicts:
#	packages/css/package.json
#	yarn.lock
Copy link
Member

@emmatown emmatown left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the below comment, LGTM

'@emotion/jest': major
---

`test` & `print` are no longer exported as named exports. If you want to access the default serializer just access the default export.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test & print were exported so you could add @emotion/jest to snapshotSerializers, I think I'd like to keep @emotion/jest working and throw an error when @emotion/jest/serializer is imported that tells people to use @emotion/jest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, isn't it packing too much into a single module? Having a separate entry point for this seems conceptually easier as with test & print being exported we kinda trick Jest to think that it's a serializer - structurally ok-ish but nominally not so much.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, okay, sure. Let's use the seperate module. Could you make the changeset clearer that using the raw package name in the snapshotSerializers option will no longer work and they'll have to use @emotion/jest/serializer instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added this info, could you re-check?

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
@Andarist Andarist merged commit 29ac0e6 into stylis-v4 May 21, 2020
@Andarist Andarist deleted the jest-serializer-new-api branch May 21, 2020 09:54
@Andarist
Copy link
Member Author

I've merged this after applying your suggested changeset changes as this was the only comment. Feel free to comment further in #1817 if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants