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

Commit

Permalink
standardize errros and include type
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding committed May 21, 2020
1 parent 3fb7b2c commit 772548d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
39 changes: 32 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ const watchify = require('watchify')

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

const typescriptExtensionRegex = /\.tsx?$/
const errorTypes = {
TYPESCRIPT_AND_TSIFY: 'TYPESCRIPT_AND_TSIFY',
TYPESCRIPT_NONEXISTENT: 'TYPESCRIPT_NONEXISTENT',
TYPESCRIPT_NOT_STRING: 'TYPESCRIPT_NOT_STRING',
}

const bundles = {}

// by default, we transform JavaScript (including some proposal features),
Expand Down Expand Up @@ -60,6 +67,16 @@ const defaultOptions = {
},
}

const throwError = ({ message, type }) => {
const prefix = 'Error running @cypress/browserify-preprocessor:\n\n'

const err = new Error(`${prefix}${message}`)

if (type) err.type = type

throw err
}

const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
let browserifyOptions = cloneDeep(defaultOptions.browserifyOptions)

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

if (typescriptPath) {
if (typeof typescriptPath !== 'string') {
throw new Error(`The 'typescript' option must be a string. You passed: ${typescriptPath}`)
throwError({
type: errorTypes.TYPESCRIPT_NOT_STRING,
message: `The 'typescript' option must be a string. You passed: ${typescriptPath}`,
})
}

const pathExists = await fs.pathExists(typescriptPath)

if (!pathExists) {
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}`)
throwError({
type: errorTypes.TYPESCRIPT_NONEXISTENT,
message: `The 'typescript' option must be a valid path to your TypeScript installation. We could not find anything at the following path: ${typescriptPath}`,
})
}

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

throw new Error(`Error running @cypress/browserify-preprocessor:
It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
throwError({
type: errorTypes.TYPESCRIPT_AND_TSIFY,
message: `It looks like you passed the 'typescript' option and also specified a browserify ${type} for TypeScript. This may cause conflicts.
Please do one of the following:
1) Pass in the 'typescript' option and omit the browserify ${type} (Recommmended)
2) Omit the 'typescript' option and continue to use your own browserify ${type}
`)
2) Omit the 'typescript' option and continue to use your own browserify ${type}`,
})
}

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

preprocessor.errorTypes = errorTypes

if (process.env.__TESTING__) {
preprocessor.reset = () => {
for (let filePath in bundles) {
Expand Down
22 changes: 18 additions & 4 deletions test/unit/index_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,19 @@ describe('browserify preprocessor', function () {
throw new Error('Should error, should not resolve')
}

const verifyErrorIncludesPrefix = (err) => {
expect(err.message).to.include('Error running @cypress/browserify-preprocessor:')
}

it('throws error when typescript path is not a string', function () {
this.options.typescript = true

return this.run()
.then(shouldntResolve)
.catch((err) => {
expect(err.message).to.equal(`The 'typescript' option must be a string. You passed: true`)
verifyErrorIncludesPrefix(err)
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NOT_STRING)
expect(err.message).to.include(`The 'typescript' option must be a string. You passed: true`)
})
})

Expand All @@ -445,7 +451,9 @@ describe('browserify preprocessor', function () {
return this.run()
.then(shouldntResolve)
.catch((err) => {
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`)
verifyErrorIncludesPrefix(err)
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_NONEXISTENT)
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`)
})
})

Expand All @@ -457,7 +465,9 @@ describe('browserify preprocessor', function () {
return this.run()
.then(shouldntResolve)
.catch((err) => {
expect(err.message).to.include('This may cause conflicts')
verifyErrorIncludesPrefix(err)
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_AND_TSIFY)
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`)
})
})

Expand All @@ -471,7 +481,11 @@ describe('browserify preprocessor', function () {
return this.run()
.then(shouldntResolve)
.catch((err) => {
expect(err.message).to.include('This may cause conflicts')
verifyErrorIncludesPrefix(err)
expect(err.type).to.equal(preprocessor.errorTypes.TYPESCRIPT_AND_TSIFY)
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`)
})
})
})
})
})
Expand Down

0 comments on commit 772548d

Please sign in to comment.