Skip to content

Commit

Permalink
test: add jest-dom matchers and eslint plugin (#2850)
Browse files Browse the repository at this point in the history
  • Loading branch information
kretajak authored Nov 13, 2024
1 parent 15eaeb7 commit 5a8842f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"react-hooks",
"import",
"@vitest",
"jest-dom",
"testing-library",
"eslint-plugin-react-compiler"
],
Expand Down Expand Up @@ -107,6 +108,7 @@
"overrides": [
{
"extends": [
"plugin:jest-dom/recommended",
"plugin:testing-library/react",
"plugin:@vitest/legacy-recommended"
],
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@rollup/plugin-node-resolve": "^15.3.0",
"@rollup/plugin-replace": "^6.0.1",
"@rollup/plugin-typescript": "^12.1.1",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@types/node": "^22.8.4",
"@types/react": "^18.3.12",
Expand All @@ -127,6 +128,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-alias": "^1.1.2",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jest-dom": "^5.4.0",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-compiler": "19.0.0-beta-6fc168f-20241025",
Expand Down
89 changes: 89 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom/vitest'
8 changes: 4 additions & 4 deletions tests/shallow.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe('useShallow', () => {
render(<TestShallow />)

expect(countRenders).toBe(1)
expect(screen.getByTestId('test-shallow').textContent).toBe('a,b,c')
expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c')

act(() => {
useMyStore.setState({ a: 4 }) // This will not cause a re-render.
Expand All @@ -153,7 +153,7 @@ describe('useShallow', () => {
})

expect(countRenders).toBe(2)
expect(screen.getByTestId('test-shallow').textContent).toBe('a,b,c,d')
expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,d')
})

it('does not cause stale closure issues', () => {
Expand All @@ -178,10 +178,10 @@ describe('useShallow', () => {

render(<TestShallowWithState />)

expect(screen.getByTestId('test-shallow').textContent).toBe('a,b,c,0')
expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,0')

fireEvent.click(screen.getByTestId('test-shallow'))

expect(screen.getByTestId('test-shallow').textContent).toBe('a,b,c,1')
expect(screen.getByTestId('test-shallow')).toHaveTextContent('a,b,c,1')
})
})
8 changes: 4 additions & 4 deletions tests/ssr.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe.skipIf(!React.version.startsWith('18'))(
document.body.appendChild(container)
container.innerHTML = view

expect(container.textContent).toContain('bears: 0')
expect(container).toHaveTextContent(/bears: 0/)

await act(async () => {
hydrateRoot(
Expand All @@ -62,7 +62,7 @@ describe.skipIf(!React.version.startsWith('18'))(
})

const bearCountText = await screen.findByText('bears: 1')
expect(bearCountText).not.toBeNull()
expect(bearCountText).toBeInTheDocument()
document.body.removeChild(container)
})
it('should not have hydration errors', async () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ describe.skipIf(!React.version.startsWith('18'))(
document.body.appendChild(container)
container.innerHTML = view

expect(container.textContent).toContain('bears: 0')
expect(container).toHaveTextContent(/bears: 0/)

const consoleMock = vi.spyOn(console, 'error')

Expand All @@ -111,7 +111,7 @@ describe.skipIf(!React.version.startsWith('18'))(
expect(consoleMock).toHaveBeenCalledTimes(0)

const bearCountText = await screen.findByText('bears: 1')
expect(bearCountText).not.toBeNull()
expect(bearCountText).toBeInTheDocument()
document.body.removeChild(container)
})
},
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"verbatimModuleSyntax": true,
"declaration": true,
"isolatedDeclarations": true,
"types": ["@testing-library/jest-dom"],
"noEmit": true,
"baseUrl": ".",
"paths": {
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineConfig({
environment: 'jsdom',
dir: 'tests',
reporters: 'basic',
setupFiles: ['tests/setup.ts'],
coverage: {
include: ['src/**/'],
reporter: ['text', 'json', 'html', 'text-summary'],
Expand Down

0 comments on commit 5a8842f

Please sign in to comment.