.spec 文件中无法识别 Angular 8+ tsconfig 路径别名

Map*_*ion 10 testing typescript jestjs visual-studio-code angular

我正在做一些 Angular 测试,我的规范文件无法识别我的路径,他们在 VS Code 中给了我一个红色波浪形导入警告(并出现在问题中),尽管它们以其他方式工作(测试工作等) .) 我假设这是一个 tsconfig 问题而不是一个 linting 问题,因为它给我的错误是:Cannot find module '@feature/<reference path>.<file type>'.ts(2307)

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

tsconfig.json:

{
    "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"
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "outDir": "../out-tsc/spec"
    }
}
Run Code Online (Sandbox Code Playgroud)

angular.json

...
            "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/**"
                    ]
                }
            }
...
Run Code Online (Sandbox Code Playgroud)

版本:

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
Run Code Online (Sandbox Code Playgroud)

Jas*_*ber 18

我想这个问题与您的filesinclude配置有关。当我包含您相当有限的一组filesinclude模式时,我立即看到了您描述的相同问题,原因很简单,并非我的所有文件都匹配*.d.ts

要查看这是否是导致您出现问题的原因(或者如果您知道您的文件与 不匹配*.d.ts),我建议您首先删除您的filesinclude部分,tsconfig.json然后在 VS Code 中保存并重新启动 TS 服务器。

对于那些不知道,你可以通过打开命令调色板(重启VS代码的TS服务器Ctrl+ Shift+P?+ Shift+ P,或只是F1一个内).ts文件,打字Restart TS和选择命令。

如果那您的解决方案,那么您只需要考虑您在files和 中的实际需求include。该文件应有助于在这一方面。

最大的外卖:你没有需要他们之一:

如果“文件”和“包含”都未指定,编译器默认将所有 TypeScript(.ts、.d.ts 和 .tsx)文件包含在包含目录和子目录中,但使用“排除”属性排除的文件除外。如果 allowJs 设置为 true,则还包括 JS 文件(.js 和 .jsx)。

如果您确实需要这种特殊性,您可能只需要在您的模式中添加*.ts和/或模式来解决问题。*.tsxinclude

我希望这有帮助!


Ako*_*tha 7

jest.config.js就我而言,我忘记根据我的项目设置文件。

tsconfig.ts
...
"@app/*": ["src/app/*"],
...
Run Code Online (Sandbox Code Playgroud)

jest.config.js

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

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