Skip to content

Invalid CSS causes test failure with cryptic error message #147

Description

@BernieSumption

I've solved this for myself by fixing the invalid CSS, but am filing an issue as the process of figuring out what was wrong took a while. The issue is that invalid CSS produces a runtime error as jest-styled-components tries to parse the CSS.

I'm not sure what is more appropriate behaviour in the event of invalid CSS, presumably you need to parse the CSS to be able to insert the right snippet into the snapshot. I guess the options are to fail with a more useful error message directing people to fix their CSS and printing out the invalid CSS, or put something like /* Error: could not parse CSS */ into the snapshot itself. Personally I'd go for the first.

Stack trace:

undefined:26:225: missing '{'

      19 |   it("renders correctly", () => {
      20 |     const component = mount(<MyComponent />);
    > 21 |     expect(component).toMatchSnapshot();
         |                       ^
      22 |   });
      23 |
      24 |   it("renders without error", () => {

      at error (node_modules/css/lib/parse/index.js:62:15)
      at declarations (node_modules/css/lib/parse/index.js:248:25)
      at rule (node_modules/css/lib/parse/index.js:561:21)
      at rules (node_modules/css/lib/parse/index.js:118:70)
      at stylesheet (node_modules/css/lib/parse/index.js:81:21)
      at Object.<anonymous>.module.exports [as parse] (node_modules/css/lib/parse/index.js:565:20)
      at getCSS (node_modules/jest-styled-components/src/utils.js:51:14)
      at getStyle (node_modules/jest-styled-components/src/styleSheetSerializer.js:62:15)
      at Object.print (node_modules/jest-styled-components/src/styleSheetSerializer.js:104:19)
      at printPlugin (node_modules/pretty-format/build/index.js:287:16)
      at printer (node_modules/pretty-format/build/index.js:332:12)
      at plugin.serialize.plugin.print.valChild (node_modules/pretty-format/build/index.js:289:23)
      at Object.print (node_modules/enzyme-to-json/createSerializer.js:22:14)
      at printPlugin (node_modules/pretty-format/build/index.js:287:16)
      at prettyFormat (node_modules/pretty-format/build/index.js:485:16)
      at Object.throwingMatcher (node_modules/expect/build/index.js:322:33)
      at Object.<anonymous> (src/components/screens/click-and-collect/__tests__/collection-point.test.tsx:21:23)

Invalid CSS in question:

... skipping 22 valid lines
/* sc-component-id: Distance-hPorFt */
.Distance-hPorFt {} .hYOTfE{height:17px;line-height:17px;background:url(process:process(src,filename) { return `module.exports = ${JSON.stringify(path.basename(filename))};`; };) no-repeat 0 0 / 32px 17px;padding-left:52px;}

(As you can see, my CSS was invalid because I was accidentally interpolating code into it, rather than calling a function and interpolating the return value :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions