简体   繁体   English

Angular 8 和 jest - 找不到文件:jest-preset-angular/InlineHtmlStripStylesTransformer.js

[英]Angular 8 and jest - File not found: jest-preset-angular/InlineHtmlStripStylesTransformer.js

I just created angular application using the cli (v8 - last version from today).我刚刚使用 cli(v8 - 今天的最新版本)创建了 angular 应用程序。 nothing special I done so far.到目前为止我没有做任何特别的事情。 ( ng new ng-app ). ng new ng-app )。

I use jest-schematic to add jest to my angular project.我使用jest-schematic将 jest 添加到我的 angular 项目中。

cd ng-app
ng add @briebug/jest-schematic

The problem is I get an error when I run the test using: npm run test :问题是我在运行测试时遇到错误: npm run test

> jest

 FAIL  src/app/app.component.spec.ts
  ● Test suite failed to run

    File not found: jest-preset-angular/InlineHtmlStripStylesTransformer.js (resolved as: C:\ng-app\jest-preset-angular\InlineHtmlStripStylesTransformer.js)

What is missing here?这里缺少什么? How do I fix that?我该如何解决?

In the jest-preset-angular documentation , the corrent configuration is this: jest-preset-angular/build/InlineFilesTransformer .jest-preset-angular 文档中,正确的配置是: jest-preset-angular/build/InlineFilesTransformer

So change in your package.json the lines from:因此,在您的package.json中更改以下行:

   "astTransformers": [
      "jest-preset-angular/InlineHtmlStripStylesTransformer.js"
   ]

to

"astTransformers": [
   "jest-preset-angular/build/InlineFilesTransformer",
   "jest-preset-angular/build/StripStylesTransformer"
]

For me, astTransformers does not exist in package.json.对我来说,package.json 中不存在 astTransformers。 Searching exhaustively, the entry is in jest.impl.js, on line 24.彻底搜索,条目在 jest.impl.js 中,第 24 行。

(Angular 8.2.1. using @nrwl/angular 8.2.7) (Angular 8.2.1。使用@nrwl/angular 8.2.7)

Aside from that, the same changes apply, change the line to:除此之外,适用相同的更改,将行更改为:

"astTransformers": [
   "jest-preset-angular/build/InlineFilesTransformer",
   "jest-preset-angular/build/StripStylesTransformer"
]

Note the comment from the jest team on that line:请注意该行的笑话团队的评论:

 // TODO: This is hacky, We should probably just configure it in the user's workspace 
// If jest-preset-angular is installed, apply settings

I definitely don't like editing that file, but putting the change in the jest.config files didn't do the trick (I was hoping the lib would pick it up as options), and if it does belong somewhere in package.json, I don't know where.我绝对不喜欢编辑该文件,但将更改放在 jest.config 文件中并没有解决问题(我希望 lib 将其作为选项选择),如果它确实属于 package.json ,我不知道在哪里。

The accepted answer's syntax has changed.接受的答案的语法已更改。 If you need to do this on Angular 12.x or @nrwl/angular 12.x then try -如果您需要在 Angular 12.x 或 @nrwl/angular 12.x 上执行此操作,请尝试 -

In jest.config.js -jest.config.js -

 module.exports = {
        // [...]
        globals: {
            'ts-jest': {
                astTransformers: {
                    before: [
                        'jest-preset-angular/build/InlineFilesTransformer',
                        'jest-preset-angular/build/StripStylesTransformer',
                    ],
                },
            },
        },
    }

In package.json -package.json -

{
    "jest": {
        "globals": {
            "ts-jest": {
                "astTransformers": {
                    "before": [
                        "jest-preset-angular/build/InlineFilesTransformer",
                        "jest-preset-angular/build/StripStylesTransformer"
                    ]
                }
            }
        }
    }
}

Reference - Link参考 - 链接

The answers are now obsolete (I am on Jest 27.5.1).答案现在已经过时(我在 Jest 27.5.1)。 What helped me was to remove astTransformers from jest.config.js because they were removed from jest-preset-angular.帮助我的是从 jest.config.js 中删除 astTransformers,因为它们已从 jest-preset-angular 中删除。 What helped me was to add this to jest.config.js:对我有帮助的是将其添加到 jest.config.js 中:

 module.exports = {... transform: { '^.+.(ts|mjs|js|html)$': 'jest-preset-angular' }, transformIgnorePatterns: ['node_modules/(?..*,mjs$)']. ... }

This is explained in greater detail here: https://github.com/nrwl/nx/issues/7844这在这里更详细地解释: https://github.com/nrwl/nx/issues/7844

It is written for Nx, but it also applies to vanilla Angular.它是为 Nx 编写的,但也适用于 vanilla Angular。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM