storybookjs / testing-react

Testing utilities that allow you to reuse your Storybook stories in your React unit tests!
MIT License
588 stars 24 forks source link

@storybook/testing-react type errors #62

Closed rexkenley closed 2 years ago

rexkenley commented 2 years ago

Describe the bug I am getting the following ts errors with my code while trying to use the new composeStories.

image image

I was able to "fix" it by using casting, but I don't think this is right.

image

What should I do?

Storybook version: 6.4.4

yannbf commented 2 years ago

Hey @rexkenley thanks for opening this issue! Could you provide an example of how your stories look like? Thanks!

robcaldecott commented 2 years ago

I am seeing something very similar with with 6.4.

This story works (if I use Meta):

import { Meta, ComponentStory } from "@storybook/react";
import { Logo } from ".";

export default {
  component: Logo,
} as Meta;

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

export const Default = Template.bind({});
Default.args = {
  width: 200,
  fill: "#3C576B",
};

This story does not work (using ComponentMeta):

import { ComponentStory, ComponentMeta } from "@storybook/react";
import { Logo } from ".";

export default {
  component: Logo,
} as ComponentMeta<typeof Logo>;

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

export const Default = Template.bind({});
Default.args = {
  width: 200,
  fill: "#3C576B",
};

The test import looks like this:

import { render, screen } from "@testing-library/react";
import { composeStories } from "@storybook/testing-react";
import * as stories from "./Logo.stories";

// TS2345
const { Default } = composeStories(stories);

Full error when using ComponentMeta:

Argument of type 'typeof import("/Users/caldecor/Projects/storybook-64/src/Logo/Logo.stories")' is not assignable to parameter of type 'StoryFile'.
  Types of property 'default' are incompatible.
    Type 'ComponentMeta<({ height, width, fill }: LogoProps) => Element>' is not assignable to type 'Meta<Args>'.
      Type 'ComponentMeta<({ height, width, fill }: LogoProps) => Element>' is not assignable to type 'BaseAnnotations<ReactFramework, Args>'.
        Types of property 'decorators' are incompatible.
          Type 'DecoratorFunction<ReactFramework, LogoProps>[] | undefined' is not assignable to type 'DecoratorFunction<ReactFramework, Args>[] | undefined'.
            Type 'DecoratorFunction<ReactFramework, LogoProps>[]' is not assignable to type 'DecoratorFunction<ReactFramework, Args>[]'.
              Type 'DecoratorFunction<ReactFramework, LogoProps>' is not assignable to type 'DecoratorFunction<ReactFramework, Args>'.
                Types of parameters 'c' and 'c' are incompatible.
                  Type 'StoryContext<ReactFramework, Args>' is not assignable to type 'StoryContext<ReactFramework, LogoProps>'.
                    Type 'StoryContext<ReactFramework, Args>' is not assignable to type '{ component?: ComponentType<any> | undefined; subcomponents?: Record<string, ComponentType<any>> | undefined; parameters: Parameters; initialArgs: LogoProps; argTypes: StrictArgTypes<...>; }'.
                      Types of property 'initialArgs' are incompatible.
                        Type 'Args' is not assignable to type 'LogoProps'.ts(2345)
rexkenley commented 2 years ago

robcaldecott pretty much nailed it. After I implemented what he did, I got new ts errors. image image

phenrique7 commented 2 years ago

I'm experienced the same type errors when I upgraded sb to 6.4. My workaround was to use Meta instead of ComponentMeta, as @robcaldecott reported.

yannbf commented 2 years ago

Hey @phenrique7 could you show the example of the component that fails? Is that component typed in any way or does it look like this?

// no types at all, just auto-inferred JSX.Element
const MyComponent = () => <div>HelloWorld</div>
phenrique7 commented 2 years ago

@yannbf, I created a reproducible repo: https://github.com/phenrique7/storybook-testing-react-type-errors. Hope this can help.

rexkenley commented 2 years ago

@yannbf, I didn't make any changes other than applying the composeStories.

Here is the repo: https://github.com/rexkenley/rollupError The story is here https://github.com/rexkenley/rollupError/tree/main/stories/components/Timesheet

yannbf commented 2 years ago

Thanks @phenrique7 and @rexkenley for the repros, that helps me a ton!! I'll work on it likely tomorrow and release a fix as soon as possible, and make sure to test the fix on both your repos.

have a great weekend!

yannbf commented 2 years ago

Hey everyone, I released a fix in v1.2.1 that should address this issue.

The repros you provided were super helpful and I made sure to test in each one of them.

@rexkenley I noticed a few things in your repo and I'll make a PR explaining the correct usage of this library!

@phenrique7 I saw the easter egg there 😂 Muito obrigado!

rexkenley commented 2 years ago

@yannbf Thank you so much to fixing this so quickly. I am looking forward to the correct usage link.

yannbf commented 2 years ago

@yannbf Thank you so much to fixing this so quickly. I am looking forward to the correct usage link.

No worries, thanks for using the library!

I opened the PR at https://github.com/rexkenley/rollupError/pull/1