Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit b5a3029

Browse files
chore: Improve typings (#405)
1 parent 4d93293 commit b5a3029

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

lib/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import { injectStylesBeforeElement } from './utils'
55

66
const rootId = 'cypress-root'
77

8-
// @ts-ignore
98
const isComponentSpec = () => Cypress.spec.specType === 'component'
109

1110
function checkMountModeEnabled() {
12-
// @ts-ignore
1311
if (!isComponentSpec()) {
1412
throw new Error(
1513
`In order to use mount or unmount functions please place the spec in component folder`,
@@ -68,7 +66,7 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
6866
})
6967
.then(injectStyles(options))
7068
.then(() => {
71-
const document = cy.state('document')
69+
const document = cy.state('document') as Document
7270
const reactDomToUse = options.ReactDom || ReactDOM
7371

7472
const el = document.getElementById(rootId)
@@ -113,7 +111,10 @@ export const mount = (jsx: React.ReactElement, options: MountOptions = {}) => {
113111
logInstance.set('consoleProps', () => logConsoleProps)
114112

115113
if (el.children.length) {
116-
logInstance.set('$el', el.children.item(0))
114+
logInstance.set(
115+
'$el',
116+
(el.children.item(0) as unknown) as JQuery<HTMLElement>,
117+
)
117118
}
118119
}
119120

@@ -168,8 +169,8 @@ export const unmount = () => {
168169

169170
// mounting hooks inside a test component mostly copied from
170171
// https://github.com/testing-library/react-hooks-testing-library/blob/master/src/pure.js
171-
function resultContainer() {
172-
let value: any = null
172+
function resultContainer<T>() {
173+
let value: T | undefined | null = null
173174
let error: Error | null = null
174175
const resolvers: any[] = []
175176

@@ -185,7 +186,7 @@ function resultContainer() {
185186
},
186187
}
187188

188-
const updateResult = (val: any, err: Error | null = null) => {
189+
const updateResult = (val: T | undefined, err: Error | null = null) => {
189190
value = val
190191
error = err
191192
resolvers.splice(0, resolvers.length).forEach(resolve => resolve())
@@ -196,13 +197,18 @@ function resultContainer() {
196197
addResolver: (resolver: any) => {
197198
resolvers.push(resolver)
198199
},
199-
setValue: (val: any) => updateResult(val),
200+
setValue: (val: T) => updateResult(val),
200201
setError: (err: Error) => updateResult(undefined, err),
201202
}
202203
}
203204

204-
// @ts-ignore
205-
function TestHook({ callback, onError, children }) {
205+
type TestHookProps = {
206+
callback: () => void
207+
onError: (e: Error) => void
208+
children: (...args: any[]) => any
209+
}
210+
211+
function TestHook({ callback, onError, children }: TestHookProps) {
206212
try {
207213
children(callback())
208214
} catch (err) {
@@ -225,7 +231,7 @@ function TestHook({ callback, onError, children }) {
225231
*
226232
* @see https://github.com/bahmutov/cypress-react-unit-test#advanced-examples
227233
*/
228-
export const mountHook = (hookFn: Function) => {
234+
export const mountHook = (hookFn: (...args: any[]) => any) => {
229235
const { result, setValue, setError } = resultContainer()
230236

231237
return mount(

0 commit comments

Comments
 (0)