测试主项目文件夹之外的角度组件:找不到模块raw-loader

问题描述

所以我想运行ng test并将测试包含在项目文件夹旁边的文件夹中。

mainproject/
|-- src/
|   |-- app/
|   |   |-- home/
|   |   |   |-- home.component.spec.ts  
|   |   |-- app-main.component.spec.ts
|   |-- test.ts
|   |-- tsconfig.spec.json
|-- node_modules/
|-- angular.json
|-- package.json
|-- tsconfig.json
components/
|-- common/
    |-- control-bar/
        |-- control-bar.component.ts
        |-- control-bar.component.spec.ts 

当我在组件文件夹中不包含其他测试的情况下运行ng测试时,它可以按预期工作,但是当我尝试包含其他组件时,ng测试失败并显示以下错误

ERROR in ../components/common/control-bar/control-bar.component.ts
Module not found: Error: Can't resolve 'raw-loader' in 'absolutepath\repos\components\common\control-bar'
resolve 'raw-loader' in 'absolutepath\repos\components\common\control-bar'
  Parsed request is a module
  No description file found
  resolve as module
    absolutepath\repos\components\common\control-bar\node_modules doesn't exist or is not a directory
    absolutepath\repos\components\common\node_modules doesn't exist or is not a directory
    absolutepath\repos\components\node_modules doesn't exist or is not a directory
    absolutepath\repos\node_modules doesn't exist or is not a directory
    absolutepath\node_modules doesn't exist or is not a directory
    C:\Users\user\Desktop\node_modules doesn't exist or is not a directory
    C:\Users\user\node_modules doesn't exist or is not a directory
    C:\Users\node_modules doesn't exist or is not a directory
    C:\node_modules doesn't exist or is not a directory
    looking for modules in absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules
      using description file: absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\package.json (relative path: ./node_modules)
        using description file: C:\Users\user\Desktop\folder absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\package.json (relative path: ./node_modules/raw-loader)
          no extension
            absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader doesn't exist
          .js
            absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader.js doesn't exist
          .json
            absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader.json doesn't exist
          as directory
            absolutepath\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader doesn't exist
[C:\Users\user\Desktop\folder\repos\components\common\control-bar\package.json]
[C:\Users\user\Desktop\folder\repos\components\common\control-bar\node_modules]
[C:\Users\user\Desktop\folder\repos\components\common\node_modules]
[C:\Users\user\Desktop\folder\repos\components\node_modules]
[C:\Users\user\Desktop\folder\repos\node_modules]
[C:\Users\user\Desktop\folder\node_modules]
[C:\Users\user\Desktop\node_modules]
[C:\Users\user\node_modules]
[C:\Users\node_modules]
[C:\node_modules]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader.js]
[C:\Users\user\Desktop\folder\repos\mainproject\node_modules\@angular-devkit\build-angular\node_modules\raw-loader.json]
 @ ../components/common/control-bar/control-bar.component.ts 13:34-85
 @ ../components/common/control-bar/control-bar.component.spec.ts
 @ ./src/test.ts

因此原始加载程序似乎丢失或找不到它。要清楚的是,此设置中只有1个node_modules,它位于mainproject文件夹中。

解决方法

我遇到了完全相同的问题,我已经放弃寻找合适的解决方案。现在,我通过确保单独安装库的依赖项来使其工作(因此库中有第二个 node_modules 文件夹)。

由于在该主题上找不到任何内容,并且 official angular documentation for creating libraries 提到需要构建库:

Build the library. You cannot use a library before it is built.

我假设动态链接到没有自己的 node_modules 的未构建库是​​完全不受支持的(即使只要库中没有导入组件类,它就可以正常工作)。