Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Property 'element' does not exist on type 'ExpectStatic' #6965

Closed
6 tasks done
Pinakin-Desai-HBK opened this issue Nov 26, 2024 · 10 comments
Closed
6 tasks done

Property 'element' does not exist on type 'ExpectStatic' #6965

Pinakin-Desai-HBK opened this issue Nov 26, 2024 · 10 comments

Comments

@Pinakin-Desai-HBK
Copy link

Pinakin-Desai-HBK commented Nov 26, 2024

Describe the bug

Installed the latest vitest (2.1.5) but getting the following error - Property 'element' does not exist on type 'ExpectStatic'.

This was reported previously but seemed to have been fixed - #4273

Test:
image

package.json:
image

vitest:
image

Reproduction

Install the latest vitest and try to access the element property of expect.

System Info

vitest 2.1.5

Used Package Manager

npm

Validations

Copy link

Hello @Pinakin-Desai-HBK. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

@Pinakin-Desai-HBK
Copy link
Author

Pinakin-Desai-HBK commented Nov 26, 2024

@kossnocorp
Copy link

Importing the matchers.d.ts directly in the test file helps, but it would be great to have a fix:

import "@vitest/browser/matchers.d.ts";

// Your tests...

@hi-ogawa
Copy link
Contributor

Adding manually dts is necessary since expect.element is only available on browser mode. Closing as this is in the doc https://vitest.dev/guide/browser/assertion-api.html and we'll soon improve it better #6934

@Smrtnyk
Copy link

Smrtnyk commented Nov 28, 2024

@hi-ogawa
I am not a typescript expert, but just an idea.
Would it be possible to have a special expect that could be imported from @vitest/browser that would already have this automatically typed?

@kossnocorp
Copy link

I think it would be safe to include both the type and the dummy matcher that would throw an error if it's not the browser environment in the global expect, or, as suggested, have a separate browser expect/browserExpect. It will be great for discoverability and improve the DX.

@hi-ogawa
Copy link
Contributor

I think it would be safe to include both the type and the dummy matcher that would throw an error if it's not the browser environment in the global expect

Yeah, that might work, but I guess there are a few concerns:

  • @vitest/browser/matchers might pollute too much (e.g. IDE auto-completion) as it includes jest-dom assertions too
  • there's still provider specific extension for @vitest/browser/matchers/providers/xxx

@Pinakin-Desai-HBK
Copy link
Author

Pinakin-Desai-HBK commented Nov 28, 2024

Adding manually dts is necessary since expect.element is only available on browser mode. Closing as this is in the doc https://vitest.dev/guide/browser/assertion-api.html and we'll soon improve it better #6934

I don't see where it says to add it manually in that link.

@Pinakin-Desai-HBK
Copy link
Author

import "@vitest/browser/matchers.d.ts";

Thanks. This works too:

import "@vitest/browser/matchers";

@hi-ogawa
Copy link
Contributor

I don't see where it says to add it manually in that link.

@Pinakin-Desai-HBK I'm referring to the first paragraph in the doc. There are actually many ways to help tsc and the doc mentions that compilerOptions.options in tsconfig.json:

If you are using TypeScript or want to have correct type hints in expect, make sure you have either @vitest/browser/providers/playwright or @vitest/browser/providers/webdriverio specified in your tsconfig depending on the provider you use. If you use the default preview provider, you can specify @vitest/browser/matchers instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants