使用玩笑来测试Angular库吗?

时间:2020-03-02 15:13:43

标签: javascript angular typescript jestjs angular-library

尝试使用Jest测试Angular 9库,我已经在本地库package.json中安装了Jest和Typescript的依赖项,如下所示:

  "devDependencies": {
    "@types/jest": "^25.1.3",
    "jest": "^25.1.0",
    "ts-jest": "^25.2.1"
  },

还要像这样设置jest.config.js

module.exports = {
  "roots": [
    "<rootDir>/src/"
  ],
  testMatch: [
    "**/__tests__/**/*.+(ts|tsx|js)",
    "**/?(*.)+(spec|test).+(ts|tsx|js)"
  ],
  "transform": {
    "^.+\\.(ts|tsx)$": "ts-jest"
  }
}


但是当我遇到这样的错误时,看起来好像玩笑没有通过ts-jest转换一样

    SyntaxError: Unexpected token export

    > 1 | import assertString from './util/assertString';
        | ^
      2 | import toString from './util/toString';```

1 个答案:

答案 0 :(得分:0)

弄清楚了。我正在使用Angular包格式将ValidatorJS转换为Typescript项目,却忘记将实用程序上的文件扩展名从.js更改为.ts,因此Jest在抱怨import语句。 / p>

万一其他人想建立他们的项目来做这种事情,我在这里写了几篇文章:

https://medium.com/@ole.ersoy/unit-testing-angular-with-jest-b65888ff33f6

https://medium.com/@ole.ersoy/unit-testing-your-angular-library-project-with-jest-42429a8716eb