enzymejs / enzyme-matchers

Jasmine/Jest assertions for enzyme
MIT License
892 stars 116 forks source link

jest-enzyme + typescript: non-string selectors are rejected #290

Closed SevInf closed 4 years ago

SevInf commented 5 years ago

This is a valid code in JS:

const DummyComponent = () => <div />;
expect(wrapper).toContainMatchingElement(DummyComponent)

However, typescript rejects it:

Argument of type '() => Element' is not assignable to parameter of type 'string'.

Enzyme's own type definitions contain a much more flexible signature for selectors, where jest-enzyme defines only string selectors.

One possible workaround is rewriting original assertion to:

expect(wrapper.find(DummyComponent)).toExist();

but would be nice if the original code was accepted as is.

blainekasten commented 4 years ago

I think you want to use toContainReact

expect(wrapper).toContainReact(<DummyComponent />)

I'm going to close, but let me know if this doesn't solve your needs and we can dive into this more. Absolutely want to make sure you get the help you need :)

allanpaiste commented 3 years ago

@blainekasten

Although the provided example indeed solves that specific use-case, the fact that the assertions use enzyme.find under the hood, does provide additional flexibility when writing tests:

// Assert the number of instances of certain component
expect(wrapper).toContainMatchingElements(3, DummyComponent);

// Assert any component with certain prop
expect(wrapper).toContainMatchingElements({someProp: 'someValue'});

Although the documentation does not really cover such use-cases, they are extremely useful when writing tests and I'd really vouch for not cutting users out from such treats as soon as they use typing argument typing.

It would just require copying over the overloads from enzyme find.