简体   繁体   English

模块<rootDir>未找到转换选项中的 /node_modules/jest-preset-angular/preprocessor.js

[英]Module <rootDir>/node_modules/jest-preset-angular/preprocessor.js in the transform option was not found

When switching from karma-jasmine to jest for my Angular 7 application, I am getting the following error :当我的 Angular 7 应用程序从 karma-jasmine 切换到 jest 时,我收到以下错误:

Validation Error:

  Module <rootDir>/node_modules/jest-preset-angular/preprocessor.js in the transform option was not found.
         <rootDir> is: C:\newAdminJestTest\jest-angular-example

  Configuration Documentation:
  https://jestjs.io/docs/configuration.html

I have been following this guide for the switch : https://blog.skarby.info/jest-with-angular/我一直在遵循本指南进行切换: https : //blog.skarby.info/jest-with-angular/

The test cases seem to run fine if I remove the transform attribute from the jest object in package.json .如果我从package.json的 jest 对象中删除 transform 属性,测试用例似乎运行良好。 However, I get the above error when running with the transform attribute.但是,使用转换属性运行时出现上述错误。

Here is my package.json :这是我的package.json

{
  "name": "jest-angular-example",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:ci": "jest -ci --runInBand",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~7.2.0",
    "@angular/common": "~7.2.0",
    "@angular/compiler": "~7.2.0",
    "@angular/core": "~7.2.0",
    "@angular/forms": "~7.2.0",
    "@angular/platform-browser": "~7.2.0",
    "@angular/platform-browser-dynamic": "~7.2.0",
    "@angular/router": "~7.2.0",
    "@types/jest": "^24.0.13",
    "core-js": "^2.5.4",
    "rxjs": "~6.3.3",
    "tslib": "^1.9.0",
    "zone.js": "~0.8.26"
  },
  "devDependencies": {
    "@angular-builders/jest": "^8.0.1",
    "@angular-devkit/build-angular": "~0.13.0",
    "@angular/cli": "~7.3.7",
    "@angular/compiler-cli": "~7.2.0",
    "@angular/language-service": "~7.2.0",
    "@types/jsdom": "^12.2.3",
    "@types/node": "~8.9.4",
    "babel-preset-env": "^1.7.0",
    "codelyzer": "~4.5.0",
    "jest": "^24.8.0",
    "jest-createspyobj": "^1.2.2",
    "jest-preset-angular": "^7.1.1",
    "protractor": "~5.4.0",
    "ts-node": "~7.0.0",
    "tslint": "~5.11.0",
    "typescript": "~3.2.2"
  },
  "jest": {
    "preset": "jest-preset-angular",
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupJest.ts"
    ],
    "transform": {
      "^.+\\.(ts|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js",
      "^.+\\.js$": "babel-jest"
    }
  }
}

This is my setupJest.ts :这是我的setupJest.ts

import 'jest-preset-angular';
import './jestGlobalMocks.ts';

This is the jestGlobalMocks.ts :这是jestGlobalMocks.ts

global['CSS'] = null;

const mock = () => {
  let storage = {};
  return {
    getItem: key => key in storage ? storage[key] : null,
    setItem: (key, value) => storage[key] = value || '',
    removeItem: key => delete storage[key],
    clear: () => storage = {},
  };
};

Object.defineProperty(window, 'localStorage', {value: mock()});
Object.defineProperty(window, 'sessionStorage', {value: mock()});
Object.defineProperty(document, 'doctype', {
  value: '<!DOCTYPE html>'
});
Object.defineProperty(window, 'getComputedStyle', {
  value: () => {
    return {
      display: 'none',
      appearance: ['-webkit-appearance']
    };
  }
});
/**
 * ISSUE: https://github.com/angular/material2/issues/7101
 * Workaround for JSDOM missing transform property
 */
Object.defineProperty(document.body.style, 'transform', {
  value: () => {
    return {
      enumerable: true,
      configurable: true,
    };
  },
});

My tsconfig.spec.json :我的tsconfig.spec.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "module": "commonjs",
    "types": [
      "node",
      "jest",
      "jsdom"
    ]
  },
  "files": [
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}

I am expecting to see the following result :我期待看到以下结果:

PS C:\newAdminJestTest\jest-example-master> npm run test

> jest-example@0.0.0 test C:\newAdminJestTest\jest-example-master
> jest

 PASS  src/app/store/todolist.effects.spec.ts (16.555s)
 PASS  src/app/app.component.spec.ts (16.836s)
 PASS  src/app/store/todolist.reducer.spec.ts
 PASS  src/app/item-list/item/item.component.spec.ts (18.045s)
 PASS  src/app/store/todolist.actions.spec.ts
 PASS  src/app/item-list/item-list.component.spec.ts
 PASS  src/app/pie-chart/pie-chart.component.spec.ts
 PASS  src/app/checkbox/checkbox.component.spec.ts
 PASS  src/app/local-storage.service.spec.ts
 PASS  src/app/window.service.spec.ts

Test Suites: 10 passed, 10 total
Tests:       68 passed, 68 total
Snapshots:   1 passed, 1 total
Time:        28.776s
Ran all test suites.

which I am getting if I remove the transform attribute from the jest object in package.json .如果我从package.jsonjest对象中删除transform属性,我会得到它。

How do I solve this issue ?我该如何解决这个问题?

I resolved this issue by updating my jest.config.json file and changing the transform properties like below:我通过更新 jest.config.json 文件并更改转换属性来解决这个问题,如下所示:

// before

...
"transform": {
    "^.+\\.(ts|js|html)$": "<rootDir>/node_modules/jest-preset-angular/preprocessor.js"
}
...

//after
...
"transform": {
    "^.+\\.(ts|js|html)$": "ts-jest"
}
...

尝试删除node_modulespackage-lock.json然后运行npm installyarn << 这个解决方案对我package-lock.json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8 和 jest - 找不到文件:jest-preset-angular/InlineHtmlStripStylesTransformer.js - Angular 8 and jest - File not found: jest-preset-angular/InlineHtmlStripStylesTransformer.js 相当于 jest-preset-angular 中的 and.callThrough() - Equivalent of and.callThrough() in jest-preset-angular 使用 angular 工作区配置 `jest-preset-angular` - Configure `jest-preset-angular` with angular workspace 如何在 jest.config.js 中设置 tsconfig 路径。 Angular 8. 开玩笑预设角度 - How to setup tsconfig paths in jest.config.js. Angular 8. jest-preset-angular 运行jest-preset-angleular时ENONET错误 - ENONET error while running jest-preset-angular 在./node_modules/@angular/core/esm5/core.js中找不到错误:TypeError:dep.isEqualResource不是一个函数 - ERROR in ./node_modules/@angular/core/esm5/core.js Module not found: TypeError: dep.isEqualResource is not a function Angular 4 + Jest-Preset-Angular:使用从文件导入的触发器在主机上测试动画 - Angular 4 + Jest-Preset-Angular : testing animation on host with trigger imported from file Angular 11:-找不到模块:错误:无法解析“../../../../node_modules/@angular/forms/forms” - Angular 11:- Module not found: Error: Can't resolve '../../../../node_modules/@angular/forms/forms' 更新到 Angular 13 后,Jest 在 node_modules 中运行测试 - Jest runs tests in node_modules after update to Angular 13 如何修复/node_modules/angular2/bundles/angular2.dev.js找不到(404) - How to fix /node_modules/angular2/bundles/angular2.dev.js Not found(404)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM