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

Styled-jsx import issue when running test with Jest #1037

Closed
thangngoc89 opened this issue Feb 8, 2017 · 9 comments
Closed

Styled-jsx import issue when running test with Jest #1037

thangngoc89 opened this issue Feb 8, 2017 · 9 comments
Assignees
Labels
please add a complete reproduction The issue lacks information for further investigation

Comments

@thangngoc89
Copy link

I'm not sure where to file an issue for this. I have 3 potential places in mind: jest, styled-jsx and next.js
Since the project that I'm working on is a next.js app, I'll start here.

I have .babelrc file like this

{
  "presets": [
    "next/babel"
  ],
  "plugins": [
    "inline-react-svg"
  ],
  "env": {
    "test": {
      "presets": [
        "latest",
        "react"
      ]
    }
  }
}

next.js version: 2.0.0-beta-19

When I run test with Jest, it throws a syntax error:

/home/khoa/web/thichdoc/src/components/BuildBlocks/Button.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){/* istanbul ignore next */"use strict";Object.defineProperty(exports, "__esModule", { value: true });var _defineProperty2 = require("/home/khoa/web/thichdoc/node_modules/babel-runtime/helpers/defineProperty");var _defineProperty3 = _interopRequireDefault(_defineProperty2);import _JSXStyle from "styled-jsx/style";var _style = require("styled-jsx/style");var _style2 = _interopRequireDefault(_style);var /* istanbul ignore next */_react = require("/home/khoa/web/thichdoc/node_modules/react/react.js"); /* istanbul ignore next */var _react2 = _interopRequireDefault(_react);
                                                                                                                                                                                                                                                                                                           
    SyntaxError: Unexpected token import

The highlighted part is this line import _JSXStyle from "styled-jsx/style

If I remove next/babel preset, jest runs tests just fine.
Since I can't find a way to disable babelrc inheritance, I have to do some weird tricks with my .babelrc file like this:

{
  "plugins": [
    "inline-react-svg"
  ],
  "env": {
    "dev": {
      "presets": [
        "next/babel"
      ]
    },
    "production": {
      "presets": [
        "next/babel"
      ]
    },
    "test": {
      "presets": [
        "react",
        "latest"
      ]
    }
  }
}
@arunoda
Copy link
Contributor

arunoda commented Feb 9, 2017

Use babel-plugin-transform-es2015-modules-commonjs babel plugin as well. We don't transpile import/export statements in our preset. (That's to support webpack's tree shaking)

But it'll have issues in the Node.js. So, you need to use the ^^^ plugin.

@arunoda
Copy link
Contributor

arunoda commented Feb 9, 2017

@thangngoc89 actually, we need to use the es2015 preset. I've updated the Jest example.
See: https://github.com/zeit/next.js/tree/master/examples/with-jest

@arunoda
Copy link
Contributor

arunoda commented Feb 9, 2017

Reopen, if the issue still exists.

@arunoda arunoda closed this as completed Feb 9, 2017
@thangngoc89
Copy link
Author

@arunoda as you can see in my config, I'm using babel-preset-latest, it covers es2015 as well as es2016.

If you looks closely to the error dump, you can see that only the styled-jsx import statement wasn't transpiled by babel, others statements are transpiled properly.

If I remove next/babel, the problem goes away. It's probably because of style-jsx tags aren't transpiled at all I guess.

@thangngoc89
Copy link
Author

By the way, I can't reopen this issue.

@arunoda arunoda reopened this Feb 9, 2017
@arunoda
Copy link
Contributor

arunoda commented Feb 9, 2017

@thangngoc89 I'll try with styled-jsx.

@arunoda arunoda self-assigned this Feb 9, 2017
@arunoda
Copy link
Contributor

arunoda commented Feb 9, 2017

@thangngoc89 I've updated the with-jest example to styled-jsx and it works pretty well.

Could you send us a sample repo?

@arunoda arunoda added the please add a complete reproduction The issue lacks information for further investigation label Feb 9, 2017
@thangngoc89
Copy link
Author

@arunoda You updated the with-jest example with next/babel in each environment just like my workaround so there shouldn't be a problem for me right now. But it looks weird to me.

@TonyHYK
Copy link

TonyHYK commented Jan 24, 2018

@arunoda I'm following the with-jest example which well with vanilla styled-component, however, I'm also using styled-jsx-plugin-sass on top of that, and I have problems configuring it with jest. Here's my current .babelrc

{
  "env": {
    "development": {
      "presets": [
        [
          "next/babel",
          {
            "styled-jsx": {
              "plugins": [
                [
                  "styled-jsx-plugin-sass",
                  {
                    "sassOptions": {
                      "includePaths": ["./client/styles"]
                    }
                  }
                ]
              ]
            }
          }
        ]
      ]
    },
    "production": {
      "presets": [
        [
          "next/babel",
          {
            "styled-jsx": {
              "plugins": [
                [
                  "styled-jsx-plugin-sass",
                  {
                    "sassOptions": {
                      "includePaths": ["./client/styles"]
                    }
                  }
                ]
              ]
            }
          }
        ]
      ]
    },
    "test": {
      "presets": [["env", { "modules": "commonjs" }], "next/babel"]
    }
  },
  "plugins": [["module-resolver", { "root": ["./"] }], "inline-react-svg"]
}

@lock lock bot locked as resolved and limited conversation to collaborators Jan 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
please add a complete reproduction The issue lacks information for further investigation
Projects
None yet
Development

No branches or pull requests

3 participants