使用路径开玩笑的角度8

问题描述 投票:0回答:1

我试图让Jest与我当前的项目一起工作。我遵循了本指南:

https://blog.angularindepth.com/angular-cli-ng-test-with-jest-in-3-minutes-v2-1060ddd7908d

[当我运行ng test时,所有测试均失败,并显示此错误:

无法从'data.service.ts'找到模块'@ environments / environment']

我相信是因为它使用了tsconfig.json中定义的自定义路径,如下所示:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "paths": {
        "@assets/*": ["src/assets/*"],
        "@core": ["src/app/_core"],
        "@models": ["src/app/_core/models"],
        "@services": ["src/app/_core/services"],
        "@shared": ["src/app/_shared"],
        "@environments/*": ["src/environments/*"]
    }
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

如何让Jest使用我定义的路径?


更新

我找到了此文档:

https://kulshekhar.github.io/ts-jest/user/config/#jest-preset

这似乎是正确的文件。因此,我向项目添加了jest.config.js文件,并执行了以下操作:

module.exports = {
    //verbose: true,
    moduleNameMapper: {
        "^@assets/(.*)$": ["<rootdir>/src/assets/$1"],
        "^@environments/(.*)$": ["<rootdir>/src/environments/$1"],
        "^@core$": ["<rootdir>/src/app/_core"],
        "^@models$": ["<rootdir>/src/app/_core/models"],
        "^@services$": ["<rootdir>/src/app/_core/services"],
        "^@shared$": ["<rootdir>/src/app/_shared"]
    }
};

这似乎可行,但现在出现新错误:

enter image description here


更新

近一点。我的jest.config.js语法错误,我将其更改为:

module.exports = {
    //verbose: true,
    moduleNameMapper: {
        "^@assets/(.*)$": "<rootdir>/src/assets/$1",
        "^@environments/(.*)$": "<rootdir>/src/environments/$1",
        "^@core$": "<rootdir>/src/app/_core",
        "^@models$": "<rootdir>/src/app/_core/models",
        "^@services$": "<rootdir>/src/app/_core/services",
        "^@shared$": "<rootdir>/src/app/_shared"
    }
};

((我卸下了阵列括号),但现在出现此错误:

Configuration error:

Could not locate module @environments/environment mapped as:
<rootdir>/src/environments/environment.

Please check your configuration for these entries:
{
  "moduleNameMapper": {
    "/^@environments\/(.*)$/": "<rootdir>/src/environments/$1"
  },
  "resolver": null
}

应该是]的地图>

"@environments/*": ["src/environments/*"]

我已检查,所有映射都遇到相同的问题


所以我已经恢复使用此:

const {
    pathsToModuleNameMapper
} = require('ts-jest/utils');
const {
    compilerOptions
} = require('./tsconfig');

module.exports = {
    //verbose: true,
    moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
        prefix: '<rootDir>/'
    })
};

对于jest.config.js,因为它更少的代码,而且看起来比手动使用正则表达式更好。我也发现了这个问题:

https://github.com/thymikee/jest-preset-angular/issues/288

我已将其添加到tsconfig.spec.json中,并且现在已经开始通过许多测试。但是现在我遇到了上面的错误,但是通过了更多的测试:

enter image description here

我试图让Jest与我当前的项目一起工作。我遵循了此指南:https://blog.angularindepth.com/angular-cli-ng-test-with-jest-in-3-minutes-v2-1060ddd7908d当我运行ng test时,所有...

angular jestjs
1个回答
0
投票

我们使用ts-jest npm软件包并配置了jest以在package.json中使用tsconfig.spec.json(从tsconfig.json继承),例如:

© www.soinside.com 2019 - 2024. All rights reserved.