简体   繁体   English

ts-jest:将“@”字符映射到 /src 文件夹

[英]ts-jest: map '@' char to /src folder

I have a node project with typescript, and I have the following paths configured in my tsconfig.json file:我有一个带有打字稿的节点项目,并且在我的 tsconfig.json 文件中配置了以下路径:

    "paths": {                                /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      "@project/*": ["./../*"],
      "@*": ["./*"],
    },

My config files are at /project folder, and my sources at /project/src我的配置文件位于 /project 文件夹中,而我的源位于 /project/src

It works ok, and it correctly maps @xxx/yyy to src/xxx/yyy and @project/package.json to /project/package.json它工作正常,它正确地将 @xxx/yyy 映射到 src/xxx/yyy 和 @project/package.json 到 /project/package.json

I'm trying to achieve the same with ts-jest, I tried with the folowing moduleNameMapper in my jest.config.js file:我正在尝试使用 ts-jest 实现相同的目标,我尝试在 jest.config.js 文件中使用以下 moduleNameMapper:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src/'],
  modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
  testMatch: ['**/*.spec.ts'],
  moduleNameMapper: {
    "^\@(.*)": "<rootDir>/src/$1",
  }
}

But I get the following error:但我收到以下错误:

 FAIL  src/lib/error/BaseError.spec.ts
  ● Test suite failed to run

    Configuration error:

    Could not locate module @babel/code-frame mapped as:
    C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\babel/code-frame.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@(.*)/": "C:\data\devel\apps\sgte-it\coordinacion\juridicos\wspjn\src\$1"
      },
      "resolver": null
    }

      at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:501:17)

On the other hand, if I map every folder at /src like this:另一方面,如果我像这样映射 /src 中的每个文件夹:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src/'],
  modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
  testMatch: ['**/*.spec.ts'],
  moduleNameMapper: {
    "@db/(.*)": "<rootDir>/src/db/$1",
    "@lib/(.*)": "<rootDir>/src/lib/$1",
    "@modules/(.*)": "<rootDir>/src/modules/$1",
    "@services/(.*)": "<rootDir>/src/services/$1"
  }
}

It works ok, but I have to update it with every new root folder I add.它工作正常,但我必须用我添加的每个新根文件夹更新它。

Is there any standard, recommended way to achieve such a thing?有没有标准的、推荐的方法来实现这样的事情? I want to have a special character (@ in this case) pointing to my /src/ folder.我希望有一个特殊字符(在本例中为 @)指向我的 /src/ 文件夹。

Also tried replacing '@' with '$' but had other errors too...还尝试用 '$' 替换 '@' 但也有其他错误......

I made it work with the following config.我使用以下配置使其工作。

    moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
        "^@components/(.*)$": "<rootDir>/src/components/$1",
    },

This article was helpful.这篇文章很有帮助。

In my case I wanted map @ to ./src and @components to ./src/components在我的情况下,我想将@映射到./src@components./src/components

I could solve it, but not with the @ sign, I had to use $ , more over, I had to escape it and enclose it in square brackets, like this:我可以解决它,但不能使用@符号,我必须使用$ ,而且,我必须将它转义并将其括在方括号中,如下所示:

tsconfig.json: tsconfig.json:

    "paths": {                                /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
      "$project/*": ["./../*"],
      "$*": ["./*"],
    },

jest.config.js: jest.config.js:

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  roots: ['<rootDir>/src/'],
  modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/build/'],
  testMatch: ['**/*.spec.ts'],
  moduleNameMapper: {
    "^[\$]project/(.*)": "<rootDir>/$1",
    "^[\$](.*)": "<rootDir>/src/$1",
  }
}

I hope it might be useful for other people, configuring all these little details is a real pain...我希望它对其他人有用,配置所有这些小细节是一个真正的痛苦......


Damn, I almost had it all working (ttsc and ts-jest works fine), but vscode wont recognize imports mapped with '$', work ok with '@' though...该死的,我几乎可以正常工作(ttsc 和 ts-jest 工作正常),但是 vscode 无法识别用 '$' 映射的导入,尽管使用 '@' 可以正常工作......

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

相关问题 带有 TypeOrm 实体的 ts-jest 中的单元测试 - Unit Testing in ts-jest with TypeOrm entities 测试是否使用 Jest 和 Typescript 和 ts-jest 调用 function? - Testing to see if a function is called with Jest and Typescript, and ts-jest? 通过 jest (ts-jest) 使用名称空间测试 typescript 代码 - Testing typescript code with namespaces by jest (ts-jest) ts-jest无法在导入的ES6模块上运行测试 - ts-jest cannot run testing on imported ES6 modules TypeScript ts-jest预处理器的目的是什么? - What's the purpose of TypeScript ts-jest pre-processor? Typescript - 我无法使用 jest、@types/jest 和 ts-jest 编译项目 - Typescript - I can't compile project with jest, @types/jest and ts-jest 当我使用jest(ts-jest异步/等待测试)时,WebStorm提示我TS错误(TS2705) - WebStorm hints me the TS error (TS2705) when I use jest(ts-jest async/await test) 如何使 ts-jest 使用在我的导入中导入的 js 文件的导入/导出语法? - How to make ts-jest work with import/export syntax of the js files that are being imported in my imports? ts-jest 无法运行 tsx 测试文件,因为从模块的 js 文件“导入” - ts-jest fails to run a tsx test file because of “Import” from a module's js file Jest 无法从“src/tests/X.test.ts”中找到打字稿模块“../X.js” - Jest cannot find typescript module '../X.js' from 'src/tests/X.test.ts'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM