我一直在try 让Storybook交互测试适用于我们的生产Angular 应用程序.我们已经有了使用Karma/Jasmine的现有测试,但我们希望为更多的用户界面驱动测试实现Storybook测试.

故事在组件文件旁边的.stories.ts个文件中定义,如下所示:

import { Meta, Story } from '@storybook/angular';
import { userEvent, within } from '@storybook/testing-library';
import { CounterComponent } from "./counter.component";

export default {
  component: CounterComponent,
  title: 'Components/Counter',
  excludeStories: /.*Data$/,
} as Meta;

const Template: Story = () => ({});

export const Default = Template.bind({});
Default.play = async ({ canvasElement }) => {
  const canvas = within(canvasElement);

  await expect(canvas.getByTestId('text').innerHTML).toBe('0');

  await userEvent.click(canvas.getByRole('button'));

  await expect(canvas.getByTestId('text').innerHTML).toBe('1');
};

But when building or running Storybook tests I run into the error cannot find name 'expect' or expect is not defined.
I have tried explicitly importing expect from @jest/globals but this is not possible.
I have tried looking into assigning various things to global in preview.ts/js but failed to fix my errors.

我已经在这个存储库中重新创建了它https://github.com/bertcardinaels/storybook-angular-test

如果有任何帮助或指点,我们将不胜感激,因为这should是可能的,对吗?

推荐答案

对我来说,补充说

import { expect } from '@storybook/jest';

帮上忙了.只要故事书已经在运行,一切都很好.但当我重新启动Storybook时,不幸地收到一个错误:

错误TS4111:属性‘toBeFalse’来自索引签名,因此 必须使用[‘toBeFalse’]访问它.

在其他一些与故事书无关的测试中.可能是因为karma/jasmine的类型定义与JEST冲突.

如果有人知道如何解决这个问题...:)

Angular相关问答推荐

Angular 17上的material 工具提示的自定义样式

Angular 17@在大小写时切换多个值

命令不起作用的初始菜单项

CDK虚拟滚动由于组件具有注入属性而在滚动时看到错误的值

Angular :为什么我得到了一个可观察到的共鸣

弯管记忆

Angular 16+使用ngTemplateOutlet有条件地呈现几个模板中的一个

如何在Angular功能路由保护中取消订阅RxJs主题

无法在单个元素上多次监听同一个事件

Angular 迁移 14 到 15 / 16:angular universal 是否停止将 <!-- this page was prerendered with angular universal --> 用于预渲染页面?

Angular 14 类型表单 - 数字控件的初始值

Angular:为什么在我的自定义 ErrorHandler 中订阅主题不触发?

如何在 cypress 测试中实现拖放?

为什么 Angular2 (click) 事件没有在

如何在Angular2中基于特定的构建环境注入不同的服务?

Angular2订阅对子组件中@Input的更改

在同一个组件中使用 MatSort 的多个 mat-table

`[(ngModel)]` vs `[(value)]`

从 Angular 中的自定义表单组件访问 FormControl

Angular 2 中的动态模板 URL