question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected Token Import Running Jest with Storyshots, Docs, and MDX

See original GitHub issue

Describe the bug When running jest with .stories.mdx files in storybook@5.3 and @storybook/addon-storyshots, returns error

.../storybook-docs/node_modules/react-syntax-highlighter/dist/esm/languages/prism/jsx.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import jsx from "refractor/lang/jsx.js";
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (node_modules/@storybook/components/dist/syntaxhighlighter/syntaxhighlighter.js:54:35)

I’ve looked through many other open issues that are related, but haven’t found anyone reporting this specific error (and other’s solutions have not resolved it for me).

To Reproduce Steps to reproduce the behavior: I’ve created a minimum reproducible example here with barebones configs to isolate the issue: https://github.com/davidgolden/storybook-mdx-issue

To reproduce:

  1. git clone https://github.com/davidgolden/storybook-mdx-issue.git storybook-mdx-issue
  2. cd storybook-mdx-issue
  3. npm ci
  4. npm run test

Expected behavior npm run test to execute without error

System:

Environment Info:

  System:
    OS: macOS Mojave 10.14.6
    CPU: (12) x64 Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
  Binaries:
    Node: 9.4.0 - ~/.nvm/versions/node/v9.4.0/bin/node
    npm: 6.9.0 - ~/.nvm/versions/node/v9.4.0/bin/npm
  Browsers:
    Chrome: 79.0.3945.117
    Firefox: 72.0.1
    Safari: 13.0.4
  npmPackages:
    @storybook/addon-docs: ^5.3.3 => 5.3.3 
    @storybook/addon-storyshots: ^5.3.3 => 5.3.3 
    @storybook/react: ^5.3.3 => 5.3.3

Relevant Code Snippets main.js:

module.exports = {
    stories: ["../**/*.stories.(jsx|mdx)"],
    addons: [
        '@storybook/addon-docs',
    ],
};

babel.config.js:

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

jest.config.js:

module.exports = {
    "transform": {
        '^.+\\.jsx?$': 'babel-jest',
        "^.+\\.mdx$": "@storybook/addon-docs/jest-transform-mdx",
    },
};

Storyshots.test.js:

import initStoryshots from '@storybook/addon-storyshots';

initStoryshots();

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mAAdhaTTahcommented, Jan 19, 2020

Looking at the PR, if you’re running into this issue, you can use this in moduleNameMapper: 'react-syntax-highlighter/dist/esm/(.*)': 'react-syntax-highlighter/dist/cjs/$1', which will remap the esm requires to cjs ones. This got me around the issue until the PR is merged and released.

2reactions
roadevcommented, Feb 25, 2020

I’m having the same issue, even using the lastest version (alpha 19)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected token then used @storybook/addon-storyshots ...
But then i run test with jest i have one error. ... import initStoryshots from '@storybook/addon-storyshots'; initStoryshots();.
Read more >
initStoryShots, jest can't parse sample mdx file #14602
console.error Trace: Jest encountered an unexpected token This usually means that you are trying to import a file which Jest cannot parse, e.g....
Read more >
storybook/addon-docs - npm
Start using @storybook/addon-docs in your project by running `npm i ... to configure Jest to transform MDX stories into something Storyshots ...
Read more >
Unexpected Token Then Used @Storybook/Addon-Storyshots
I am loading a third party library in my project and when running Jest tests I get the SyntaxError: Unexpected token export >...
Read more >
hypnosphi/addon-storyshots - Storybook
StoryShots is a Jest Snapshot Testing Addon for Storybook. ... Now, inside of your Storybook config file, import the macro and run it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found