Jest.js StoryBook Addon Angular 12的StoryShots

pkln4tw6  于 2023-08-01  发布在  Jest
关注(0)|答案(1)|浏览(136)

我将在Angular 12下安装Snapshot Testing with Storybook。我安装jest $ yarn add --dev jest jest-preset-angular @types/jest并进行设置

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

字符串
并设置setupJest.ts Data,其中只有一行import 'jest-preset-angular/setup-jest';
这就是Jest设置。这对我很有效。
问题是Storybook中的Storyshorts插件。我安装了Storyshorts yarn add @storybook/addon-storyshots --dev并使用以下代码创建了文件src/storyshorts.test.js

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

initStoryshots();


当我在我的Angular项目中运行jest命令时,我会出现以下错误:

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包

rjjhvcjd

rjjhvcjd1#

编辑:>= Angular 16
上下文

  • 带jest-preset-angular 13.1.X
  • 使用@storybook/addon-storyshots 7.X.X

由于一个错误(TypeError: Cannot destructure property 'imports' of 'analyzedMetadata' as it is undefined.),您将无法再运行Storyshots。目前正在打开一个PR来修复它(https://github.com/storybookjs/storybook/pull/23555)。
我将在PR被合并时提供新的jest配置。
另外,你应该注意到StoryShots将在不久的将来被弃用,并将被test-runnera migration guide will be provided by the core team, stay tune)取代。
因此,Storyshots不会被改进/更改(除非社区在其弃用日期之前)。

编辑:>= Angular 13:
上下文

  • 关于jest-preset-angular 12.X.X
  • 关于@storybook/addon-storyshots 6.5.X

由于我保留Karma/Jasmine用于单元测试(更大的社区),Jest仅用于Storybook的快照测试,因此我的jest.config.js位于.storybook文件夹下,我创建了一个特定的tsconfig文件。

jest.config.js

const { defaults: jestNgPreset } = require('jest-preset-angular/presets');

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
  rootDir: '../',
  preset: 'jest-preset-angular',
  setupFilesAfterEnv: ['<rootDir>/.storybook/setup-jest.ts'],
  // Ensure to run ngcc for uncompliant Ivy lib
  globalSetup: 'jest-preset-angular/global-setup',
  globals: {
    'ts-jest': {
       ...jestNgPreset.globals['ts-jest'],
      tsconfig: '<rootDir>/.storybook/tsconfig.storyshots.json',
    },
  },
  transform: {
    ...jestNgPreset.transform,
    '^.+\\.stories\\.mdx?$': '<rootDir>/.storybook/empty.js',
  },
  // Don't transform files in node_modules, except those which match those patterns
  transformIgnorePatterns: [
    '<rootDir>/node_modules/(?!.*\\.mjs$|@storybook/addon-docs/angular|@angular|primeng|angular-auth-oidc-client)',
  ],
  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',
  },
};

字符串

tsconfig.storyshots.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "skipLibCheck": true,
    "outDir": "./out-tsc/spec",
    "module": "CommonJS",
    "types": ["jest"]
  },
  "files": ["./typings.d.ts"]
}

为空.js

/**
 * This file is used to prevent mdx files importation during snapshot testing with Jest (see jest.config.js).
 * I tried with testPathIgnorePatterns or storyKindRegex, but impossible.
 */
module.exports = {
  process: function () {
    return { code: '' };
  },
};


= * 原始(Angular 12)**=
欢迎来到地狱:D!
对于您的问题:storyshots插件试图从错误路径加载setup-jest.ts文件。自jest-preset-angular版本9以来,setup-jest.ts不再位于jest-preset-angular/build/文件夹中(并且这不是唯一受影响的文件)。Storyshots插件还没有处理这个变化,所以你可以使用jest config中的moduleNameMapper来重写路径并修复你的问题。
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',
  },
};


如果你需要更多的解释,问我:)

相关问题