简体   繁体   English

Jest 遇到意外的令牌导出

[英]Jest encountered an unexpected token export

Hej, I am trying to set up jest with some library. Hej,我正试图用一些图书馆来开玩笑。 But when I try to test the library I am keep getting the SyntaxError: Unexpected token export error.但是当我尝试测试库时,我不断收到SyntaxError: Unexpected token export错误。 Seems like he is not transpiling the node_modules, but I excluded it in the transformIgnorePatterns settings.似乎他没有转译 node_modules,但我在 transformIgnorePatterns 设置中排除了它。 I really dont know what to do and I am having pain to set it up.我真的不知道该怎么做,我很难设置它。 I am not using React nor Vue or something else.我没有使用 React 或 Vue 或其他东西。

My package.json我的 package.json

{
  "dependencies": {
    "MY-MODULE": "latest"
  },
  "devDependencies": {
    "@babel/core": "^7.3.4",
    "@babel/plugin-transform-modules-commonjs": "^7.2.0",
    "@babel/preset-env": "^7.3.4",
    "babel-jest": "^24.3.1",
    "jest": "^24.3.1"
  },
  "babel": {
    "presets": [
      [ "@babel/preset-env" ]
    ],
    "plugins": [
      "@babel/plugin-transform-modules-commonjs"
    ]
  },
  "jest": {
    "moduleFileExtensions": [ "js", "json" ],
    "transform": {
      "^.+\\.jsx?$": "babel-jest"
    },
    "transformIgnorePatterns": [
      "/node_modules/(?!MY-MODULE)"
    ]
  }
}

My testcase is something like this:我的测试用例是这样的:

import { Test } from '../../src/Test';


describe('Test', () => {

    it('should work', () => {
        const test = new Test();
        expected(test).toBeDefined();
    });

});

and my Test.js is something like this:我的 Test.js 是这样的:

import MyModule from 'MY-MODULE';

export class Test extends MyModule { }

The only way I could get this to work for myself was to add:我可以让它为自己工作的唯一方法是添加:

   {
      "targets": {
        "node": "current"
      }
    }

So that my presets read:所以我的预设是:

"presets": [
  [
    "@babel/preset-env",
    {
      "targets": {
        "node": "current"
      }
    }
  ],

This however appeared to mess with my development settings which included:然而,这似乎扰乱了我的开发设置,其中包括:

    {
      "modules": false
    }

So I included a separate babel 'dev' and 'test' environment:所以我包含了一个单独的 babel 'dev' 和 'test' 环境:

  "babel": {
    "env": {
      "dev": {
        "presets": [
          [
            "@babel/preset-env",
            {
              "modules": false
            }
          ],
          "@babel/preset-react"
        ],
        "plugins": [
          "@babel/plugin-proposal-class-properties"
        ]
      },
      "test": {
        "presets": [
          [
            "@babel/preset-env",
            {
              "targets": {
                "node": "current"
              }
            }
          ],
          "@babel/preset-react"
        ],
        "plugins": [
          "@babel/plugin-proposal-class-properties"
        ]
      }
    }
  },

I set the NODE_ENV in my scripts like this:我在脚本中设置了NODE_ENV ,如下所示:

"test": "cross-env NODE_ENV=test jest",
"dev": "cross-env NODE_ENV=dev concurrently \"webpack --watch\" \"electron .\"",

I am on Windows so I need to include cross-env which I believe is not required for other systems.我在 Windows 上,所以我需要包含我认为其他系统不需要的cross-env

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM