Skip to content

Commit 3ea57ca

Browse files
authored
Merge 48cf918 into 5bc9364
2 parents 5bc9364 + 48cf918 commit 3ea57ca

15 files changed

+336
-207
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"import/prefer-default-export": "off",
6666
"import/no-unassigned-import": "off",
6767
"import/no-useless-path-segments": "off",
68-
"no-console": "off"
68+
"no-console": "off",
69+
"@typescript-eslint/no-non-null-assertion": "off"
6970
}
7071
},
7172
"eslintIgnore": [

src/__tests__/config.js renamed to src/__tests__/config.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {configure, getConfig} from '../config'
2+
import {Config} from '../../types/config'
23

34
describe('configuration API', () => {
4-
let originalConfig
5+
let originalConfig: Config
56
beforeEach(() => {
67
// Grab the existing configuration so we can restore
78
// it at the end of the test
@@ -15,10 +16,6 @@ describe('configuration API', () => {
1516
configure(originalConfig)
1617
})
1718

18-
beforeEach(() => {
19-
configure({other: 123})
20-
})
21-
2219
describe('getConfig', () => {
2320
test('returns existing configuration', () => {
2421
const conf = getConfig()

src/__tests__/helpers.js renamed to src/__tests__/helpers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ test('returns global document if exists', () => {
1616
describe('window retrieval throws when given something other than a node', () => {
1717
test('Promise as node', () => {
1818
expect(() =>
19+
// @ts-expect-error using a promise will trhow a specific error
1920
getWindowFromNode(new Promise(jest.fn())),
2021
).toThrowErrorMatchingInlineSnapshot(
2122
`"It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?"`,
2223
)
2324
})
2425
test('unknown as node', () => {
26+
// @ts-expect-error using an object will throw a specific error
2527
expect(() => getWindowFromNode({})).toThrowErrorMatchingInlineSnapshot(
2628
`"Unable to find the \\"window\\" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new"`,
2729
)
@@ -37,16 +39,19 @@ describe('query container validation throws when validation fails', () => {
3739
)
3840
})
3941
test('null as container', () => {
42+
// @ts-expect-error passing a wrong container will throw an error
4043
expect(() => checkContainerType(null)).toThrowErrorMatchingInlineSnapshot(
4144
`"Expected container to be an Element, a Document or a DocumentFragment but got null."`,
4245
)
4346
})
4447
test('array as container', () => {
48+
// @ts-expect-error passing a wrong container will throw an error
4549
expect(() => checkContainerType([])).toThrowErrorMatchingInlineSnapshot(
4650
`"Expected container to be an Element, a Document or a DocumentFragment but got Array."`,
4751
)
4852
})
4953
test('object as container', () => {
54+
// @ts-expect-error passing a wrong container will throw an error
5055
expect(() => checkContainerType({})).toThrowErrorMatchingInlineSnapshot(
5156
`"Expected container to be an Element, a Document or a DocumentFragment but got Object."`,
5257
)
@@ -61,7 +66,9 @@ test('should always use realTimers before using callback when timers are faked w
6166
runWithRealTimers(() => {
6267
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
6368
})
69+
// @ts-expect-error if we are using logacy timers
6470
expect(globalObj.setTimeout._isMockFunction).toBe(true)
71+
// @ts-expect-error if we are using logacy timers
6572
expect(globalObj.setTimeout.clock).toBeUndefined()
6673

6774
jest.useRealTimers()
@@ -71,19 +78,22 @@ test('should always use realTimers before using callback when timers are faked w
7178
runWithRealTimers(() => {
7279
expect(originalSetTimeout).toEqual(globalObj.setTimeout)
7380
})
81+
// @ts-expect-error if we are using modern timers
7482
expect(globalObj.setTimeout._isMockFunction).toBeUndefined()
83+
// @ts-expect-error if we are using modern timers
7584
expect(globalObj.setTimeout.clock).toBeDefined()
7685
})
7786

7887
test('should not use realTimers when timers are not faked with useFakeTimers', () => {
7988
const originalSetTimeout = globalObj.setTimeout
8089

8190
// useFakeTimers is not used, timers are faked in some other way
82-
const fakedSetTimeout = callback => {
91+
const fakedSetTimeout = (callback: () => void) => {
8392
callback()
8493
}
8594
fakedSetTimeout.clock = jest.fn()
8695

96+
//@ts-expect-error override the default setTimeout with a fake timer
8797
globalObj.setTimeout = fakedSetTimeout
8898

8999
runWithRealTimers(() => {
File renamed without changes.

src/__tests__/matches.js renamed to src/__tests__/matches.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {fuzzyMatches, matches} from '../matches'
33
// unit tests for text match utils
44

55
const node = null
6-
const normalizer = str => str
6+
const normalizer = (text: string) => text
77

88
test('matchers accept strings', () => {
99
expect(matches('ABC', node, 'ABC', normalizer)).toBe(true)
@@ -39,3 +39,8 @@ test('matchers throw on invalid matcher inputs', () => {
3939
`"It looks like undefined was passed instead of a matcher. Did you do something like getByText(undefined)?"`,
4040
)
4141
})
42+
43+
test('should use matchers with numbers', () => {
44+
expect(matches('1234', node, 1234, normalizer)).toBe(true)
45+
expect(fuzzyMatches('test1234', node, 1234, normalizer)).toBe(true)
46+
})

src/__tests__/pretty-dom.js renamed to src/__tests__/pretty-dom.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ beforeEach(() => {
99
})
1010

1111
afterEach(() => {
12-
console.log.mockRestore()
12+
;(console.log as jest.Mock).mockRestore()
1313
})
1414

1515
test('prettyDOM prints out the given DOM element tree', () => {
@@ -38,6 +38,7 @@ test('prettyDOM defaults to document.body', () => {
3838
`
3939
renderIntoDocument('<div>Hello World!</div>')
4040
expect(prettyDOM()).toMatchInlineSnapshot(defaultInlineSnapshot)
41+
//@ts-expect-error js check, should print the document.body
4142
expect(prettyDOM(null)).toMatchInlineSnapshot(defaultInlineSnapshot)
4243
})
4344

@@ -54,7 +55,8 @@ test('logDOM logs prettyDOM to the console', () => {
5455
const {container} = render('<div>Hello World!</div>')
5556
logDOM(container)
5657
expect(console.log).toHaveBeenCalledTimes(1)
57-
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
58+
expect(((console.log as jest.Mock).mock.calls[0] as string[])[0])
59+
.toMatchInlineSnapshot(`
5860
"<div>
5961
<div>
6062
Hello World!
@@ -64,7 +66,7 @@ test('logDOM logs prettyDOM to the console', () => {
6466
})
6567

6668
test('logDOM logs prettyDOM with code frame to the console', () => {
67-
getUserCodeFrame.mockImplementationOnce(
69+
;(getUserCodeFrame as jest.Mock).mockImplementationOnce(
6870
() => `"/home/john/projects/sample-error/error-example.js:7:14
6971
5 | document.createTextNode('Hello World!')
7072
6 | )
@@ -76,7 +78,8 @@ test('logDOM logs prettyDOM with code frame to the console', () => {
7678
const {container} = render('<div>Hello World!</div>')
7779
logDOM(container)
7880
expect(console.log).toHaveBeenCalledTimes(1)
79-
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
81+
expect(((console.log as jest.Mock).mock.calls[0] as string[])[0])
82+
.toMatchInlineSnapshot(`
8083
"<div>
8184
<div>
8285
Hello World!
@@ -95,16 +98,19 @@ test('logDOM logs prettyDOM with code frame to the console', () => {
9598

9699
describe('prettyDOM fails with first parameter without outerHTML field', () => {
97100
test('with array', () => {
101+
// @ts-expect-error use an array as arg
98102
expect(() => prettyDOM(['outerHTML'])).toThrowErrorMatchingInlineSnapshot(
99103
`"Expected an element or document but got Array"`,
100104
)
101105
})
102106
test('with number', () => {
107+
// @ts-expect-error use a number as arg
103108
expect(() => prettyDOM(1)).toThrowErrorMatchingInlineSnapshot(
104109
`"Expected an element or document but got number"`,
105110
)
106111
})
107112
test('with object', () => {
113+
// @ts-expect-error use an object as arg
108114
expect(() => prettyDOM({})).toThrowErrorMatchingInlineSnapshot(
109115
`"Expected an element or document but got Object"`,
110116
)

0 commit comments

Comments
 (0)