Angular 12 的故事书插件 StoryShots
Storybook Addon StoryShots for Angular 12
我将在 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只有一行的数据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();
当我在我的 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 个包。
编辑:更新到 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
版本 9,setup-jest.ts
不再位于 jest-preset-angular/build/
文件夹中(这不是唯一受影响的文件)。
Storyshots 插件尚未处理此更改,因此您可以使用 jest 配置中的 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',
},
};
如果您需要更多解释,请问我:)
我将在 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只有一行的数据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();
当我在我的 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 个包。
编辑:更新到 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
版本 9,setup-jest.ts
不再位于 jest-preset-angular/build/
文件夹中(这不是唯一受影响的文件)。
Storyshots 插件尚未处理此更改,因此您可以使用 jest 配置中的 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',
},
};
如果您需要更多解释,请问我:)