Skip to content
This repository was archived by the owner on Jan 31, 2023. It is now read-only.

Commit 772548d

Browse files
committed
standardize errros and include type
1 parent 3fb7b2c commit 772548d

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

index.js

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ const watchify = require('watchify')
1010

1111
const debug = require('debug')('cypress:browserify')
1212

13+
const typescriptExtensionRegex = /\.tsx?$/
14+
const errorTypes = {
15+
TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY',
16+
TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT',
17+
TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING',
18+
}
19+
1320
const bundles = {}
1421

1522
// by default, we transform JavaScript (including some proposal features),
@@ -60,6 +67,16 @@ const defaultOptions = {
6067
},
6168
}
6269

70+
const throwError = ({ message, type }) => {
71+
const prefix = 'Error running @cypress/browserify-preprocessor:\n\n'
72+
73+
const err = new Error(`${prefix}${message}`)
74+
75+
if (type) err.type = type
76+
77+
throw err
78+
}
79+
6380
const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
6481
let browserifyOptions = cloneDeep(defaultOptions.browserifyOptions)
6582

@@ -83,13 +100,19 @@ const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescrip
83100

84101
if (typescriptPath) {
85102
if (typeof typescriptPath !== 'string') {
86-
throw new Error(`The 'typescript' option must be a string. You passed: ${typescriptPath}`)
103+
throwError({
104+
type: errorTypes.TYPESCRIPT_NOT_STRING,
105+
message: `The 'typescript' option must be a string. You passed: ${typescriptPath}`,
106+
})
87107
}
88108

89109
const pathExists = await fs.pathExists(typescriptPath)
90110

91111
if (!pathExists) {
92-
throw new Error(`The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: ${typescriptPath}`)
112+
throwError({
113+
type: errorTypes.TYPESCRIPT_NONEXISTENT,
114+
message: `The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: ${typescriptPath}`,
115+
})
93116
}
94117

95118
const transform = browserifyOptions.transform
@@ -99,15 +122,15 @@ const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescrip
99122
if (hasTsifyTransform || hastsifyPlugin) {
100123
const type = hasTsifyTransform ? 'transform' : 'plugin'
101124

102-
throw new Error(`Error running @cypress/browserify-preprocessor:
103-
104-
It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
125+
throwError({
126+
type: errorTypes.TYPESCRIPT_AND_TSIFY,
127+
message: `It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
105128
106129
Please do one of the following:
107130
108131
1) Pass in the 'typescript' option and omit the browserify ${type} (Recommmended)
109-
2) Omit the 'typescript' option and continue to use your own browserify ${type}
110-
`)
132+
2) Omit the 'typescript' option and continue to use your own browserify ${type}`,
133+
})
111134
}
112135

113136
browserifyOptions.extensions.push('.ts', '.tsx')
@@ -263,6 +286,8 @@ const preprocessor = (options = {}) => {
263286
// provide a clone of the default options
264287
preprocessor.defaultOptions = JSON.parse(JSON.stringify(defaultOptions))
265288

289+
preprocessor.errorTypes = errorTypes
290+
266291
if (process.env.__TESTING__) {
267292
preprocessor.reset = () => {
268293
for (let filePath in bundles) {

test/unit/index_spec.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,19 @@ describe('browserify preprocessor', function () {
429429
throw new Error('Should error, should not resolve')
430430
}
431431

432+
const verifyErrorIncludesPrefix = (err) => {
433+
expect(err.message).to.include('Error running @cypress/browserify-preprocessor:')
434+
}
435+
432436
it('throws error when typescript path is not a string', function () {
433437
this.options.typescript = true
434438

435439
return this.run()
436440
.then(shouldntResolve)
437441
.catch((err) => {
438-
expect(err.message).to.equal(`The 'typescript' option must be a string. You passed: true`)
442+
verifyErrorIncludesPrefix(err)
443+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_STRING)
444+
expect(err.message).to.include(`The 'typescript' option must be a string. You passed: true`)
439445
})
440446
})
441447

@@ -445,7 +451,9 @@ describe('browserify preprocessor', function () {
445451
return this.run()
446452
.then(shouldntResolve)
447453
.catch((err) => {
448-
expect(err.message).to.equal(`The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: /nothing/here`)
454+
verifyErrorIncludesPrefix(err)
455+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NONEXISTENT)
456+
expect(err.message).to.include(`The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: /nothing/here`)
449457
})
450458
})
451459

@@ -457,7 +465,9 @@ describe('browserify preprocessor', function () {
457465
return this.run()
458466
.then(shouldntResolve)
459467
.catch((err) => {
460-
expect(err.message).to.include('This may cause conflicts')
468+
verifyErrorIncludesPrefix(err)
469+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_AND_TSIFY)
470+
expect(err.message).to.include(`It looks like you passed the 'typescript' option and also specified a browserify plugin for TypeScript. This may cause conflicts`)
461471
})
462472
})
463473

@@ -471,7 +481,11 @@ describe('browserify preprocessor', function () {
471481
return this.run()
472482
.then(shouldntResolve)
473483
.catch((err) => {
474-
expect(err.message).to.include('This may cause conflicts')
484+
verifyErrorIncludesPrefix(err)
485+
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_AND_TSIFY)
486+
expect(err.message).to.include(`It looks like you passed the 'typescript' option and also specified a browserify transform for TypeScript. This may cause conflicts`)
487+
})
488+
})
475489
})
476490
})
477491
})

0 commit comments

Comments
 (0)