首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角8+ tsconfig路径别名在.spec文件中无法识别

角8+ tsconfig路径别名在.spec文件中无法识别
EN

Stack Overflow用户
提问于 2019-09-26 18:31:12
回答 2查看 10K关注 0票数 10

我正在进行一些角度测试,我的规范文件将不会识别我的路径,它们会在VS代码中给我一个红色的输入警告(并出现在问题中),尽管它们以其他方式工作(测试工作,等等)。我假设这是一个tsconfig问题,而不是一个linting问题,因为它给我的错误是:Cannot find module '@feature/<reference path>.<file type>'.ts(2307)

它在功能上对我没有太大影响,但它很烦人(并且杀死了自动导入)。

tsconfig.json:

代码语言:javascript
复制
{
    "compileOnSave": false,
    "compilerOptions": {
        "module": "esnext",
        "outDir": "./dist/out-tsc",
        "strictNullChecks": true,
        "noImplicitAny": false,
        "noImplicitThis": true,
        "alwaysStrict": false,
        "forceConsistentCasingInFileNames": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "declaration": false,
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowJs": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "target": "es2015",
        "types": [
            "node",
            "arcgis-js-api",
            "jest"
        ],
        "typeRoots": [
            "node_modules/@types"
        ],
        "lib": [
            "es2017",
            "dom",
            "esnext.asynciterable"
        ],
        "baseUrl": ".",
        "paths": {
            "core-js/es6/*": [
                "node_modules/core-js/es/*"
            ],
            "core-js/es7/reflect": [
                "node_modules/core-js/proposals/reflect-metadata"
            ],
            "@core/*": [
                "src/app/core/*"
            ],
            "@shared/*": [
                "src/app/shared/*"
            ],
            "@feature/*": [
                "src/app/feature/*"
            ],
            "@test/*": [
                "src/test/*"
            ],
            "@/*": [
                "src/*"
            ]
        }
    },
    "files": [
        "src/main.ts",
        "src/polyfills.ts"
    ],
    "include": [
        "**/*.d.ts"
    ]
}

tsconfig.spec.json和tsconfig.app.json路径(注意来自角-cli的标准文件夹结构):

代码语言:javascript
复制
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/spec"
    }
}

angular.json

代码语言:javascript
复制
...
            "test": {
                "builder": "@angular-builders/jest:run",
                "options": {
                    "tsConfig": "src/tsconfig.spec.json",
                    "no-cache": true,
                    "polyfills": "src/polyfills.ts",
                    "configPath": "./jest.config.js",
                    "styles": [
                        "node_modules/font-awesome/css/font-awesome.css",
                        "src/styles.scss"
                    ],
                    "scripts": [],
                    "assets": [
                        "src/favicon.ico",
                        "src/assets",
                        "src/manifest.json",
                    ],
                    "stylePreprocessorOptions": {
                        "includePaths": [
                            "src"
                        ]
                    }
                }
            },
            "lint": {
                "builder": "@angular-devkit/build-angular:tslint",
                "options": {
                    "tsConfig": [
                        "src/tsconfig.app.json",
                        "src/tsconfig.spec.json"
                    ],
                    "exclude": [
                        "**/node_modules/**"
                    ]
                }
            }
...

版本:

代码语言:javascript
复制
Angular CLI: 8.3.6
Node: 12.3.1
OS: win32 x64
Angular: 8.2.8
... animations, common, compiler, compiler-cli, core, forms     
... language-service, platform-browser, platform-browser-dynamic
... router, service-worker

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.803.6
@angular-devkit/build-angular     0.803.6
@angular-devkit/build-optimizer   0.803.6
@angular-devkit/build-webpack     0.803.6
@angular-devkit/core              8.3.6
@angular-devkit/schematics        8.3.6
@angular/cli                      8.3.6
@angular/pwa                      0.803.6
@ngtools/webpack                  8.3.6
@schematics/angular               8.3.6
@schematics/update                0.803.6
rxjs                              6.5.3
typescript                        3.5.3
webpack                           4.39.2
EN

回答 2

Stack Overflow用户

发布于 2020-05-20 15:07:42

在我的例子中,我忘了根据我的项目设置jest.config.js文件。

代码语言:javascript
复制
tsconfig.ts
...
"@app/*": ["src/app/*"],
...

代码语言:javascript
复制
jest.config.js

module.exports = {
    moduleNameMapper: {
        "@app/(.*)$": "<rootDir>/src/app/$1"
    } 
};

参考资料:https://jestjs.io/docs/en/configuration#modulenamemapper-objectstring-string--arraystring

票数 5
EN

Stack Overflow用户

发布于 2020-07-17 01:37:18

tsconfig.ts

代码语言:javascript
复制
{
  "compilerOptions": {
    ....
    "paths": {
      "@app1/*" : ["src/*"]
    }
  }
}

packages.json

代码语言:javascript
复制
  "jest": {
    ...
    "rootDir": "src",
    "moduleNameMapper": {
      "@app1/(.*)$": "<rootDir>/$1"
    } 
  }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58122806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档