Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions jest.config.build.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const config = require("./jest.config");
module.exports = {
import config from './jest.config.js'

const useBuildConfig = {
...config,
// override to use build files
moduleNameMapper: {}
}

export default useBuildConfig
11 changes: 7 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
const config = {
testEnvironment: 'jsdom',
testRegex: '/test/.*\\.test\\.tsx?$',
testPathIgnorePatterns: ['/node_modules/', '/e2e/'],
Expand All @@ -10,17 +10,20 @@ module.exports = {
'^swr/immutable$': '<rootDir>/src/immutable/index.ts',
'^swr/subscription$': '<rootDir>/src/subscription/index.ts',
'^swr/mutation$': '<rootDir>/src/mutation/index.ts',
'^swr/_internal$': '<rootDir>/src/_internal/index.ts',
'^swr/_internal$': '<rootDir>/src/_internal/index.ts'
},
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest']
'^.+\\.(t|j)sx?$': '@swc/jest'
},
coveragePathIgnorePatterns: [
'/node_modules/',
'/dist/',
'/test/',
'<rootDir>/src/_internal/utils/env.ts',
'<rootDir>/src/_internal/utils/env.ts'
],
coverageReporters: ['text', 'html'],
reporters: [['github-actions', { silent: false }], 'summary']
}


export default config
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,18 @@
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.18.2",
"@edge-runtime/jest-environment": "^3.0.4",
"@edge-runtime/jest-environment": "^4.0.0",
"@eslint/compat": "^2.0.0",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.39.1",
"@playwright/test": "1.57.0",
"@swc/core": "^1.15.3",
"@swc/jest": "0.2.39",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.2.1",
"@testing-library/dom": "^10.4.1",
"@testing-library/jest-dom": "^6.9.1",
"@testing-library/react": "^16.3.0",
"@type-challenges/utils": "0.1.1",
"@types/jest": "^29.5.2",
"@types/jest": "^30.0.0",
"@types/node": "^22.19.1",
"@types/react": "^19.2.7",
"@types/use-sync-external-store": "^1.5.0",
Expand All @@ -147,9 +148,8 @@
"eslint-plugin-testing-library": "7.13.5",
"globals": "^16.5.0",
"husky": "9.1.7",
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"lint-staged": "16.2.7",
"jest": "30.2.0",
"jest-environment-jsdom": "29.7.0", "lint-staged": "16.2.7",
"next": "16.0.5",
"prettier": "2.8.8",
"react": "^18.2.0",
Expand Down
1,454 changes: 875 additions & 579 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/unit/web-preset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function runTests(propertyName) {
const release = initFocus(fn) as () => void

target.emit(eventName)
expect(fn).toBeCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(1)

release()
target.emit(eventName)
expect(fn).toBeCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(1)
})

it(`should not trigger listener when ${propertyName} is falsy`, async () => {
Expand All @@ -79,13 +79,13 @@ function runTests(propertyName) {

target?.emit?.(eventName)

expect(fn).toBeCalledTimes(0)
expect(fn).toHaveBeenCalledTimes(0)

release()
if (target && target.emit) {
target.emit(eventName)
}
expect(fn).toBeCalledTimes(0)
expect(fn).toHaveBeenCalledTimes(0)
})
})
}
Expand Down
10 changes: 5 additions & 5 deletions test/use-swr-cache.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ describe('useSWR - cache provider', () => {
// revalidateOnFocus won't work
screen.getByText('1')
unmount()
expect(focusFn).toBeCalled()
expect(unsubscribeFocusFn).toBeCalledTimes(1)
expect(unsubscribeReconnectFn).toBeCalledTimes(1)
expect(focusFn).toHaveBeenCalled()
expect(unsubscribeFocusFn).toHaveBeenCalledTimes(1)
expect(unsubscribeReconnectFn).toHaveBeenCalledTimes(1)
})

it('should work with revalidateOnFocus', async () => {
Expand Down Expand Up @@ -336,9 +336,9 @@ describe('useSWR - cache provider', () => {
}

renderWithConfig(<Page />)
expect(createCacheProvider).toBeCalledTimes(1)
expect(createCacheProvider).toHaveBeenCalledTimes(1)
act(() => rerender({}))
expect(createCacheProvider).toBeCalledTimes(1)
expect(createCacheProvider).toHaveBeenCalledTimes(1)
})
})

Expand Down
6 changes: 3 additions & 3 deletions test/use-swr-error.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -589,11 +589,11 @@ describe('useSWR - error', () => {
}
renderWithConfig(<App />)
await act(() => sleep(320))
expect(fn).toBeCalledTimes(1)
expect(fn).toHaveBeenCalledTimes(1)
fireEvent.click(screen.getByText('toggle'))
await act(() => sleep(320))
expect(fn).toBeCalledTimes(2)
expect(fn).toHaveBeenCalledTimes(2)
await act(() => sleep(320))
expect(fn).toBeCalledTimes(3)
expect(fn).toHaveBeenCalledTimes(3)
})
})
6 changes: 3 additions & 3 deletions test/use-swr-immutable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ describe('useSWR - immutable', () => {
await sleep(20)

// `fetcher` should only be called twice, with each key.
expect(fetcher).toBeCalledTimes(2)
expect(fetcher).nthCalledWith(1, key + '0')
expect(fetcher).nthCalledWith(2, key + '1')
expect(fetcher).toHaveBeenCalledTimes(2)
expect(fetcher).toHaveBeenNthCalledWith(1, key + '0')
expect(fetcher).toHaveBeenNthCalledWith(2, key + '1')
})
})
28 changes: 14 additions & 14 deletions test/use-swr-infinite-preload.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ describe('useSWRInfinite - preload', () => {
}

preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

renderWithConfig(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('should avoid preloading the resource multiple times', async () => {
Expand All @@ -54,11 +54,11 @@ describe('useSWRInfinite - preload', () => {
preload(getKey(0), fetcher)
preload(getKey(0), fetcher)
preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

renderWithConfig(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('should be able to prealod resources in effects', async () => {
Expand All @@ -84,12 +84,12 @@ describe('useSWRInfinite - preload', () => {
}

renderWithConfig(<Page />)
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

fireEvent.click(screen.getByText('click'))

await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('preload the fetcher function with the suspense mode', async () => {
Expand All @@ -103,7 +103,7 @@ describe('useSWRInfinite - preload', () => {
}

preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

renderWithConfig(
<Suspense
Expand All @@ -117,8 +117,8 @@ describe('useSWRInfinite - preload', () => {
</Suspense>
)
await screen.findByText('data:foo')
expect(onRender).toBeCalledTimes(1)
expect(fetcher).toBeCalledTimes(1)
expect(onRender).toHaveBeenCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it.skip('avoid suspense waterfall by prefetching the resources', async () => {
Expand Down Expand Up @@ -222,18 +222,18 @@ describe('useSWRInfinite - preload', () => {
}

preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

const { rerender } = renderWithConfig(<Page />)
expect(onRender).toBeCalledTimes(1)
expect(onRender).toHaveBeenCalledTimes(1)
// rerender when the preloading is in-flight, and the deduping interval is over
await act(() => sleep(10))
rerender(<Page />)
expect(onRender).toBeCalledTimes(2)
expect(onRender).toHaveBeenCalledTimes(2)

await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)
expect(onRender).toBeCalledTimes(3)
expect(fetcher).toHaveBeenCalledTimes(1)
expect(onRender).toHaveBeenCalledTimes(3)
})

it('should pass serialize key to fetcher', async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/use-swr-infinite.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,7 @@ describe('useSWRInfinite', () => {

fireEvent.click(screen.getByText('mutate'))
await screen.findByText('data: foo-1')
expect(fetcher).toBeCalledTimes(2)
expect(fetcher).toHaveBeenCalledTimes(2)

expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']])
})
Expand Down Expand Up @@ -1268,7 +1268,7 @@ describe('useSWRInfinite', () => {

fireEvent.click(screen.getByText('mutate'))
await screen.findByText('data: foo-1')
expect(fetcher).toBeCalledTimes(2)
expect(fetcher).toHaveBeenCalledTimes(2)

expect(logger).toEqual([undefined, ['foo-0'], ['foo-1']])
})
Expand Down Expand Up @@ -1300,7 +1300,7 @@ describe('useSWRInfinite', () => {
await screen.findByText('data: foo-0')

fireEvent.click(screen.getByText('mutate'))
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

expect(logger).toEqual([undefined, ['foo-0']])
})
Expand Down
8 changes: 4 additions & 4 deletions test/use-swr-integration.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('useSWR', () => {
screen.getByText('hello,')

await screen.findByText('hello, SWR')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('should not call fetch function when revalidateOnMount is false', async () => {
Expand Down Expand Up @@ -154,7 +154,7 @@ describe('useSWR', () => {
screen.getByText(',')

await screen.findByText('SWR, SWR')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('should trigger the onSuccess event', async () => {
Expand Down Expand Up @@ -363,7 +363,7 @@ describe('useSWR', () => {
renderWithConfig(<Page />)

await screen.findByText('hello, Initial')
expect(fetcher).not.toBeCalled()
expect(fetcher).not.toHaveBeenCalled()
})

it('should revalidate even if fallbackData is provided', async () => {
Expand Down Expand Up @@ -421,7 +421,7 @@ describe('useSWR', () => {

renderWithConfig(<Page />)
screen.getByText('hello,')
expect(fetcher).toBeCalled()
expect(fetcher).toHaveBeenCalled()
await screen.findByText('hello, SWR')
})

Expand Down
4 changes: 2 additions & 2 deletions test/use-swr-key.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ describe('useSWR - key', () => {

renderWithConfig(<Page />)
await screen.findByText(`key-undefined`)
expect(fetcher).toBeCalledTimes(0)
expect(fetcher).toHaveBeenCalledTimes(0)
})

it('should cleanup state when key turns to empty', async () => {
Expand Down Expand Up @@ -222,6 +222,6 @@ describe('useSWR - key', () => {
await screen.findByText('data,data')

// Only 1 request since the keys are the same.
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})
})
2 changes: 1 addition & 1 deletion test/use-swr-loading.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('useSWR - loading', () => {
screen.getByText('validating,validating')
await nextTick()
screen.getByText('stopped,stopped')
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
})

it('should sync all validating states if errored', async () => {
Expand Down
12 changes: 6 additions & 6 deletions test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ describe('useSWR - local mutation', () => {

await act(() => sleep(150))
// fetcher result should be ignored
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
await screen.findByText('data: 1')
})

Expand Down Expand Up @@ -730,7 +730,7 @@ describe('useSWR - local mutation', () => {

// fetcher result should be ignored
await act(() => sleep(200))
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
await screen.findByText('data: 1')
})

Expand Down Expand Up @@ -773,7 +773,7 @@ describe('useSWR - local mutation', () => {

// fetcher result should be ignored
await act(() => sleep(100))
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)
screen.getByText('loading')

// mutate success
Expand Down Expand Up @@ -807,17 +807,17 @@ describe('useSWR - local mutation', () => {
renderWithConfig(<Page />)
screen.getByText('set ready')

expect(fetcher).toBeCalledTimes(0)
expect(fetcher).toHaveBeenCalledTimes(0)

// it should trigger the fetch
fireEvent.click(screen.getByText('set ready'))
await act(() => sleep(10))
expect(fetcher).toBeCalledTimes(1)
expect(fetcher).toHaveBeenCalledTimes(1)

// it should trigger the fetch again
fireEvent.click(screen.getByText('mutate'))
await act(() => sleep(10))
expect(fetcher).toBeCalledTimes(2)
expect(fetcher).toHaveBeenCalledTimes(2)
})

it('should reset isValidating after mutate', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/use-swr-middlewares.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ describe('useSWR - middleware', () => {
screen.getByText('hello,')
await nextTick()

expect(logger).toBeCalledWith(true)
expect(logger).toHaveBeenCalledWith(true)
screen.getByText('hello, world')
})
})
Loading
Loading