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

chore: simplify test coverage strategy #2909

Merged
merged 4 commits into from
Mar 10, 2024
Merged
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
3 changes: 1 addition & 2 deletions .github/workflows/test-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ jobs:

- name: Lint and test
env:
REACT_CANARY: 1
TEST_REACT_LEGACY: 1
run: |
pnpm clean
pnpm build
pnpm run-all-checks
pnpm test
pnpm test:build
28 changes: 28 additions & 0 deletions .github/workflows/test-legacy-react.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test React 17

on:
push:
branches:
- main
tags:
- v*
pull_request:

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install
uses: ./.github/workflows/install

- name: Test
env:
TEST_REACT_LEGACY: 1
run: |
pnpm clean
pnpm build
pnpm test
pnpm test:build
7 changes: 6 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ module.exports = {
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest']
},
coveragePathIgnorePatterns: ['/node_modules/', '/dist/', '/test/'],
coveragePathIgnorePatterns: [
'/node_modules/',
'/dist/',
'/test/',
'<rootDir>/src/_internal/utils/env.ts',
],
coverageReporters: ['text', 'html'],
reporters: [['github-actions', { silent: false }], 'summary']
}
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"csb:build": "pnpm build",
"clean": "rimraf ./dist && rimraf playwright-report test-result",
"watch": "pnpm -r run watch",
"build": "__SWR_TEST_REACT_LEGACY='' __SWR_TEST_SERVER='' bunchee --env=__SWR_TEST_REACT_LEGACY,__SWR_TEST_SERVER",
"build": "bunchee",
"build:e2e": "pnpm next build e2e/site",
"attw": "attw --pack",
"types:check": "tsc --noEmit",
Expand All @@ -115,8 +115,7 @@
"coverage": "jest --coverage",
"test-typing": "tsc --noEmit -p test/type/tsconfig.json && tsc --noEmit -p test/tsconfig.json",
"test": "jest",
"test-canary": "REACT_CANARY=1 jest",
"test:build": "__SWR_TEST_BUILD=1 jest --config jest.config.build.js",
"test:build": "jest --config jest.config.build.js",
"test:e2e": "playwright test",
"run-all-checks": "pnpm types:check && pnpm lint && pnpm test-typing"
},
Expand Down
6 changes: 2 additions & 4 deletions src/_internal/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React, { useEffect, useLayoutEffect } from 'react'
import { hasRequestAnimationFrame, isWindowDefined } from './helper'

export const IS_REACT_LEGACY =
process.env.__SWR_TEST_REACT_LEGACY || !React.useId
export const IS_REACT_LEGACY = !React.useId

export const IS_SERVER =
process.env.__SWR_TEST_SERVER || !isWindowDefined || 'Deno' in window
export const IS_SERVER = !isWindowDefined || 'Deno' in window

// Polyfill requestAnimationFrame
export const rAF = (
Expand Down
8 changes: 1 addition & 7 deletions test/use-swr-legacy-react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ jest.mock('react', () => jest.requireActual('react'))

async function withLegacyReact(runner: () => Promise<void>) {
await jest.isolateModulesAsync(async () => {
process.env.__SWR_TEST_REACT_LEGACY = '1'

try {
await runner()
} finally {
process.env.__SWR_TEST_REACT_LEGACY = ''
}
await runner()
})
}

Expand Down
92 changes: 48 additions & 44 deletions test/use-swr-server.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,56 @@ jest.mock('react', () => jest.requireActual('react'))

async function withServer(runner: () => Promise<void>) {
await jest.isolateModulesAsync(async () => {
process.env.__SWR_TEST_SERVER = '1'

try {
await runner()
} finally {
process.env.__SWR_TEST_SERVER = ''
}
await runner()
})
}

describe('useSWR - SSR', () => {
;(process.env.__SWR_TEST_BUILD ? it.skip : it)(
'should enable the IS_SERVER flag - suspense on server without fallback',
async () => {
await withServer(async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})

const useSWR = (await import('swr')).default

const key = Math.random().toString()

const Page = () => {
const { data } = useSWR(key, () => 'SWR', {
suspense: true
})
return <div>{data || 'empty'}</div>
}

render(
<ErrorBoundary
fallbackRender={({ error }) => {
console.error(error)
return <div>{error.message}</div>
}}
>
<Suspense>
<Page />
</Suspense>
</ErrorBoundary>
)

await screen.findByText(
'Fallback data is required when using Suspense in SSR.'
)
})
}
)
beforeAll(() => {
// Store the original window object
// @ts-expect-error
global.window.Deno = '1'

// Mock window to undefined
// delete global.window;
})

afterAll(() => {
// Restore window back to its original value
// @ts-expect-error
delete global.window.Deno
})
it('should enable the IS_SERVER flag - suspense on server without fallback', async () => {
await withServer(async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
jest.spyOn(console, 'error').mockImplementation(() => {})
const useSWR = (await import('swr')).default

const key = Math.random().toString()

const Page = () => {
const { data } = useSWR(key, () => 'SWR', {
suspense: true
})
return <div>{data || 'empty'}</div>
}

render(
<ErrorBoundary
fallbackRender={({ error }) => {
console.error(error)
return <div>{error.message}</div>
}}
>
<Suspense>
<Page />
</Suspense>
</ErrorBoundary>
)

await screen.findByText(
'Fallback data is required when using Suspense in SSR.'
)
})
})
})
7 changes: 6 additions & 1 deletion test/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ export const hydrateWithConfig = (
const TestSWRConfig = ({ children }: { children: React.ReactNode }) => (
<SWRConfig value={{ provider, ...config }}>{children}</SWRConfig>
)
return render(element, { container, wrapper: TestSWRConfig, hydrate: true })
return render(element, {
container,
wrapper: TestSWRConfig,
hydrate: true,
legacyRoot: process.env.TEST_REACT_LEGACY === '1'
})
}

export const mockVisibilityHidden = () => {
Expand Down
Loading