Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

javascript - 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:

(我有一个带有Typescript的节点项目,并且在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:datadevelappssgte-itcoordinacionjuridicoswspjnsrcabel/code-frame.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@(.*)/": "C:datadevelappssgte-itcoordinacionjuridicoswspjnsrc$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...

(还尝试用'$'替换'@',但也有其他错误...)

  ask by opensas translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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不会识别映射为'$'的导入,但是可以使用'@'正常工作...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...