Skip to content

Commit 91fe08e

Browse files
authored
chore: update CLI snapshots to include ANSI, replace check-more-types and la, and use vi.mocked (#32486)
* chore: rework vitest mocks * chore: turn on ANSI for snapshots in the CLI * chore: remove check-more-types from the CLI and just use lodash/regex * chore: remove lazy-ass in favor of the native assertion library in node
1 parent 1614ca9 commit 91fe08e

31 files changed

+905
-1193
lines changed

cli/lib/errors.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import chalk from 'chalk'
22
import { stripIndent, stripIndents } from 'common-tags'
3-
import la from 'lazy-ass'
3+
import _ from 'lodash'
4+
import assert from 'assert'
45
import util from './util'
56
import state from './tasks/state'
67

7-
// TODO: this package needs to be replaced as we can't import it in vitest
8-
const is = require('check-more-types')
9-
108
const docsUrl = 'https://on.cypress.io'
119
const requiredDependenciesUrl = `${docsUrl}/required-dependencies`
1210
const runDocumentationUrl = `${docsUrl}/cypress-run`
@@ -309,18 +307,14 @@ export async function formErrorText (info: any, msg?: string, prevMessage?: stri
309307
formatted.push(stripIndents(msg))
310308
}
311309

312-
la(
313-
is.unemptyString(infoWithPlatform.description),
314-
'expected error description to be text',
315-
infoWithPlatform.description,
316-
)
310+
assert.ok(_.isString(infoWithPlatform.description) && !_.isEmpty(infoWithPlatform.description), 'expected error description to be text.')
317311

318312
// assuming that if there the solution is a function it will handle
319313
// error message and (optional previous error message)
320-
if (is.fn(infoWithPlatform.solution)) {
314+
if (_.isFunction(infoWithPlatform.solution)) {
321315
const text = infoWithPlatform.solution(msg, prevMessage)
322316

323-
la(is.unemptyString(text), 'expected solution to be text', text)
317+
assert.ok(_.isString(text) && !_.isEmpty(text), 'expected solution to be text.')
324318

325319
add(`
326320
${infoWithPlatform.description}
@@ -329,11 +323,7 @@ export async function formErrorText (info: any, msg?: string, prevMessage?: stri
329323
330324
`)
331325
} else {
332-
la(
333-
is.unemptyString(infoWithPlatform.solution),
334-
'expected error solution to be text',
335-
infoWithPlatform.solution,
336-
)
326+
assert.ok(_.isString(infoWithPlatform.solution) && !_.isEmpty(infoWithPlatform.solution), 'expected error solution to be text.')
337327

338328
add(`
339329
${infoWithPlatform.description}

cli/lib/tasks/download.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import la from 'lazy-ass'
21
import os from 'os'
2+
import assert from 'assert'
3+
import _ from 'lodash'
34
import url from 'url'
45
import path from 'path'
56
import Debug from 'debug'
@@ -12,9 +13,6 @@ import { throwFormErrorText, errors } from '../errors'
1213
import fs from 'fs-extra'
1314
import util from '../util'
1415

15-
// TODO: this package needs to be replaced as we can't import it in vitest
16-
const is = require('check-more-types')
17-
1816
const debug = Debug('cypress:cli')
1917

2018
const defaultBaseUrl = 'https://download.cypress.io/'
@@ -84,7 +82,7 @@ const prepend = (arch: string, urlPath: string, version: string): string => {
8482
}
8583

8684
const getUrl = (arch: string, version?: string): string => {
87-
if (is.webUrl(version)) {
85+
if (_.isString(version) && version.match(/^https?:\/\/.*$/)) {
8886
debug('version is already an url', version)
8987

9088
return version as string
@@ -338,7 +336,7 @@ const start = async (opts: any): Promise<any> => {
338336
let { version, downloadDestination, progress, redirectTTL } = opts
339337

340338
if (!downloadDestination) {
341-
la(is.unemptyString(downloadDestination), 'missing download dir', opts)
339+
assert.ok(_.isString(downloadDestination) && !_.isEmpty(downloadDestination), 'missing download dir')
342340
}
343341

344342
if (!progress) {

cli/lib/tasks/unzip.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import _ from 'lodash'
2-
import la from 'lazy-ass'
32
import cp from 'child_process'
43
import os from 'os'
54
import yauzl from 'yauzl'
@@ -9,9 +8,7 @@ import readline from 'readline'
98
import fs from 'fs-extra'
109
import { throwFormErrorText, errors } from '../errors'
1110
import util from '../util'
12-
13-
// TODO: this package needs to be replaced as we can't import it in vitest
14-
const is = require('check-more-types')
11+
import assert from 'assert'
1512

1613
const debug = Debug('cypress:cli:unzip')
1714

@@ -199,7 +196,7 @@ function isMaybeWindowsMaxPathLengthError (err: any): boolean {
199196
}
200197

201198
const start = async ({ zipFilePath, installDir, progress }: any): Promise<void> => {
202-
la(is.unemptyString(installDir), 'missing installDir')
199+
assert.ok(_.isString(installDir) && !_.isEmpty(installDir), 'missing installDir')
203200
if (!progress) {
204201
progress = { onProgress: () => {
205202
return {}

cli/lib/util.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import _ from 'lodash'
2+
import assert from 'assert'
23
import arch from 'arch'
34
import os from 'os'
45
import ospath from 'ospath'
56
import hasha from 'hasha'
6-
import la from 'lazy-ass'
77
import tty from 'tty'
88
import path from 'path'
99
import { isCI as isCi } from 'ci-info'
@@ -23,9 +23,6 @@ import Debug from 'debug'
2323
import fs from 'fs-extra'
2424
import pkg from '../package.json'
2525

26-
// TODO: this package needs to be replaced as we can't import it in vitest
27-
const is = require('check-more-types')
28-
2926
const debug = Debug('cypress:cli')
3027

3128
const issuesUrl = 'https://github.com/cypress-io/cypress/issues'
@@ -34,13 +31,13 @@ const issuesUrl = 'https://github.com/cypress-io/cypress/issues'
3431
* Returns SHA512 of a file
3532
*/
3633
const getFileChecksum = (filename: string): any => {
37-
la(is.unemptyString(filename), 'expected filename', filename)
34+
assert.ok(_.isString(filename) && !_.isEmpty(filename), 'expected filename')
3835

3936
return hasha.fromFile(filename, { algorithm: 'sha512' })
4037
}
4138

4239
const getFileSize = async (filename: string): Promise<any> => {
43-
la(is.unemptyString(filename), 'expected filename', filename)
40+
assert.ok(_.isString(filename) && !_.isEmpty(filename), 'expected filename')
4441

4542
const { size } = await fs.stat(filename)
4643

@@ -165,7 +162,7 @@ function printNodeOptions (log: any = debug): void {
165162
```
166163
*/
167164
const dequote = (str: string): string => {
168-
la(is.string(str), 'expected a string to remove double quotes', str)
165+
assert.ok(_.isString(str), 'expected a string to remove double quotes')
169166
if (str.length > 1 && str[0] === '"' && str[str.length - 1] === '"') {
170167
return str.substr(1, str.length - 2)
171168
}
@@ -501,7 +498,7 @@ const util = {
501498
},
502499

503500
getEnv (varName: string, trim?: boolean): string | undefined {
504-
la(is.unemptyString(varName), 'expected environment variable name, not', varName)
501+
assert.ok(_.isString(varName) && !_.isEmpty(varName), 'expected environment variable name, not')
505502

506503
const configVarName = `npm_config_${varName}`
507504
const configVarNameLower = configVarName.toLowerCase()
@@ -560,8 +557,8 @@ const util = {
560557
isPossibleLinuxWithIncorrectDisplay,
561558

562559
getGitHubIssueUrl (number: number): string {
563-
la(is.positive(number), 'github issue should be a positive number', number)
564-
la(_.isInteger(number), 'github issue should be an integer', number)
560+
assert.ok(_.isInteger(number), 'github issue should be an integer')
561+
assert.ok(number > 0, 'github issue should be a positive number')
565562

566563
return `${issuesUrl}/${number}`
567564
},

cli/package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"buffer": "^5.7.1",
3030
"cachedir": "^2.3.0",
3131
"chalk": "^4.1.0",
32-
"check-more-types": "^2.24.0",
3332
"ci-info": "^4.1.0",
3433
"cli-cursor": "^3.1.0",
3534
"cli-table3": "0.6.1",
@@ -46,7 +45,6 @@
4645
"fs-extra": "^9.1.0",
4746
"hasha": "5.2.2",
4847
"is-installed-globally": "~0.4.0",
49-
"lazy-ass": "^1.6.0",
5048
"listr2": "^3.8.3",
5149
"lodash": "^4.17.21",
5250
"log-symbols": "^4.0.0",

cli/test/lib/build.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ describe('package.json build', () => {
2222
// with a few test props
2323
// the rest should come from root package.json file
2424

25-
// @ts-expect-error - mockResolvedValue
26-
fs.readJson.mockResolvedValue({
25+
vi.mocked(fs.readJson).mockResolvedValue({
2726
name: 'test',
2827
engines: 'test engines',
29-
})
28+
} as any)
3029

31-
// @ts-expect-error - mockResolvedValue
32-
fs.outputJson.mockResolvedValue(undefined)
30+
vi.mocked(fs.outputJson).mockResolvedValue(undefined)
3331
})
3432

3533
it('has a semver version', async () => {

0 commit comments

Comments
 (0)