最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jest fails to transpile import from npm linked module - Stack Overflow

programmeradmin2浏览0评论

I have a project with multiple modules (using Lerna) and I want to use Jest to run tests. However, when I test code that uses a shared module (npm linked module via Lerna) it seems that Babel is not correctly applied and I get the following error:

SyntaxError: Unexpected token import

The structure of my project is like this:

- my-project
|- shared
|- native
|- web

web and native require the shared module. When I go into the shared directory and run the local tests in Jest everything works fine. If I run Jest tests in the web directory the above error occurs as soon as I include something from shared.

Here is a super simple test that causes the error:

import { util } from 'shared';

it('returns false if not prod', () => {
    expect(util.isProd()).toBe(false);
});

My .babelrc looks like this:

{
    "presets": [
        "env",
        "flow",
        "react"
        ],
    "plugins": [
        "flow-react-proptypes",
        "transform-object-rest-spread",
        "transform-class-properties"
    ]
}

I tried everything I could find, including:

  • Different Babel configs, including one with the es2015 preset and enabling modules for the test environment
  • Manually setting the transform option for babel-jest
  • As mentioned, Jest can be executed in the shared module, thus, Jest and babel-jest are installed there as well.

I have a project with multiple modules (using Lerna) and I want to use Jest to run tests. However, when I test code that uses a shared module (npm linked module via Lerna) it seems that Babel is not correctly applied and I get the following error:

SyntaxError: Unexpected token import

The structure of my project is like this:

- my-project
|- shared
|- native
|- web

web and native require the shared module. When I go into the shared directory and run the local tests in Jest everything works fine. If I run Jest tests in the web directory the above error occurs as soon as I include something from shared.

Here is a super simple test that causes the error:

import { util } from 'shared';

it('returns false if not prod', () => {
    expect(util.isProd()).toBe(false);
});

My .babelrc looks like this:

{
    "presets": [
        "env",
        "flow",
        "react"
        ],
    "plugins": [
        "flow-react-proptypes",
        "transform-object-rest-spread",
        "transform-class-properties"
    ]
}

I tried everything I could find, including:

  • Different Babel configs, including one with the es2015 preset and enabling modules for the test environment
  • Manually setting the transform option for babel-jest
  • As mentioned, Jest can be executed in the shared module, thus, Jest and babel-jest are installed there as well.
Share Improve this question asked Jun 7, 2017 at 13:12 Florian EckerstorferFlorian Eckerstorfer 1,5261 gold badge14 silver badges21 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

I had to change the transformIgnorePatterns configuration option of Jest in the package.json of the web module.

{
    "jest": {
        "transformIgnorePatterns": [
            "<rootDir>/node_modules/(?!shared|another)"
        ]
    },
}
发布评论

评论列表(0)

  1. 暂无评论