[英]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 的路径,因为我不太确定您的目录结构是什么样的。
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.