Skip to content

Commit

Permalink
chore: Remove pkg/driver @ts-nocheck part 1 (#19353)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Henkes <mjhenkes@gmail.com>
Co-authored-by: Ryan Manuel <ryanm@cypress.io>
  • Loading branch information
3 people authored Dec 20, 2021
1 parent b3d114e commit 01f0261
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 119 deletions.
11 changes: 11 additions & 0 deletions packages/driver/patches/unfetch+4.1.0.dev.patch
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ index 57a2761..0000000
@@ -1 +0,0 @@
-{"version":3,"file":"unfetch.umd.js","sources":["../src/index.mjs"],"sourcesContent":["export default function(url, options) {\n\toptions = options || {};\n\treturn new Promise( (resolve, reject) => {\n\t\tconst request = new XMLHttpRequest();\n\t\tconst keys = [];\n\t\tconst all = [];\n\t\tconst headers = {};\n\n\t\tconst response = () => ({\n\t\t\tok: (request.status/100|0) == 2,\t\t// 200-299\n\t\t\tstatusText: request.statusText,\n\t\t\tstatus: request.status,\n\t\t\turl: request.responseURL,\n\t\t\ttext: () => Promise.resolve(request.responseText),\n\t\t\tjson: () => Promise.resolve(JSON.parse(request.responseText)),\n\t\t\tblob: () => Promise.resolve(new Blob([request.response])),\n\t\t\tclone: response,\n\t\t\theaders: {\n\t\t\t\tkeys: () => keys,\n\t\t\t\tentries: () => all,\n\t\t\t\tget: n => headers[n.toLowerCase()],\n\t\t\t\thas: n => n.toLowerCase() in headers\n\t\t\t}\n\t\t});\n\n\t\trequest.open(options.method || 'get', url, true);\n\n\t\trequest.onload = () => {\n\t\t\trequest.getAllResponseHeaders().replace(/^(.*?):[^\\S\\n]*([\\s\\S]*?)$/gm, (m, key, value) => {\n\t\t\t\tkeys.push(key = key.toLowerCase());\n\t\t\t\tall.push([key, value]);\n\t\t\t\theaders[key] = headers[key] ? `${headers[key]},${value}` : value;\n\t\t\t});\n\t\t\tresolve(response());\n\t\t};\n\n\t\trequest.onerror = reject;\n\n\t\trequest.withCredentials = options.credentials=='include';\n\n\t\tfor (const i in options.headers) {\n\t\t\trequest.setRequestHeader(i, options.headers[i]);\n\t\t}\n\n\t\trequest.send(options.body || null);\n\t});\n}\n"],"names":["url","options","Promise","resolve","reject","request","XMLHttpRequest","keys","all","headers","response","ok","status","statusText","responseURL","text","responseText","json","JSON","parse","blob","Blob","clone","entries","get","n","toLowerCase","has","const","i","open","method","onload","getAllResponseHeaders","replace","m","key","value","push","onerror","withCredentials","credentials","setRequestHeader","send","body"],"mappings":"6KAAe,SAASA,EAAKC,UAC5BA,EAAUA,GAAW,GACd,IAAIC,iBAAUC,EAASC,OACvBC,EAAU,IAAIC,eACdC,EAAO,GACPC,EAAM,GACNC,EAAU,GAEVC,oBACLC,GAA8B,IAAzBN,EAAQO,OAAO,IAAI,GACxBC,WAAYR,EAAQQ,WACpBD,OAAQP,EAAQO,OAChBZ,IAAKK,EAAQS,YACbC,uBAAYb,QAAQC,QAAQE,EAAQW,eACpCC,uBAAYf,QAAQC,QAAQe,KAAKC,MAAMd,EAAQW,gBAC/CI,uBAAYlB,QAAQC,QAAQ,IAAIkB,KAAK,CAAChB,EAAQK,aAC9CY,MAAOZ,EACPD,QAAS,CACRF,uBAAYA,GACZgB,0BAAef,GACfgB,aAAKC,UAAKhB,EAAQgB,EAAEC,gBACpBC,aAAKF,UAAKA,EAAEC,gBAAiBjB,UAmB1BmB,IAAMC,KAfXxB,EAAQyB,KAAK7B,EAAQ8B,QAAU,MAAO/B,GAAK,GAE3CK,EAAQ2B,kBACP3B,EAAQ4B,wBAAwBC,QAAQ,wCAAiCC,EAAGC,EAAKC,GAChF9B,EAAK+B,KAAKF,EAAMA,EAAIV,eACpBlB,EAAI8B,KAAK,CAACF,EAAKC,IACf5B,EAAQ2B,GAAO3B,EAAQ2B,GAAU3B,EAAQ2B,OAAQC,EAAUA,IAE5DlC,EAAQO,MAGTL,EAAQkC,QAAUnC,EAElBC,EAAQmC,gBAAuC,WAArBvC,EAAQwC,YAElBxC,EAAQQ,QACvBJ,EAAQqC,iBAAiBb,EAAG5B,EAAQQ,QAAQoB,IAG7CxB,EAAQsC,KAAK1C,EAAQ2C,MAAQ"}
\ No newline at end of file
diff --git a/node_modules/unfetch/src/index.d.ts b/node_modules/unfetch/src/index.d.ts
index 0c53ad9..8dd2d54 100644
--- a/node_modules/unfetch/src/index.d.ts
+++ b/node_modules/unfetch/src/index.d.ts
@@ -14,4 +14,6 @@ declare namespace unfetch {

declare const unfetch: typeof fetch;

+export function registerFetch(window: Window): unfetch;
+
export default unfetch;
diff --git a/node_modules/unfetch/src/index.mjs b/node_modules/unfetch/src/index.mjs
deleted file mode 100644
index 783ad42..0000000
Expand Down
33 changes: 25 additions & 8 deletions packages/driver/src/cy/assertions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// @ts-nocheck

import _ from 'lodash'
import Promise from 'bluebird'

Expand Down Expand Up @@ -54,12 +52,18 @@ const isDomSubjectAndMatchesValue = (value, subject) => {
return false
}

type Parsed = {
subject?: JQuery<any>
actual?: any
expected?: any
}

// Rules:
// 1. always remove value
// 2. if value is a jquery object set a subject
// 3. if actual is undefined or its not expected remove both actual + expected
const parseValueActualAndExpected = (value, actual, expected) => {
const obj = { actual, expected }
const obj: Parsed = { actual, expected }

if ($dom.isJquery(value)) {
obj.subject = value
Expand All @@ -77,7 +81,7 @@ export const create = (Cypress, cy) => {
const getUpcomingAssertions = () => {
const index = cy.state('index') + 1

const assertions = []
const assertions: any[] = []

// grab the rest of the queue'd commands
for (let cmd of cy.queue.slice(index)) {
Expand Down Expand Up @@ -137,7 +141,11 @@ export const create = (Cypress, cy) => {
message = message.replace(stackTracesRe, '\n')
}

let obj = parseValueActualAndExpected(value, actual, expected)
let parsed = parseValueActualAndExpected(value, actual, expected)
// TODO: make it more specific after defining the type for Cypress.log().
let obj: Record<string, any> = {
...parsed,
}

if ($dom.isElement(value)) {
obj.$el = $dom.wrap(value)
Expand Down Expand Up @@ -216,10 +224,18 @@ export const create = (Cypress, cy) => {
})
}

type VerifyUpcomingAssertionsCallbacks = {
ensureExistenceFor?: 'subject' | 'dom' | boolean
onPass?: Function
onFail?: (err?, isDefaultAssertionErr?: boolean, cmds?: any[]) => void
onRetry?: () => any
}

return {
finishAssertions,

verifyUpcomingAssertions (subject, options = {}, callbacks = {}) {
// TODO: define the specific type of options
verifyUpcomingAssertions (subject, options: Record<string, any> = {}, callbacks: VerifyUpcomingAssertionsCallbacks = {}) {
const cmds = getUpcomingAssertions()

cy.state('upcomingAssertions', cmds)
Expand Down Expand Up @@ -433,12 +449,13 @@ export const create = (Cypress, cy) => {
cy.state('onBeforeLog', setCommandLog)

// send verify=true as the last arg
return assertFn.apply(this, args.concat(true))
return assertFn.apply(this, args.concat(true) as any)
}

const fns = injectAssertionFns(cmds)

const subjects = []
// TODO: remove any when the type of subject, the first argument of this function is specified.
const subjects: any[] = []

// iterate through each subject
// and force the assertion to return
Expand Down
109 changes: 46 additions & 63 deletions packages/driver/src/cy/chai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
/* eslint-disable prefer-rest-params */
// tests in driver/cypress/integration/commands/assertions_spec.js

Expand Down Expand Up @@ -31,20 +30,6 @@ const whitespace = /\s/g
const valueHasLeadingOrTrailingWhitespaces = /\*\*'\s+|\s+'\*\*/g
const imageMarkdown = /!\[.*?\]\(.*?\)/g

let assertProto = null
let matchProto = null
let lengthProto = null
let containProto = null
let existProto = null
let getMessage = null
let chaiUtils = null
let replaceArgMessages = null
let removeOrKeepSingleQuotesBetweenStars = null
let setSpecWindowGlobals = null
let restoreAsserts = null
let overrideExpect = null
let overrideChaiAsserts = null

type CreateFunc = ((specWindow, state, assertFn) => ({
chai: Chai.ChaiStatic
expect: (val: any, message?: string) => Chai.Assertion
Expand All @@ -55,7 +40,7 @@ export let create: CreateFunc | null = null
chai.use(sinonChai)

chai.use((chai, u) => {
chaiUtils = u
const chaiUtils = u

$chaiJquery(chai, chaiUtils, {
onInvalid (method, obj) {
Expand Down Expand Up @@ -87,14 +72,14 @@ chai.use((chai, u) => {
},
})

assertProto = chai.Assertion.prototype.assert
matchProto = chai.Assertion.prototype.match
lengthProto = chai.Assertion.prototype.__methods.length.method
containProto = chai.Assertion.prototype.__methods.contain.method
existProto = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, 'exist').get
const { objDisplay } = chai.util;
const assertProto = chai.Assertion.prototype.assert
const matchProto = (chai.Assertion.prototype as any).match
const lengthProto = (chai.Assertion.prototype as any).__methods.length.method
const containProto = (chai.Assertion.prototype as any).__methods.contain.method
const existProto = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, 'exist')!.get
const { objDisplay } = chai.util

({ getMessage } = chai.util)
const getMessage = chai.util.getMessage
const _inspect = chai.util.inspect

const { inspect, setFormatValueHook } = chaiInspect.create(chai)
Expand All @@ -108,17 +93,19 @@ chai.use((chai, u) => {
try {
val && val.document
val && val.inspect
} catch (e) {
} catch (e: any) {
if (e.stack.indexOf('cross-origin') !== -1 || // chrome
e.message.indexOf('cross-origin') !== -1) { // firefox
return `[window]`
}
}

return
})

// remove any single quotes between our **,
// except escaped quotes, empty strings and number strings.
removeOrKeepSingleQuotesBetweenStars = (message) => {
const removeOrKeepSingleQuotesBetweenStars = (message) => {
// remove any single quotes between our **, preserving escaped quotes
// and if an empty string, put the quotes back
return message.replace(allBetweenFourStars, (match) => {
Expand Down Expand Up @@ -149,8 +136,8 @@ chai.use((chai, u) => {
return message.replace(imageMarkdown, '``$&``')
}

replaceArgMessages = (args, str) => {
return _.reduce(args, (memo, value, index) => {
const replaceArgMessages = (args, str) => {
return _.reduce(args, (memo: string[], value, index) => {
if (_.isString(value)) {
value = value
.replace(allWordsBetweenCurlyBraces, '**$1**')
Expand All @@ -166,18 +153,17 @@ chai.use((chai, u) => {
}

return memo
}
, [])
}, [])
}

restoreAsserts = function () {
const restoreAsserts = function () {
chai.util.inspect = _inspect
chai.util.getMessage = getMessage
chai.util.objDisplay = objDisplay
chai.Assertion.prototype.assert = assertProto
chai.Assertion.prototype.match = matchProto
chai.Assertion.prototype.__methods.length.method = lengthProto
chai.Assertion.prototype.__methods.contain.method = containProto
chai.Assertion.prototype.assert = assertProto;
(chai.Assertion.prototype as any).match = matchProto;
(chai.Assertion.prototype as any).__methods.length.method = lengthProto;
(chai.Assertion.prototype as any).__methods.contain.method = containProto

return Object.defineProperty(chai.Assertion.prototype, 'exist', { get: existProto })
}
Expand Down Expand Up @@ -218,7 +204,7 @@ chai.use((chai, u) => {
}
}

overrideChaiAsserts = function (specWindow, state, assertFn) {
const overrideChaiAsserts = function (specWindow, state, assertFn) {
chai.Assertion.prototype.assert = createPatchedAssert(specWindow, state, assertFn)

const _origGetmessage = function (obj, args) {
Expand Down Expand Up @@ -248,6 +234,9 @@ chai.use((chai, u) => {
return (flagMsg ? `${flagMsg}: ${msg}` : msg)
}

// There are 2 types of getMessage. And we're overriding the second one.
// But TypeScript wants us to do both. So we're ignoring this.
// @ts-ignore
chaiUtils.getMessage = function (assert, args) {
const obj = assert._obj

Expand Down Expand Up @@ -307,13 +296,16 @@ chai.use((chai, u) => {
})
}

const containFn2 = (_super) => {
const makeMethodChainable = (_super) => {
return (function () {
return _super.apply(this, arguments)
})
}

chai.Assertion.overwriteChainableMethod('contain', containFn1, containFn2)
// `makeMethodChainable` doesn't match any type definition,
// but it is necessary to make the method chainable.
// @ts-ignore
chai.Assertion.overwriteChainableMethod('contain', containFn1, makeMethodChainable)

chai.Assertion.overwriteChainableMethod('length',
(_super) => {
Expand Down Expand Up @@ -346,7 +338,7 @@ chai.use((chai, u) => {
length,
obj.length,
)
} catch (e1) {
} catch (e1: any) {
e1.node = node
e1.negated = chaiUtils.flag(this, 'negate')
e1.type = 'length'
Expand Down Expand Up @@ -377,21 +369,21 @@ chai.use((chai, u) => {
}
})
},

(_super) => {
return (function () {
return _super.apply(this, arguments)
})
})

return chai.Assertion.overwriteProperty('exist', (_super) => {
// `makeMethodChainable` doesn't match any type definition,
// but it is necessary to make the method chainable.
// @ts-ignore
makeMethodChainable)

// _super is not documented.
// @ts-ignore
chai.Assertion.overwriteProperty('exist', (_super) => {
return (function () {
const obj = this._obj

if (!($dom.isJquery(obj) || $dom.isElement(obj))) {
try {
return _super.apply(this, arguments)
} catch (e) {
} catch (e: any) {
e.type = 'existence'
throw e
}
Expand All @@ -412,7 +404,7 @@ chai.use((chai, u) => {
node,
node,
)
} catch (e1) {
} catch (e1: any) {
e1.node = node
e1.negated = chaiUtils.flag(this, 'negate')
e1.type = 'existence'
Expand Down Expand Up @@ -456,20 +448,20 @@ chai.use((chai, u) => {
const createPatchedAssert = (specWindow, state, assertFn) => {
return (function (...args) {
let err
const passed = chaiUtils.test(this, args)
const passed = chaiUtils.test(this, args as Chai.AssertionArgs)
const value = chaiUtils.flag(this, 'object')
const expected = args[3]

const customArgs = replaceArgMessages(args, this._obj)

let message = chaiUtils.getMessage(this, customArgs)
const actual = chaiUtils.getActual(this, customArgs)
let message = chaiUtils.getMessage(this, customArgs as Chai.AssertionArgs)
const actual = chaiUtils.getActual(this, customArgs as Chai.AssertionArgs)

message = removeOrKeepSingleQuotesBetweenStars(message)
message = escapeMarkdown(message)

try {
assertProto.apply(this, args)
assertProto.apply(this, args as Chai.AssertionArgs)
} catch (e) {
err = e
}
Expand All @@ -489,7 +481,7 @@ chai.use((chai, u) => {
})
}

overrideExpect = (specWindow, state) => {
const overrideExpect = (specWindow, state) => {
// only override assertions for this specific
// expect function instance so we do not affect
// the outside world
Expand Down Expand Up @@ -523,7 +515,7 @@ chai.use((chai, u) => {
return fn
}

setSpecWindowGlobals = function (specWindow, state) {
const setSpecWindowGlobals = function (specWindow, state) {
const expect = overrideExpect(specWindow, state)
const assert = overrideAssert(specWindow, state)

Expand Down Expand Up @@ -552,12 +544,3 @@ chai.use((chai, u) => {
export interface IChai {
expect: ReturnType<CreateFunc>['expect']
}

export default {
replaceArgMessages,
removeOrKeepSingleQuotesBetweenStars,
setSpecWindowGlobals,
restoreAsserts,
overrideExpect,
overrideChaiAsserts,
}
7 changes: 7 additions & 0 deletions packages/driver/src/cy/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ export type KeyEventType =
| 'textInput'
| 'beforeinput'

export type ModifiersEventOptions = {
altKey: boolean
ctrlKey: boolean
metaKey: boolean
shiftKey: boolean
}

const toModifiersEventOptions = (modifiers: KeyboardModifiers) => {
return {
altKey: modifiers.alt,
Expand Down
Loading

3 comments on commit 01f0261

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 01f0261 Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.1.2/circle-develop-01f0261d3ae5fa12ae115501cd9887550517074d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 01f0261 Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.1.2/circle-develop-01f0261d3ae5fa12ae115501cd9887550517074d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 01f0261 Dec 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.1.2/circle-develop-01f0261d3ae5fa12ae115501cd9887550517074d/cypress.tgz

Please sign in to comment.