简体   繁体   English

Jest 在尝试使用打字稿使用故事书中的故事时抛出“属性 'id' 的类型不兼容”错误

[英]Jest throws "Types of property 'id' are incompatible" error when it is trying to use stories from storybook using typescript

So..I am trying to use stories for my unit test with jest + RTL to minimize duplication (explained here ) and the test throws "Types of property 'id' are incompatible" error when I pass the args that is also used in my story.所以..我正在尝试通过 jest + RTL 将故事用于我的单元测试,以最大程度地减少重复( 在此处解释),并且当我传递也用于我的故事。

I am currently using below packages as my testing suite.我目前正在使用以下软件包作为我的测试套件。

"@storybook/react": "^6.5.9",
"@storybook/testing-react": "^1.2.4",
"@testing-library/react": "^13.3.0",
"jest": "^28.1.1",
// and others...

Below is my test.下面是我的测试。

import * as React from 'react';
import { screen, render } from '@testing-library/react';

import { Default as Checkbox } from '../../stories/AccountCheckbox.stories';

const renderCheckbox = () => {
  render(<Checkbox {...Checkbox.args} />); // the point where it throws an type error that says 'id is incompatible'.
}

describe('<AccountCheckbox />', () => {
  test('should display account information', () => {
    renderCheckbox();

    expect(screen.getByTestId('example-test-id')).toBeInTheDocument();
  });
});

Below is my story.下面是我的故事。

import React from 'react';
import { ComponentMeta, ComponentStory } from '@storybook/react';

import AccountCheckbox from '../components/AccountCheckbox';
import { StoryAppWrapper } from '../../.storybook/wrapper/StoryAppWrapper';

export default {
  title: 'AccountCheckbox',
  component: AccountCheckbox,
  decorators: [(Story) => <StoryAppWrapper>{Story()}</StoryAppWrapper>]
} as ComponentMeta<typeof AccountCheckbox>;

const Template: ComponentStory<typeof AccountCheckbox> = (args) => (
  <AccountCheckbox {...args} />
);

export const Default = Template.bind({});

// These are the args I am trying to utilize in my test as well.
Default.args = {
  id: 'example-id',
  testId: 'example-test-id',
  label: 'Account 1',
  selected: false,
  onChange: () => { },
};

Below is the type for the checkbox I declared.下面是我声明的复选框的类型。

export type CheckboxProps = {
  id: string;
  testId: string;
  label: string;
  selected: boolean;
  onChange: any;
};

Below is the actual code.下面是实际代码。

import * as React from 'react';
import { Checkbox } from 'custom-lib';

import { CheckboxProps } from '../../types/types';

const AccountCheckbox = ({
  id,
  testId,
  label,
  selected,
  onChange,
}: CheckboxProps): JSX.Element => {
  return (
    <Checkbox
      id={id}
      data-testid={testId}
      label={label}
      state={selected ? 'true' : 'false'}
      onChange={onChange}
    />
  );
};

export default AccountCheckbox;

Can anyone pinpoint what I've missed within these codes?谁能指出我在这些代码中遗漏了什么?

You're trying to render the Story, not the Checkbox itself.您正在尝试渲染故事,而不是复选框本身。 Why not import the checkbox directly, but use the story args too?为什么不直接导入复选框,但也使用故事 args 呢?

import * as React from 'react';
import { screen, render } from '@testing-library/react';
import AccountCheckbox from '../components/AccountCheckbox';
import { Default as AccountCheckboxStory } from '../../stories/AccountCheckbox.stories';

const renderCheckbox = () => {
  render(<AccountCheckbox {...AccountCheckboxStory.args} />);
}

describe('<AccountCheckbox />', () => {
  test('should display account information', () => {
    renderCheckbox();

    expect(screen.getByTestId('example-test-id')).toBeInTheDocument();
  });
});

You may need to adjust paths to the Story/AccountCheckbox as I'm not quite sure what your directory structure looks like.您可能需要调整 Story/AccountCheckbox 的路径,因为我不太确定您的目录结构是什么样的。

Update更新

Going from your comment and the link, it seems that they are using some form of a compositional function to wrap the exported stories and return them as components.从您的评论和链接来看,他们似乎正在使用某种形式的组合函数来包装导出的故事并将它们作为组件返回。 So in your case, would you not do this:所以在你的情况下,你不会这样做:

import React from 'react';
import { render, screen } from '@testing-library/react';
import { composeStories } from '@storybook/testing-react';

import * as stories from '../../stories/AccountCheckbox.stories';

const { Default: AccountCheckBoxDefault } = composeStories(stories);

test('renders profile page', async () => {
  render(<AccountCheckBoxDefault />);
  // ... some assertions here
});

describe('<AccountCheckbox />', () => {
  test('should display account information', () => {
    renderCheckbox();

    expect(screen.getByTestId('example-test-id')).toBeInTheDocument();
  });
});

I don't think it's actually necessary to pass in the args, as those args should be used by the story itself.我认为实际上没有必要传递参数,因为这些参数应该由故事本身使用。

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

相关问题 如何让故事书在故事中显示打字稿类型 - How to get storybook to display typescript types in stories ThemeProvider/Typescript 错误:属性“孩子”的类型不兼容 - ThemeProvider/Typescript error: Types of property 'children' are incompatible 与 typescript 开玩笑在导入文件时抛出错误 - jest with typescript throws an error when importing a file 打字稿:属性“数据”的类型不兼容 - Typescript: Types of property 'data' are incompatible 使用rewiremock时,故事书构建的版本故事崩溃 - Storybook built version stories crashing when using rewiremock 运行Jest测试命令时,React本机打字稿会引发错误 - React native typescript throws error when running jest test command 使用故事书中的组件时出现addComponentAsRefTo错误 - addComponentAsRefTo error when using component from storybook React + Redux + Storybook:在编写故事书故事时如何使用连接的 React 路由器的 useParams? - React + Redux + Storybook: How to use connected react router's useParams when writing storybook stories? 材质 UI + Typescript:属性“颜色”的类型不兼容 - Material UI + Typescript: Types of property 'color' are incompatible React typescript 属性“类型”的类型不兼容 - React typescript Types of property 'type' are incompatible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM