Jest / Mocha + TypeScript:Jest / Mocha尝试读取JS依赖项并失败-“意外的令牌导入”

时间:2018-09-26 19:21:19

标签: typescript unit-testing mocha jestjs

因此,我正在用Mocha测试TypeScript文件,我通过import访问文件中的依赖项,并且要测试的功能在同一文件中,如下所示:

import { Foo } from 'foo-library';

// I'm trying to test this
export const myHelperFunction = (a, b) => {
  return a + b;
  // Foo not used
};

export class BigClass {
  public doStuff() {
    // uses Foo
  }
}

然后我有我的测试文件:

import { myHelperFunction } from './my-file';

it('does the right thing', () => {
    expect(myHelperFunction(2, 3)).to.equal(5);
});

现在,Mocha在尝试执行测试时尝试解析foo-library的内容,并且即使在myHelperFunction中未使用导入,也会引发一个错误,指出“意外的令牌导入”。该文件为ES6格式,并且Mocha / Node无法正确解析。

是否应将依赖文件以某种方式移植到ES5?在测试期间是否可以完全跳过导入?我试图用几个库(Sinon等)来模拟导入,但是也没有用。

我非常感谢任何想法。

2 个答案:

答案 0 :(得分:1)

要回答原始问题,请尝试mocha -r ts-node/register src/**/test.ts。 Mocha不会立即运行TS文件。您必须使用ts-node/register将它们作为TS文件运行。

答案 1 :(得分:0)

问题解决了,我换了玩笑。

实际上,我也有一些问题。问题在于未转译node_modules中的JavaScript文件。工作配置如下。

package.json

{
  "name": "myApp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "http://ionicframework.com/",
  "private": true,
  "scripts": {
    "start": "ionic-app-scripts serve",
    "clean": "ionic-app-scripts clean",
    "build": "ionic-app-scripts build",
    "lint": "ionic-app-scripts lint",
    "test": "jest"
  },
  "dependencies": {
    "@angular/animations": "5.2.11",
    "@angular/common": "5.2.11",
    "@angular/compiler": "5.2.11",
    "@angular/compiler-cli": "5.2.11",
    "@angular/core": "5.2.11",
    "@angular/forms": "5.2.11",
    "@angular/http": "5.2.11",
    "@angular/platform-browser": "5.2.11",
    "@angular/platform-browser-dynamic": "5.2.11",
    "@ionic-native/core": "~4.12.0",
    "@ionic-native/splash-screen": "~4.12.0",
    "@ionic-native/sqlite": "^4.15.0",
    "@ionic-native/status-bar": "~4.12.0",
    "@ionic/storage": "2.2.0",
    "cordova-sqlite-storage": "^2.4.0",
    "ionic-angular": "3.9.2",
    "ionicons": "3.0.0",
    "moment": "^2.22.2",
    "rxjs": "5.5.11",
    "sw-toolbox": "3.6.0",
    "zone.js": "0.8.26"
  },
  "devDependencies": {
    "@babel/core": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@ionic/app-scripts": "3.2.0",
    "@types/jest": "^23.3.2",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^23.6.0",
    "jest": "^23.6.0",
    "regenerator-runtime": "^0.12.1",
    "ts-jest": "^23.10.2",
    "typescript": "~2.6.2"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-sqlite-storage": {}
    }
  }
}

babel.config.js

'use strict';

module.exports = {
    presets: ['@babel/preset-env'],
}

jest.config.js

module.exports = {
  "roots": [
    "<rootDir>/src"
  ],
  "transform": {
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.js?$": "babel-jest" // had to add this
  },
  "testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js",
    "jsx",
    "json",
    "node"
  ],
  "moduleDirectories": [
    "node_modules",
    "src"
  ],
  "transformIgnorePatterns": [
    "node_modules/(?!(@ionic-native)/)" // had to add this
  ]
}