【问题标题】:Using jest to test an Angular Library?使用 jest 测试 Angular 库?
【发布时间】:2020-06-14 22:03:15
【问题描述】:

尝试使用 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"
  }
}


但是看起来 jest 没有通过 ts-jest 转换,因为我遇到了这样的错误:

    SyntaxError: Unexpected token export

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

【问题讨论】:

    标签: javascript angular typescript jestjs angular-library


    【解决方案1】:

    想通了。我正在使用 Angular 包格式将 ValidatorJS 转换为 Typescript 项目,但我忘记将实用程序上的文件扩展名从 .js 更改为 .ts,因此 Jest 抱怨导入语句。

    如果其他人想设置他们的项目来做这种事情,我在这里写了几篇文章:

    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

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 1970-01-01
      • 2020-05-20
      • 2022-08-23
      • 2018-11-24
      • 2020-06-14
      • 2020-06-01
      • 2021-09-20
      • 2022-06-10
      相关资源
      最近更新 更多