diff --git a/package.json b/package.json index 9d62f0fa..abac2f09 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "docz-theme-default": "1.2.0", "docz-utils": "2.3.0", "eslint": "7.28.0", - "kcd-scripts": "10.0.0", + "kcd-scripts": "11.1.0", "prettier": "^2.2.1", "react": "17.0.2", "react-dom": "^17.0.1", diff --git a/src/__tests__/defaultRenderer.pure.test.ts b/src/__tests__/defaultRenderer.pure.test.ts new file mode 100644 index 00000000..d0fe14ff --- /dev/null +++ b/src/__tests__/defaultRenderer.pure.test.ts @@ -0,0 +1,41 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +import { ReactHooksRenderer } from '../types/react' + +describe('default renderer', () => { + beforeEach(() => { + jest.resetModules() + }) + + test('should resolve native renderer as default renderer', () => { + const expectedRenderer = require('../native/pure') as ReactHooksRenderer + const actualRenderer = require('../pure') as ReactHooksRenderer + + expect(actualRenderer).toEqual(expectedRenderer) + }) + + test('should resolve dom renderer as default renderer', () => { + jest.doMock('react-test-renderer', () => { + throw new Error('missing dependency') + }) + + const expectedRenderer = require('../dom/pure') as ReactHooksRenderer + const actualRenderer = require('../pure') as ReactHooksRenderer + + expect(actualRenderer).toEqual(expectedRenderer) + }) + + test('should throw error if a default renderer cannot be resolved', () => { + jest.doMock('react-test-renderer', () => { + throw new Error('missing dependency') + }) + + jest.doMock('react-dom', () => { + throw new Error('missing dependency') + }) + + const expectedMessage = + "Could not auto-detect a React renderer. Are you sure you've installed one of the following\n - react-dom\n - react-test-renderer\nIf you are using a bundler, please update your imports to use a specific renderer.\nFor instructions see: https://react-hooks-testing-library.com/installation#being-specific" + + expect(() => require('../pure')).toThrowError(new Error(expectedMessage)) + }) +}) diff --git a/src/__tests__/defaultRenderer.test.ts b/src/__tests__/defaultRenderer.test.ts index 45d2e7ce..de38354a 100644 --- a/src/__tests__/defaultRenderer.test.ts +++ b/src/__tests__/defaultRenderer.test.ts @@ -1,41 +1,8 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -import { ReactHooksRenderer } from '../types/react' +import * as actualRenderer from '..' +import * as expectedRenderer from '../native' describe('default renderer', () => { - beforeEach(() => { - jest.resetModules() - }) - test('should resolve native renderer as default renderer', () => { - const expectedRenderer = require('../native/pure') as ReactHooksRenderer - const actualRenderer = require('..') as ReactHooksRenderer - expect(actualRenderer).toEqual(expectedRenderer) }) - - test('should resolve dom renderer as default renderer', () => { - jest.doMock('react-test-renderer', () => { - throw new Error('missing dependency') - }) - - const expectedRenderer = require('../dom/pure') as ReactHooksRenderer - const actualRenderer = require('..') as ReactHooksRenderer - - expect(actualRenderer).toEqual(expectedRenderer) - }) - - test('should throw error if a default renderer cannot be resolved', () => { - jest.doMock('react-test-renderer', () => { - throw new Error('missing dependency') - }) - - jest.doMock('react-dom', () => { - throw new Error('missing dependency') - }) - - const expectedMessage = - "Could not auto-detect a React renderer. Are you sure you've installed one of the following\n - react-dom\n - react-test-renderer\nIf you are using a bundler, please update your imports to use a specific renderer.\nFor instructions see: https://react-hooks-testing-library.com/installation#being-specific" - - expect(() => require('..')).toThrowError(new Error(expectedMessage)) - }) }) diff --git a/src/dom/__tests__/autoCleanup.disabled.test.ts b/src/dom/__tests__/autoCleanup.disabled.test.ts index 2c574b83..cd32a7ee 100644 --- a/src/dom/__tests__/autoCleanup.disabled.test.ts +++ b/src/dom/__tests__/autoCleanup.disabled.test.ts @@ -6,12 +6,8 @@ import { ReactHooksRenderer } from '../../types/react' // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (disabled) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) + process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/dom/__tests__/autoCleanup.noAfterEach.test.ts b/src/dom/__tests__/autoCleanup.noAfterEach.test.ts index 40b33f16..5f773d93 100644 --- a/src/dom/__tests__/autoCleanup.noAfterEach.test.ts +++ b/src/dom/__tests__/autoCleanup.noAfterEach.test.ts @@ -6,13 +6,9 @@ import { ReactHooksRenderer } from '../../types/react' // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (no afterEach) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/dom/__tests__/autoCleanup.noProcessEnv.test.ts b/src/dom/__tests__/autoCleanup.noProcessEnv.test.ts index f6adc8ad..35febc66 100644 --- a/src/dom/__tests__/autoCleanup.noProcessEnv.test.ts +++ b/src/dom/__tests__/autoCleanup.noProcessEnv.test.ts @@ -4,24 +4,15 @@ import { ReactHooksRenderer } from '../../types/react' // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks -describe('skip auto cleanup (no process.env) tests', () => { - const originalEnv = process.env +describe('auto cleanup (no process.env) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { + throw new Error('expected') } - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) - - afterAll(() => { - process.env = originalEnv - }) + } + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/dom/__tests__/autoCleanup.pure.test.ts b/src/dom/__tests__/autoCleanup.pure.test.ts index 1f84b87c..66c27a07 100644 --- a/src/dom/__tests__/autoCleanup.pure.test.ts +++ b/src/dom/__tests__/autoCleanup.pure.test.ts @@ -1,16 +1,10 @@ import { useEffect } from 'react' - -import { ReactHooksRenderer } from '../../types/react' +import { renderHook } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (pure) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - renderHook = (require('../pure') as ReactHooksRenderer).renderHook - }) test('first', () => { const hookWithCleanup = () => { diff --git a/src/dom/__tests__/errorHook.test.ts b/src/dom/__tests__/errorHook.test.ts index 6e6c0a38..8e97e5de 100644 --- a/src/dom/__tests__/errorHook.test.ts +++ b/src/dom/__tests__/errorHook.test.ts @@ -1,5 +1,5 @@ import { useState, useEffect } from 'react' -import { renderHook, act } from '..' +import { renderHook } from '..' describe('error hook tests', () => { function useError(throwError?: boolean) { diff --git a/src/dom/__tests__/errorSuppression.disabled.test.ts b/src/dom/__tests__/errorSuppression.disabled.test.ts index e1921f09..8cf200ab 100644 --- a/src/dom/__tests__/errorSuppression.disabled.test.ts +++ b/src/dom/__tests__/errorSuppression.disabled.test.ts @@ -2,11 +2,8 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (disabled) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' - require('..') - }) + process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/dom/__tests__/errorSuppression.noAfterEach.test.ts b/src/dom/__tests__/errorSuppression.noAfterEach.test.ts index c736020e..f83d068f 100644 --- a/src/dom/__tests__/errorSuppression.noAfterEach.test.ts +++ b/src/dom/__tests__/errorSuppression.noAfterEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noAfterEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - require('..') - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/dom/__tests__/errorSuppression.noBeforeEach.test.ts b/src/dom/__tests__/errorSuppression.noBeforeEach.test.ts index c3f2496f..609cab95 100644 --- a/src/dom/__tests__/errorSuppression.noBeforeEach.test.ts +++ b/src/dom/__tests__/errorSuppression.noBeforeEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noBeforeEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type - beforeEach = false - require('..') - }) + // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type + beforeEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/dom/__tests__/errorSuppression.noProcessEnv.test.ts b/src/dom/__tests__/errorSuppression.noProcessEnv.test.ts index 24a50f21..414aea26 100644 --- a/src/dom/__tests__/errorSuppression.noProcessEnv.test.ts +++ b/src/dom/__tests__/errorSuppression.noProcessEnv.test.ts @@ -1,22 +1,14 @@ // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks describe('error output suppression (no process.env) tests', () => { - const originalEnv = process.env const originalConsoleError = console.error - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { + throw new Error('expected') } - require('..') - }) - - afterAll(() => { - process.env = originalEnv - }) + } + require('..') test('should not patch console.error', () => { expect(console.error).not.toBe(originalConsoleError) diff --git a/src/dom/__tests__/errorSuppression.pure.test.ts b/src/dom/__tests__/errorSuppression.pure.test.ts index e60ec710..6e356cb6 100644 --- a/src/dom/__tests__/errorSuppression.pure.test.ts +++ b/src/dom/__tests__/errorSuppression.pure.test.ts @@ -1,16 +1,10 @@ -import { ReactHooksRenderer } from '../../types/react' +import { suppressErrorOutput } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (pure) tests', () => { const originalConsoleError = console.error - let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] - - beforeAll(() => { - suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput - }) - test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) }) diff --git a/src/dom/__tests__/errorSuppression.test.ts b/src/dom/__tests__/errorSuppression.test.ts index 69250f47..8a4b72ad 100644 --- a/src/dom/__tests__/errorSuppression.test.ts +++ b/src/dom/__tests__/errorSuppression.test.ts @@ -1,14 +1,11 @@ import { useEffect } from 'react' - -import { ReactHooksRenderer } from '../../types/react' +import { renderHook, act, suppressErrorOutput } from '..' describe('error output suppression tests', () => { + const consoleError = console.error + test('should not suppress relevant errors', () => { - const consoleError = console.error console.error = jest.fn() - - const { suppressErrorOutput } = require('..') as ReactHooksRenderer - try { const restoreConsole = suppressErrorOutput() @@ -28,8 +25,6 @@ describe('error output suppression tests', () => { }) test('should allow console.error to be mocked', async () => { - const { renderHook, act } = require('..') as ReactHooksRenderer - const consoleError = console.error console.error = jest.fn() try { diff --git a/src/native/__tests__/autoCleanup.disabled.test.ts b/src/native/__tests__/autoCleanup.disabled.test.ts index 2c574b83..cd32a7ee 100644 --- a/src/native/__tests__/autoCleanup.disabled.test.ts +++ b/src/native/__tests__/autoCleanup.disabled.test.ts @@ -6,12 +6,8 @@ import { ReactHooksRenderer } from '../../types/react' // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (disabled) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) + process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/native/__tests__/autoCleanup.noAfterEach.test.ts b/src/native/__tests__/autoCleanup.noAfterEach.test.ts index 40b33f16..5f773d93 100644 --- a/src/native/__tests__/autoCleanup.noAfterEach.test.ts +++ b/src/native/__tests__/autoCleanup.noAfterEach.test.ts @@ -6,13 +6,9 @@ import { ReactHooksRenderer } from '../../types/react' // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (no afterEach) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/native/__tests__/autoCleanup.noProcessEnv.test.ts b/src/native/__tests__/autoCleanup.noProcessEnv.test.ts index f6adc8ad..35febc66 100644 --- a/src/native/__tests__/autoCleanup.noProcessEnv.test.ts +++ b/src/native/__tests__/autoCleanup.noProcessEnv.test.ts @@ -4,24 +4,15 @@ import { ReactHooksRenderer } from '../../types/react' // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks -describe('skip auto cleanup (no process.env) tests', () => { - const originalEnv = process.env +describe('auto cleanup (no process.env) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { + throw new Error('expected') } - renderHook = (require('..') as ReactHooksRenderer).renderHook - }) - - afterAll(() => { - process.env = originalEnv - }) + } + const renderHook = (require('..') as ReactHooksRenderer).renderHook test('first', () => { const hookWithCleanup = () => { diff --git a/src/native/__tests__/autoCleanup.pure.test.ts b/src/native/__tests__/autoCleanup.pure.test.ts index 1f84b87c..66c27a07 100644 --- a/src/native/__tests__/autoCleanup.pure.test.ts +++ b/src/native/__tests__/autoCleanup.pure.test.ts @@ -1,16 +1,10 @@ import { useEffect } from 'react' - -import { ReactHooksRenderer } from '../../types/react' +import { renderHook } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks describe('skip auto cleanup (pure) tests', () => { let cleanupCalled = false - let renderHook: ReactHooksRenderer['renderHook'] - - beforeAll(() => { - renderHook = (require('../pure') as ReactHooksRenderer).renderHook - }) test('first', () => { const hookWithCleanup = () => { diff --git a/src/native/__tests__/errorHook.test.ts b/src/native/__tests__/errorHook.test.ts index 8399a50b..8e97e5de 100644 --- a/src/native/__tests__/errorHook.test.ts +++ b/src/native/__tests__/errorHook.test.ts @@ -1,5 +1,5 @@ import { useState, useEffect } from 'react' -import { renderHook, act } from '..' +import { renderHook } from '..' describe('error hook tests', () => { function useError(throwError?: boolean) { @@ -109,7 +109,7 @@ describe('error hook tests', () => { }) describe('effect', () => { - test('should raise effect error', () => { + test('this one - should raise effect error', () => { const { result } = renderHook(() => useEffectError(true)) expect(() => { @@ -117,7 +117,7 @@ describe('error hook tests', () => { }).toThrow(Error('expected')) }) - test('should capture effect error', () => { + test('this one - should capture effect error', () => { const { result } = renderHook(() => useEffectError(true)) expect(result.error).toEqual(Error('expected')) }) diff --git a/src/native/__tests__/errorSuppression.disabled.test.ts b/src/native/__tests__/errorSuppression.disabled.test.ts index e1921f09..8cf200ab 100644 --- a/src/native/__tests__/errorSuppression.disabled.test.ts +++ b/src/native/__tests__/errorSuppression.disabled.test.ts @@ -2,11 +2,8 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (disabled) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' - require('..') - }) + process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/native/__tests__/errorSuppression.noAfterEach.test.ts b/src/native/__tests__/errorSuppression.noAfterEach.test.ts index c736020e..f83d068f 100644 --- a/src/native/__tests__/errorSuppression.noAfterEach.test.ts +++ b/src/native/__tests__/errorSuppression.noAfterEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noAfterEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - require('..') - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/native/__tests__/errorSuppression.noBeforeEach.test.ts b/src/native/__tests__/errorSuppression.noBeforeEach.test.ts index c3f2496f..609cab95 100644 --- a/src/native/__tests__/errorSuppression.noBeforeEach.test.ts +++ b/src/native/__tests__/errorSuppression.noBeforeEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noBeforeEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type - beforeEach = false - require('..') - }) + // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type + beforeEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/native/__tests__/errorSuppression.noProcessEnv.test.ts b/src/native/__tests__/errorSuppression.noProcessEnv.test.ts index 24a50f21..414aea26 100644 --- a/src/native/__tests__/errorSuppression.noProcessEnv.test.ts +++ b/src/native/__tests__/errorSuppression.noProcessEnv.test.ts @@ -1,22 +1,14 @@ // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks describe('error output suppression (no process.env) tests', () => { - const originalEnv = process.env const originalConsoleError = console.error - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { + throw new Error('expected') } - require('..') - }) - - afterAll(() => { - process.env = originalEnv - }) + } + require('..') test('should not patch console.error', () => { expect(console.error).not.toBe(originalConsoleError) diff --git a/src/native/__tests__/errorSuppression.pure.test.ts b/src/native/__tests__/errorSuppression.pure.test.ts index e60ec710..6e356cb6 100644 --- a/src/native/__tests__/errorSuppression.pure.test.ts +++ b/src/native/__tests__/errorSuppression.pure.test.ts @@ -1,16 +1,10 @@ -import { ReactHooksRenderer } from '../../types/react' +import { suppressErrorOutput } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (pure) tests', () => { const originalConsoleError = console.error - let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] - - beforeAll(() => { - suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput - }) - test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) }) diff --git a/src/native/__tests__/errorSuppression.test.ts b/src/native/__tests__/errorSuppression.test.ts index 69250f47..a5cb4b79 100644 --- a/src/native/__tests__/errorSuppression.test.ts +++ b/src/native/__tests__/errorSuppression.test.ts @@ -1,14 +1,12 @@ import { useEffect } from 'react' - -import { ReactHooksRenderer } from '../../types/react' +import { renderHook, act, suppressErrorOutput } from '..' describe('error output suppression tests', () => { + const consoleError = console.error + test('should not suppress relevant errors', () => { - const consoleError = console.error console.error = jest.fn() - const { suppressErrorOutput } = require('..') as ReactHooksRenderer - try { const restoreConsole = suppressErrorOutput() @@ -28,10 +26,7 @@ describe('error output suppression tests', () => { }) test('should allow console.error to be mocked', async () => { - const { renderHook, act } = require('..') as ReactHooksRenderer - const consoleError = console.error console.error = jest.fn() - try { const { rerender, unmount } = renderHook( (stage) => { diff --git a/src/server/__tests__/autoCleanup.disabled.test.ts b/src/server/__tests__/autoCleanup.disabled.test.ts index a39e4e72..fd597168 100644 --- a/src/server/__tests__/autoCleanup.disabled.test.ts +++ b/src/server/__tests__/autoCleanup.disabled.test.ts @@ -9,12 +9,8 @@ describe('skip auto cleanup (disabled) tests', () => { ssr: false, hydrated: false } - let renderHook: ReactHooksServerRenderer['renderHook'] - - beforeAll(() => { - process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' - renderHook = (require('..') as ReactHooksServerRenderer).renderHook - }) + process.env.RHTL_SKIP_AUTO_CLEANUP = 'true' + const renderHook = (require('..') as ReactHooksServerRenderer).renderHook test('first', () => { const hookWithCleanup = (name: string) => { diff --git a/src/server/__tests__/autoCleanup.noAfterEach.test.ts b/src/server/__tests__/autoCleanup.noAfterEach.test.ts index 6468296b..d7ea75ac 100644 --- a/src/server/__tests__/autoCleanup.noAfterEach.test.ts +++ b/src/server/__tests__/autoCleanup.noAfterEach.test.ts @@ -9,13 +9,9 @@ describe('skip auto cleanup (no afterEach) tests', () => { ssr: false, hydrated: false } - let renderHook: ReactHooksServerRenderer['renderHook'] - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - renderHook = (require('..') as ReactHooksServerRenderer).renderHook - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + const renderHook = (require('..') as ReactHooksServerRenderer).renderHook test('first', () => { const hookWithCleanup = (name: string) => { diff --git a/src/server/__tests__/autoCleanup.noProcessEnv.test.ts b/src/server/__tests__/autoCleanup.noProcessEnv.test.ts index f734d73e..de8bf795 100644 --- a/src/server/__tests__/autoCleanup.noProcessEnv.test.ts +++ b/src/server/__tests__/autoCleanup.noProcessEnv.test.ts @@ -5,26 +5,17 @@ import { ReactHooksServerRenderer } from '../../types/react' // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks describe('skip auto cleanup (no process.env) tests', () => { - const originalEnv = process.env const cleanups: Record = { ssr: false, hydrated: false } - let renderHook: ReactHooksServerRenderer['renderHook'] - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_SKIP_AUTO_CLEANUP(): string | undefined { + throw new Error('expected') } - renderHook = (require('..') as ReactHooksServerRenderer).renderHook - }) - - afterAll(() => { - process.env = originalEnv - }) + } + const renderHook = (require('..') as ReactHooksServerRenderer).renderHook test('first', () => { const hookWithCleanup = (name: string) => { diff --git a/src/server/__tests__/autoCleanup.pure.test.ts b/src/server/__tests__/autoCleanup.pure.test.ts index 0044e17f..ecf8a72d 100644 --- a/src/server/__tests__/autoCleanup.pure.test.ts +++ b/src/server/__tests__/autoCleanup.pure.test.ts @@ -1,6 +1,5 @@ import { useEffect } from 'react' - -import { ReactHooksServerRenderer } from '../../types/react' +import { renderHook } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks @@ -9,11 +8,6 @@ describe('skip auto cleanup (pure) tests', () => { ssr: false, hydrated: false } - let renderHook: ReactHooksServerRenderer['renderHook'] - - beforeAll(() => { - renderHook = (require('../pure') as ReactHooksServerRenderer).renderHook - }) test('first', () => { const hookWithCleanup = (name: string) => { diff --git a/src/server/__tests__/errorHook.test.ts b/src/server/__tests__/errorHook.test.ts index 75925a98..f7977465 100644 --- a/src/server/__tests__/errorHook.test.ts +++ b/src/server/__tests__/errorHook.test.ts @@ -1,6 +1,6 @@ import { useState, useEffect } from 'react' -import { renderHook, act } from '..' +import { renderHook } from '..' describe('error hook tests', () => { function useError(throwError?: boolean) { diff --git a/src/server/__tests__/errorSuppression.disabled.test.ts b/src/server/__tests__/errorSuppression.disabled.test.ts index e1921f09..8cf200ab 100644 --- a/src/server/__tests__/errorSuppression.disabled.test.ts +++ b/src/server/__tests__/errorSuppression.disabled.test.ts @@ -2,11 +2,8 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (disabled) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' - require('..') - }) + process.env.RHTL_DISABLE_ERROR_FILTERING = 'true' + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/server/__tests__/errorSuppression.noAfterEach.test.ts b/src/server/__tests__/errorSuppression.noAfterEach.test.ts index c736020e..f83d068f 100644 --- a/src/server/__tests__/errorSuppression.noAfterEach.test.ts +++ b/src/server/__tests__/errorSuppression.noAfterEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noAfterEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type - afterEach = false - require('..') - }) + // @ts-expect-error Turning off AfterEach -- ignore Jest LifeCycle Type + afterEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/server/__tests__/errorSuppression.noBeforeEach.test.ts b/src/server/__tests__/errorSuppression.noBeforeEach.test.ts index c3f2496f..609cab95 100644 --- a/src/server/__tests__/errorSuppression.noBeforeEach.test.ts +++ b/src/server/__tests__/errorSuppression.noBeforeEach.test.ts @@ -2,12 +2,9 @@ // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (noBeforeEach) tests', () => { const originalConsoleError = console.error - - beforeAll(() => { - // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type - beforeEach = false - require('..') - }) + // @ts-expect-error Turning off BeforeEach -- ignore Jest LifeCycle Type + beforeEach = false + require('..') test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) diff --git a/src/server/__tests__/errorSuppression.noProcessEnv.test.ts b/src/server/__tests__/errorSuppression.noProcessEnv.test.ts index 24a50f21..414aea26 100644 --- a/src/server/__tests__/errorSuppression.noProcessEnv.test.ts +++ b/src/server/__tests__/errorSuppression.noProcessEnv.test.ts @@ -1,22 +1,14 @@ // This verifies that if process.env is unavailable // then we still auto-wire up the afterEach for folks describe('error output suppression (no process.env) tests', () => { - const originalEnv = process.env const originalConsoleError = console.error - - beforeAll(() => { - process.env = { - ...process.env, - get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { - throw new Error('expected') - } + process.env = { + ...process.env, + get RHTL_DISABLE_ERROR_FILTERING(): string | undefined { + throw new Error('expected') } - require('..') - }) - - afterAll(() => { - process.env = originalEnv - }) + } + require('..') test('should not patch console.error', () => { expect(console.error).not.toBe(originalConsoleError) diff --git a/src/server/__tests__/errorSuppression.pure.test.ts b/src/server/__tests__/errorSuppression.pure.test.ts index e60ec710..6e356cb6 100644 --- a/src/server/__tests__/errorSuppression.pure.test.ts +++ b/src/server/__tests__/errorSuppression.pure.test.ts @@ -1,16 +1,10 @@ -import { ReactHooksRenderer } from '../../types/react' +import { suppressErrorOutput } from '../pure' // This verifies that if pure imports are used // then we DON'T auto-wire up the afterEach for folks describe('error output suppression (pure) tests', () => { const originalConsoleError = console.error - let suppressErrorOutput!: ReactHooksRenderer['suppressErrorOutput'] - - beforeAll(() => { - suppressErrorOutput = (require('../pure') as ReactHooksRenderer).suppressErrorOutput - }) - test('should not patch console.error', () => { expect(console.error).toBe(originalConsoleError) }) diff --git a/src/server/__tests__/errorSuppression.test.ts b/src/server/__tests__/errorSuppression.test.ts index e4492756..d97f9735 100644 --- a/src/server/__tests__/errorSuppression.test.ts +++ b/src/server/__tests__/errorSuppression.test.ts @@ -1,14 +1,12 @@ import { useEffect } from 'react' - -import { ReactHooksServerRenderer } from '../../types/react' +import { renderHook, act, suppressErrorOutput } from '..' describe('error output suppression tests', () => { + const consoleError = console.error + test('should not suppress relevant errors', () => { - const consoleError = console.error console.error = jest.fn() - const { suppressErrorOutput } = require('..') as ReactHooksServerRenderer - try { const restoreConsole = suppressErrorOutput() @@ -28,10 +26,7 @@ describe('error output suppression tests', () => { }) test('should allow console.error to be mocked', async () => { - const { renderHook, act } = require('..') as ReactHooksServerRenderer - const consoleError = console.error console.error = jest.fn() - try { const { hydrate, rerender, unmount } = renderHook( (stage) => {