簡體   English   中英

Jest 遇到意外的令牌導出

[英]Jest encountered an unexpected token export

Hej,我正試圖用一些圖書館來開玩笑。 但是當我嘗試測試庫時,我不斷收到SyntaxError: Unexpected token export錯誤。 似乎他沒有轉譯 node_modules,但我在 transformIgnorePatterns 設置中排除了它。 我真的不知道該怎么做,我很難設置它。 我沒有使用 React 或 Vue 或其他東西。

我的 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)"
    ]
  }
}

我的測試用例是這樣的:

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


describe('Test', () => {

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

});

我的 Test.js 是這樣的:

import MyModule from 'MY-MODULE';

export class Test extends MyModule { }

我可以讓它為自己工作的唯一方法是添加:

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

所以我的預設是:

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

然而,這似乎擾亂了我的開發設置,其中包括:

    {
      "modules": false
    }

所以我包含了一個單獨的 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"
        ]
      }
    }
  },

我在腳本中設置了NODE_ENV ,如下所示:

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

我在 Windows 上,所以我需要包含我認為其他系統不需要的cross-env

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM