Skip to content

Commit

Permalink
fix workaround by @hi-ogawa
Browse files Browse the repository at this point in the history
  • Loading branch information
dolie committed Jun 20, 2024
1 parent b6255bf commit ac9d34b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
__FIXED__ : workaround in `vite.config.js` by @hi-ogawa

This repo shows an error while testing SWR on Preact with Vitest

TypeError: Cannot read properties of null (reading 'useContext')

`pnpm dev` working site

`pnpm vitest` not working test
`pnpm vitest` not working test

Issues follow up :
[Issue in Vitest repo](https://github.com/vitest-dev/vitest/issues/5915)
[Issue in Preact repo](https://github.com/preactjs/preset-vite/issues/124)
[Issue in SWR repo](https://github.com/vercel/swr/issues/2981)
9 changes: 6 additions & 3 deletions src/comp.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@testing-library/preact';
import { render, screen, waitFor } from '@testing-library/preact';
import { Comp } from './comp';
import { describe, it, expect, afterAll, beforeAll, afterEach } from 'vitest';
import { SWRConfig } from 'swr';
Expand All @@ -16,7 +16,7 @@ describe('Comp', () => {
afterAll(() => server.close())
afterEach(() => server.resetHandlers())

it('should render', () => {
it('should render', async () => {
render(
<SWRConfig value={{
dedupingInterval: 2000,
Expand All @@ -25,6 +25,9 @@ describe('Comp', () => {
<Comp />
</SWRConfig>
);
expect(screen.getByText('working')).toBeTruthy();

await waitFor(() => {
expect(screen.getByText('working')).toBeTruthy();
})
});
});
8 changes: 8 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
deps: {
optimizer: {
web: {
enabled: true,
include: ['swr', 'use-sync-external-store'],
},
},
},
}
});

0 comments on commit ac9d34b

Please sign in to comment.