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

fix(types): augment jest matchers #665

Merged
merged 1 commit into from Feb 3, 2022
Merged

fix(types): augment jest matchers #665

merged 1 commit into from Feb 3, 2022

Conversation

sheremet-va
Copy link
Member

Closes #664

Vitest expect now can see external jest libraries matchers, namely @testing-library/jest-dom assertions are recognized when importing from vitest. (global expect is still overriden by jest's expect)

Vitest expect now can see external jest libraries matchers
@netlify
Copy link

netlify bot commented Feb 2, 2022

✔️ Deploy Preview for vitest-dev ready!

🔨 Explore the source changes: 834696f

🔍 Inspect the deploy log: https://app.netlify.com/sites/vitest-dev/deploys/61fa23be40abaa0008504879

😎 Browse the preview: https://deploy-preview-665--vitest-dev.netlify.app

@antfu antfu changed the title fix: augment jest matchers fix(types): augment jest matchers Feb 3, 2022
@antfu antfu merged commit 6565492 into main Feb 3, 2022
@antfu antfu deleted the sheremet-va/augment-jest branch February 3, 2022 02:14
@pd4d10
Copy link
Contributor

pd4d10 commented Feb 4, 2022

Now we have the types, but there seems to be a problem at runtime? Because @testing-library/jest-dom doesn't add the methods to the expect() correctly

@sheremet-va
Copy link
Member Author

Now we have the types, but there seems to be a problem at runtime? Because @testing-library/jest-dom doesn't add the methods to the expect() correctly

Can you create a reproduction? It works for me.

@pd4d10
Copy link
Contributor

pd4d10 commented Feb 4, 2022

Just got it working correctly. This is the repo: https://github.com/bytedance/bytemd

Errors messages:

 FAIL  packages/bytemd/src/__tests__/editor.test.ts > mode > tab
Error: Invalid Chai property: toBeVisible
 ❯ packages/bytemd/src/__tests__/editor.test.ts:79:56
     77|     const preview = $.getByText('Preview')
     78| 
     79|     expect($.container.querySelector('.bytemd-editor')).toBeVisible()
       |                                                        ^
     80|     expect(write).toHaveClass('bytemd-toolbar-tab-active')
     81|     // expect($.container.querySelector('.bytemd-preview')).toHaveStyle('width:0');

Added these lines to make it work:

import { expect } from 'vitest'
import matchers from '@testing-library/jest-dom'
expect.extend(matchers)

https://github.com/bytedance/bytemd/blob/7fb94a215d955ed1d0ea845dbf1a2528e8834f3d/test-setup.ts#L2-L3

@sheremet-va
Copy link
Member Author

Just got it working correctly. This is the repo: https://github.com/bytedance/bytemd

Errors messages:

 FAIL  packages/bytemd/src/__tests__/editor.test.ts > mode > tab
Error: Invalid Chai property: toBeVisible
 ❯ packages/bytemd/src/__tests__/editor.test.ts:79:56
     77|     const preview = $.getByText('Preview')
     78| 
     79|     expect($.container.querySelector('.bytemd-editor')).toBeVisible()
       |                                                        ^
     80|     expect(write).toHaveClass('bytemd-toolbar-tab-active')
     81|     // expect($.container.querySelector('.bytemd-preview')).toHaveStyle('width:0');

Added these lines to make it work:

import { expect } from 'vitest'
import matchers from '@testing-library/jest-dom'
expect.extend(matchers)

https://github.com/bytedance/bytemd/blob/7fb94a215d955ed1d0ea845dbf1a2528e8834f3d/test-setup.ts#L2-L3

If you are not using test.globals, then you need to manually extend them, since library doesn't have access to expect, yes.

@pd4d10
Copy link
Contributor

pd4d10 commented Feb 4, 2022

Thanks. Got stuck by this issue for a little while, so I added a note to the documentation for the benefit of other users

#676

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

Successfully merging this pull request may close these issues.

Missing toHaveAttribute from expect() return type
3 participants