简体   繁体   English

Angular 8 与 Jest 使用路径

[英]Angular 8 with Jest using paths

I am trying to get Jest to work with my current project.我试图让 Jest 与我当前的项目一起工作。 I have followed this guide:我已遵循本指南:

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

When I run ng test all my tests fail with this error:当我运行ng test时,我的所有测试都失败并出现此错误:

Cannot find module '@environments/environment' from 'data.service.ts'无法从“data.service.ts”中找到模块“@environments/environment”

I believe that is because it's using my custom paths defined in tsconfig.json , which looks like this:我相信这是因为它使用了我在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
  }
}

How can I get Jest to use the paths I have defined?如何让 Jest 使用我定义的路径?


Update更新

I have found this document:我找到了这个文件:

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

which seems to be the correct file.这似乎是正确的文件。 So I added a jest.config.js file to my project and did this:所以我在我的项目中添加了一个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"]
    }
};

Which seems to have worked, but I am getting a new error now:这似乎有效,但我现在收到一个新错误:

在此处输入图像描述


Update更新

Getting a bit closer.越来越近了。 My jest.config.js was syntax wrong, i changed it to this:我的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"
    }
};

(I removed the array brackets) but now I am getting this error: (我删除了数组括号)但现在我收到了这个错误:

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
}

which should be a map of这应该是一个 map

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

I have checked and I am getting the same issue for all my mappings我已经检查过了,我的所有映射都遇到了同样的问题


So I have reverted to using this:所以我已经恢复使用这个:

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

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

for the jest.config.js because it is less code and seems better than manually using regex.对于jest.config.js ,因为它的代码更少,而且似乎比手动使用正则表达式更好。 I also found this issue:我也发现了这个问题:

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

which I have added to my tsconfig.spec.json and that has now started passing a lot of tests.我已将其添加到我的tsconfig.spec.json中,现在已经开始通过大量测试。 But now I am getting the error I got above, but with more passing tests:但是现在我得到了上面的错误,但是通过了更多的测试:

在此处输入图像描述

We used the ts-jest npm package and configured jest to use the tsconfig.spec.json (which inherits from tsconfig.json) in package.json like: We used the ts-jest npm package and configured jest to use the tsconfig.spec.json (which inherits from tsconfig.json) in package.json like:

"jest": {
    "globals": {
        "ts-jest": {
            "tsConfig": "<rootDir>/src/tsconfig.spec.json",
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM