【问题标题】:Testing angular components outside of main project folder: module not found raw-loader在主项目文件夹之外测试角度组件:找不到模块 raw-loader
【发布时间】:2020-09-11 06:54:51
【问题描述】:

所以我想运行 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 test 时,它按预期工作,但是当我尝试包含其他组件时,ng test 失败并出现以下错误。

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

所以 raw-loader 似乎丢失或找不到它。需要明确的是,此设置中只有 1 个 node_modules,它位于 mainproject 文件夹中。

【问题讨论】:

标签: javascript angular unit-testing karma-jasmine karma-runner


【解决方案1】:

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

由于没有关于该主题的内容,并且official angular documentation for creating libraries 提到构建库是必需的:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-06
    • 2017-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-08-18
    相关资源
    最近更新 更多