Skip to content

Commit

Permalink
chore(test): fix various test typings after moving to @jest/globals
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored and rexxars committed Feb 14, 2024
1 parent 23add76 commit 79f89c8
Show file tree
Hide file tree
Showing 49 changed files with 177 additions and 180 deletions.
11 changes: 3 additions & 8 deletions packages/@sanity/cli/test/shared/describe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ import {describe, test} from '@jest/globals'

import {cliUserToken, hasBuiltCli} from './environment'

// From jest typings, not exposed by jest
interface DoneCallback {
(...args: unknown[]): unknown
fail(error?: string | {message: string}): unknown
}

type ProvidesCallback = ((cb: DoneCallback) => void | undefined) | (() => Promise<unknown>)
type ProvidesCallback = () => Promise<unknown>

// only run cli tests if we have an environment variable set with the cli auth token
export const describeCliTest = cliUserToken && hasBuiltCli ? describe : describe.skip
export const describeCliTest: typeof describe.skip =
cliUserToken && hasBuiltCli ? describe : describe.skip

// test.concurrent() runs even if the parent describe is skipped, so we need to wrap it as well
export const testConcurrent = (name: string, testFn: ProvidesCallback, timeout = 30000): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ test('all permutations', () => {
del('corge'),
]

expect(mutations).toEqual<Mutation[]>([
expect(mutations).toEqual([
{
type: 'create',
document: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
/* eslint-disable simple-import-sort/imports */
// Note: for some reason, this needs to be imported before the mocked module
import {afterEach, describe, expect, it, jest} from '@jest/globals'
/* eslint-enable simple-import-sort/imports */

import {SanityEncoder} from '@bjoerge/mutiny'

import {type Mutation, type Transaction} from '../../../mutations'
import {toSanityMutations, type TransactionPayload} from '../toSanityMutations'

jest.mock('@bjoerge/mutiny', () => {
const actual = jest.requireActual('@bjoerge/mutiny')
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actual = jest.requireActual<typeof import('@bjoerge/mutiny')>('@bjoerge/mutiny')
return {
...actual,
SanityEncoder: {
encode: jest.fn().mockImplementation(actual.SanityEncoder.encode),
...actual.SanityEncoder.encode,
encode: jest
.fn<typeof actual.SanityEncoder.encode>()
.mockImplementation(actual.SanityEncoder.encode),
},
}
})
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/schema/test/validation/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('Validation test', () => {

const invalidObjectResult = validation.get('invalidObject')
const problems = flatten(
invalidObjectResult.fields[0].of[0].of.map((item) => item._problems),
invalidObjectResult.fields[0].of[0].of.map((item: {_problems: unknown[]}) => item._problems),
).filter(Boolean)
expect(problems).toHaveLength(7)
expect(problems[0]).toMatchObject({
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/alias.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/array.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/block.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/boolean.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/date.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/datetime.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/document.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/email.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
import {type BooleanDefinition, type EmailDefinition} from '../definition'
import {defineType} from '../types'
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/file.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/generic.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/geopoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/image.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/number.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/object.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/reference.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/slug.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/types/src/schema/test/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, it } from '@jest/globals';
import {describe, it} from '@jest/globals'
/* eslint-disable @typescript-eslint/no-unused-vars */
/**
* Some of these tests have no expect statement;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {it, expect} from '@jest/globals'
import {isDeepEmpty} from '../isDeepEmpty'

it('returns true for undefined', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/@sanity/util/test/PathUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '@jest/globals';
import {expect, test} from '@jest/globals'
/* eslint-disable max-nested-callbacks, @typescript-eslint/ban-ts-comment */
import {fromString, get, resolveKeyedPath, toString} from '../src/paths'

Expand Down
1 change: 0 additions & 1 deletion packages/@sanity/util/test/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { expect, test } from '@jest/globals';
/* eslint-disable no-sync */
import {expect, test} from '@jest/globals'
import * as fs from 'fs'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {type SchemaTypeDefinition} from '@sanity/types'

import {definePlugin} from '../definePlugin'
import {resolveConfigProperty} from '../resolveConfigProperty'
import {type AsyncConfigPropertyReducer, type ConfigPropertyReducer} from '../types'

describe('resolveConfigProperty', () => {
it('traverses configs and returns a resolved configuration property', () => {
Expand Down Expand Up @@ -50,9 +51,11 @@ describe('resolveConfigProperty', () => {
const context = {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const reducer = jest.fn((prev, config, _context) => {
return [...prev, ...config.schema.types]
})
const reducer = jest.fn<ConfigPropertyReducer<SchemaTypeDefinition[], unknown>>(
(prev, config, _context) => {
return [...prev, ...((config.schema?.types || []) as SchemaTypeDefinition[])]
},
)

const result = resolveConfigProperty({
config: {
Expand Down Expand Up @@ -119,10 +122,12 @@ describe('resolveConfigProperty', () => {
const context = {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const asyncReducer = jest.fn(async (prev, config, _context) => {
await new Promise((resolve) => setTimeout(resolve, 0))
return [...prev, ...config.schema.types]
})
const asyncReducer = jest.fn<AsyncConfigPropertyReducer<SchemaTypeDefinition[], unknown>>(
async (prev, config, _context) => {
await new Promise((resolve) => setTimeout(resolve, 0))
return [...prev, ...((config.schema?.types || []) as SchemaTypeDefinition[])]
},
)

const result = await resolveConfigProperty({
config: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(({document}) => document?.title !== 'Hello world'),
checkProperty: jest.fn<ConditionalPropertyCallback>(
({document}) => document?.title !== 'Hello world',
),
...DEFAULT_PROPS,
}),
{wrapper: TestWrapper},
Expand All @@ -105,7 +107,9 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(({document}) => Boolean(document?.isPublished)),
checkProperty: jest.fn<ConditionalPropertyCallback>(({document}) =>
Boolean(document?.isPublished),
),
...DEFAULT_PROPS,
}),
{wrapper: TestWrapper},
Expand Down Expand Up @@ -135,7 +139,7 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(({value}) => value === 'test value'),
checkProperty: jest.fn<ConditionalPropertyCallback>(({value}) => value === 'test value'),
...DEFAULT_PROPS,
value: 'test value',
}),
Expand All @@ -149,7 +153,7 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(({value}) => value === 'test'),
checkProperty: jest.fn<ConditionalPropertyCallback>(({value}) => value === 'test'),
...DEFAULT_PROPS,
value: 'test value',
}),
Expand All @@ -163,7 +167,7 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(
checkProperty: jest.fn<ConditionalPropertyCallback>(
({currentUser}) => !currentUser?.roles.some((role) => role.name === 'developer'),
),
...DEFAULT_PROPS,
Expand All @@ -179,7 +183,7 @@ describe('Conditional property resolver', () => {
const {result} = renderHook(
() =>
useConditionalProperty({
checkProperty: jest.fn(({currentUser}) =>
checkProperty: jest.fn<ConditionalPropertyCallback>(({currentUser}) =>
Boolean(currentUser?.roles.some((role) => role.name === 'administrator')),
),
...DEFAULT_PROPS,
Expand All @@ -199,7 +203,9 @@ describe('Conditional property resolver', () => {
document: dummyDocument,
value: dummyDocument.venue.address,
parent: dummyDocument.venue,
checkProperty: jest.fn(({parent}) => Boolean(parent.location)),
checkProperty: jest.fn<ConditionalPropertyCallback>(({parent}) =>
Boolean(parent.location),
),
}),
{wrapper: TestWrapper},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import {describe, expect, jest, test} from '@jest/globals'
import {type SanityClient} from '@sanity/client'
import {act, waitForElementToBeRemoved, within} from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import {type Observable, of} from 'rxjs'
import {of} from 'rxjs'

import {renderCrossDatasetReferenceInput} from '../../../../../../test/form'
import {createMockSanityClient} from '../../../../../../test/mocks/mockSanityClient'
import {createTestProvider} from '../../../../../../test/testUtils/TestProvider'
import {defineConfig} from '../../../../config'
import {CrossDatasetReferenceInput} from '../CrossDatasetReferenceInput'
import {type CrossDatasetSearchHit} from '../types'
import {featureDisabledRequest, featureEnabledRequest} from './mocks'

const AVAILABLE = {
Expand Down Expand Up @@ -79,9 +78,9 @@ describe('render states', () => {
const client = createMockSanityClient({requests: {'/features': featureEnabledRequest}})
const TestProvider = await createWrapperComponent(client as any)

const getReferenceInfo = jest.fn().mockReturnValue(
const getReferenceInfo = jest.fn(() =>
of({
_id: 'foo',
id: 'foo',
type: 'product',
availability: AVAILABLE,
preview: {
Expand Down Expand Up @@ -130,9 +129,9 @@ describe('render states', () => {
const client = createMockSanityClient({requests: {'/features': featureEnabledRequest}})
const TestProvider = await createWrapperComponent(client as any)

const getReferenceInfo = jest.fn().mockReturnValue(
const getReferenceInfo = jest.fn(() =>
of({
_id: 'foo',
id: 'foo',
type: 'product',
availability: AVAILABLE,
preview: {
Expand Down Expand Up @@ -183,9 +182,9 @@ describe('render states', () => {
const client = createMockSanityClient({requests: {'/features': featureDisabledRequest}})
const TestProvider = await createWrapperComponent(client as any)

const getReferenceInfo = jest.fn().mockReturnValue(
const getReferenceInfo = jest.fn(() =>
of({
_id: 'foo',
id: 'foo',
type: 'product',
availability: AVAILABLE,
preview: {
Expand Down Expand Up @@ -237,7 +236,7 @@ describe('user interaction happy paths', () => {
const client = createMockSanityClient({requests: {'/features': featureEnabledRequest}})
const TestProvider = await createWrapperComponent(client as any)

const handleSearch = jest.fn<Observable<CrossDatasetSearchHit[]>, [string]>().mockReturnValue(
const handleSearch = jest.fn(() =>
of([
{id: 'one', type: 'product', published: {_id: 'one', _type: 'product'}},
{id: 'two', type: 'product', published: {_id: 'two', _type: 'product'}},
Expand Down Expand Up @@ -325,7 +324,7 @@ describe('user interaction happy paths', () => {
const client = createMockSanityClient({requests: {'/features': featureEnabledRequest}})
const TestProvider = await createWrapperComponent(client as any)

const handleSearch = jest.fn<Observable<CrossDatasetSearchHit[]>, [string]>().mockReturnValue(
const handleSearch = jest.fn(() =>
of([
{id: 'one', type: 'product', published: {_id: 'one', _type: 'product'}},
{id: 'two', type: 'product', published: {_id: 'two', _type: 'product'}},
Expand All @@ -349,7 +348,7 @@ describe('user interaction happy paths', () => {
_projectId: 'mock-project-id',
}

const {onChange, onPathFocus, rerender, result} = await renderCrossDatasetReferenceInput({
const {onChange, onPathFocus, result} = await renderCrossDatasetReferenceInput({
getReferenceInfo,
onSearch: handleSearch,

Expand Down Expand Up @@ -385,11 +384,11 @@ describe('user interaction happy paths', () => {
expect(onPathFocus).toHaveBeenCalledTimes(1)
expect(onPathFocus).toHaveBeenCalledWith(['_ref'])

rerender((inputProps) => (
<TestProvider>
<CrossDatasetReferenceInput {...inputProps} focusPath={['_ref']} />
</TestProvider>
))
// rerender((inputProps) => (
// <TestProvider>
// <CrossDatasetReferenceInput {...inputProps} focusPath={['_ref']} />
// </TestProvider>
// ))

// rerender({
// focusPath: ['_ref'],
Expand Down
Loading

0 comments on commit 79f89c8

Please sign in to comment.