Node.js jest无法解析模块别名

Node.js jest无法解析模块别名,node.js,typescript,unit-testing,jestjs,alias,Node.js,Typescript,Unit Testing,Jestjs,Alias,我正在使用一个名为modulealias的npm模块。我在tsconfig.json和package.json中映射了一些模块 ... "_moduleAliases": { "@config": "src/config", "@interface": "src/interface", "@services": "src/services" }, "jest": { "moduleNameMapper": { "@config/(.*)"

我正在使用一个名为modulealias的npm模块。我在tsconfig.json和package.json中映射了一些模块

  ...
 "_moduleAliases": {
    "@config": "src/config",
    "@interface": "src/interface",
    "@services": "src/services"
  },
  "jest": {
    "moduleNameMapper": {
      "@config/(.*)": "src/config/$1",
      "@interface/(.*)": "src/interface/$1",
      "@services/(.*)": "src/services/$1"
    },

     "moduleFileExtensions": ['js', 'json', 'jsx', 'ts', 'tsx', 'node']
  },
...

tsconfig.json

"compilerOptions": {
   "baseUrl": "./src",
   "paths": {
   "@config/*": ["config/*"],
   "@interfaces/*": ["interfaces/*"],
   "@services/*": ["services/*"]
  },
  "module": "commonjs",
  "target": "es2015",                   /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
  "sourceMap": true,
  "outDir": "./dist",                        /* Redirect output structure to the directory. */
  "rootDir": "./src",    
  "allowSyntheticDefaultImports": true          /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
}
package.json

  ...
 "_moduleAliases": {
    "@config": "src/config",
    "@interface": "src/interface",
    "@services": "src/services"
  },
  "jest": {
    "moduleNameMapper": {
      "@config/(.*)": "src/config/$1",
      "@interface/(.*)": "src/interface/$1",
      "@services/(.*)": "src/services/$1"
    },

     "moduleFileExtensions": ['js', 'json', 'jsx', 'ts', 'tsx', 'node']
  },
...

server.ts

import { logger } from '@config/logger';
当我运行
npm start
时,一切正常,但当我运行jest时,它会给我一个错误

FAIL  src/test/article.spec.ts
  ● Test suite failed to run

    Cannot find module '@config/logger' from 'server.ts'

    However, Jest was able to find:
        'rest/server.ts'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['js', 'json', 'jsx', 'ts', 'tsx', 'node'].
有人知道问题出在哪里吗?谢谢

解决方案适合我(2019年10月18日更新):

创建一个jest.config.js,代码如下:

module.exports = {
  "roots": [
    "<rootDir>/src/"
],
  "transform": {
    "^.+\\.tsx?$": "ts-jest"
  }

}
module.exports={
“根”:[
“/src/”
],
“转变”:{
“^.+\.tsx?$”:“ts笑话”
}
}
并更新package.json中的moduleNameMapper:

...
  "_moduleAliases": {
    "@config": "./src/config",
    "@interfaces": "./src/interfaces",
    "@services": "./src/services"
  },
  "jest": {

    "moduleNameMapper": {
      "@config/(.*)": "<rootDir>/src/config/$1",
      "@interfaces/(.*)": "<rootDir>/src/interfaces/$1",
      "@services/(.*)": "<rootDir>/src/services/$1"
    }
  }
...
import { IApiResponse } from '@intf/IApi'
。。。
“_moduleAlias”:{
“@config”:“/src/config”,
“@interfaces”:“/src/interfaces”,
“@services”:“/src/services”
},
“笑话”:{
“moduleNameMapper”:{
“@config/(.*)”:“/src/config/$1”,
“@interfaces/(.*):”/src/interfaces/$1“,
@services/(.*):“/src/services/$1”
}
}
...

我认为您的
tsconfig
中没有正确配置路由,因为路径缺少
src
文件夹(它们出现在package.json模块别名配置中):

您的
tsconfig
代码:

"@config/*": ["config/*"],
"@interfaces/*": ["interfaces/*"],
"@services/*": ["services/*"]
我认为应该是:

"@config/*": ["src/config/*"],
"@interfaces/*": ["src/interfaces/*"],
"@services/*": ["src/services/*"]

几个小时后,我终于成功了。我会尽我所能简化它,以节省别人的时间,使它顺利

我的应用程序具有以下堆栈:

  • API构建(
    .ts
    )的类型脚本(
    tsc
    ts节点/寄存器
    ,无Babel)
  • 反应(
    .tsx
    使用网页包)
  • jest
    用于API测试(无巴别塔)
  • testcafe
    用于UI/E2E
  • VSCode作为IDE
关键注意事项:

...
  "_moduleAliases": {
    "@config": "./src/config",
    "@interfaces": "./src/interfaces",
    "@services": "./src/services"
  },
  "jest": {

    "moduleNameMapper": {
      "@config/(.*)": "<rootDir>/src/config/$1",
      "@interfaces/(.*)": "<rootDir>/src/interfaces/$1",
      "@services/(.*)": "<rootDir>/src/services/$1"
    }
  }
...
import { IApiResponse } from '@intf/IApi'
  • 适合我的解决方案是别名前面必须有“@”字符。理论上,
    模块别名
    不需要它,但是当我们应用模块名称映射器时,Jest会丢失
  • “webpack”别名和“tsconfig”之间的命名需要一致。VSCode不必在TSX文件中的模块名称下加红色下划线
  • 无论您的文档结构如何,这都应该起作用,但如果遇到问题,请记住调整
    baseUrl
    和jest配置
  • 在为
    .tsx
    文件应用VSCode更改时,不要担心某些路径带有下划线。这是暂时的,因为VSCode似乎只有在所有文件都正确地相互连接时才能理解它。一开始就让我失去了动力
  • 首先,使用从安装模块别名

    然后添加到初始启动文件(仅适用于
    .ts
    文件,即应用程序服务器):

    模块别名
    文档所示。然后设置
    tsconfig.ts
    。请记住,
    baseUrl
    也与此相关:

    {
     "compilerOptions": {
        "baseUrl": ".",
        ...
        "paths": {
          "@helpers/*": ["src/helpers/*"],
          "@root/*": ["src/*"]
          ...
        }
     }
    
    然后设置
    webpack.js

    const path = require('path')
    ...
    {
      resolve: {
        ...
        alias: {
          '@helpers': path.resolve(__dirname, 'src', 'helpers'),
          '@root': path.resolve(__dirname, 'src')
        }
      }
    }
    
    然后设置您的
    包.json

    {
       ...
       "_moduleAliases": {
         "@helpers": "src/helpers",
         "@root": "src"
       }
    }
    
    然后设置您的
    jest
    config(我只附加应用更改时相关的内容):

    现在它变成了

    tsc && tscpaths -p tsconfig.json -s ./src -o ./dist/server
    
    您需要根据您的结构调整
    -s
    -o
    ,但在生成后检查
    .js
    文件时,您应该查看相对路径是否正确链接(并进行相应的调试)

    就这样。它应该是王牌。虽然很多,但值得

    控制器(.ts)和反应组件(.tsx)文件中的调用示例:

    ...
      "_moduleAliases": {
        "@config": "./src/config",
        "@interfaces": "./src/interfaces",
        "@services": "./src/services"
      },
      "jest": {
    
        "moduleNameMapper": {
          "@config/(.*)": "<rootDir>/src/config/$1",
          "@interfaces/(.*)": "<rootDir>/src/interfaces/$1",
          "@services/(.*)": "<rootDir>/src/services/$1"
        }
      }
    ...
    
    import { IApiResponse } from '@intf/IApi'
    

    compilerOptions中的baseUrl似乎可以通知编译器在何处查找模块,因为在我将“src”添加到路径中后,它给了我typescript错误。我甚至无法启动我的应用程序,它也给了我找不到模块的“@config/…”哇,这让我很困惑。我有一个使用ts和jest的工作配置,我正在指定指向路径的完整路径,指示
    src
    段。我看到在您的package.json中,您指的是“接口”,但在您的tsconfig中,您指的是“接口”(带有额外的
    s
    )。也许这是相关的,我不确定。我创建了一个jest.config.js并添加了两个选项,包括“根”和“转换”,然后一切都正常了。你应该从你的问题中删除你的解决方案,然后添加你自己的答案。