1

我将在 Angular 12 下使用 Storybook 安装快照测试。我安装 jest$ yarn add --dev jest jest-preset-angular @types/jest并进行设置

"jest": {
  "preset": "jest-preset-angular",
  "setupFilesAfterEnv": ["<rootDir>/src/setupJest.ts"]
}

并设置 setupJest.ts 数据只有一行import 'jest-preset-angular/setup-jest';

那是开玩笑的设置。这对我有用。

问题是来自 Storybook 的 Storyshorts 插件。我安装 Storyshorts并使用以下代码yarn add @storybook/addon-storyshots --dev创建文件:src/storyshorts.test.js

import initStoryshots from '@storybook/addon-storyshots';

initStoryshots();

jest当我在我的 Angular 项目中运行命令时,我变成了这个错误:

Cannot find module 'jest-preset-angular/build/setup-jest' from 'node_modules/@storybook/addon-storyshots/dist/ts3.9/frameworks/angular/loader.js'
  at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
  at setupAngularJestPreset (node_modules/@storybook/addon-storyshots/dist/ts3.9/frameworks/angular/loader.js:36:14)
  at Object.load (node_modules/@storybook/addon-storyshots/dist/ts3.9/frameworks/angular/loader.js:43:5)
  at Object.loadFramework [as default] (node_modules/@storybook/addon-storyshots/dist/ts3.9/frameworks/frameworkLoader.js:26:19)
  at Object.testStorySnapshots [as default] (node_modules/@storybook/addon-storyshots/dist/ts3.9/api/index.js:42:39)

我的 Package.json 有以下条目:

...
"@storybook/addon-storyshots": "^6.3.12",
"@types/jest": "^27.0.2",
"jest": "^27.3.1",
"jest-preset-angular": "^10.0.1",

和 Angular 12 包。

4

1 回答 1

2

编辑:更新到 Angular 13 后:

/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  globals: {
    "ts-jest": {
        tsconfig: '<rootDir>/tsconfig.storyshots.json',
    }
  },
  transformIgnorePatterns: [],
  moduleNameMapper: {
    'jest-preset-angular/build/setup-jest': 'jest-preset-angular/setup-jest',
    'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer': 'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/AngularSnapshotSerializer': 'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/HTMLCommentSerializer': 'jest-preset-angular/build/serializers/html-comment',
  }
};

===原始(Angular 12) ===

欢迎来到地狱:D!

对于您的问题:storyshots 插件尝试setup-jest.ts从错误的路径加载文件。从jest-preset-angular版本 9setup-jest.ts开始,不再位于jest-preset-angular/build/文件夹中(这不是唯一受影响的文件)。Storyshots 插件尚未处理此更改,因此您可以使用moduleNameMapperfrom jest config 来重写路径并解决您的问题。

以我jest.config.js为例:

require('jest-preset-angular/ngcc-jest-processor');

module.exports = {
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!(@storybook/addon-docs))',
  ],
  moduleNameMapper: {
    'jest-preset-angular/build/setup-jest': 'jest-preset-angular/setup-jest',
    'jest-preset-angular/build/AngularNoNgAttributesSnapshotSerializer':
      'jest-preset-angular/build/serializers/no-ng-attributes',
    'jest-preset-angular/build/AngularSnapshotSerializer':
      'jest-preset-angular/build/serializers/ng-snapshot',
    'jest-preset-angular/build/HTMLCommentSerializer':
      'jest-preset-angular/build/serializers/html-comment',
  },
};

如果您需要更多解释,请问我:)

于 2021-10-27T15:22:13.880 回答