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

Commit 3fb7b2c

Browse files
committed
fix: validate type of typescript option and its existence as a path
1 parent ad88970 commit 3fb7b2c

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

index.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const defaultOptions = {
6060
},
6161
}
6262

63-
const getBrowserifyOptions = (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
63+
const getBrowserifyOptions = async (entry, userBrowserifyOptions = {}, typescriptPath = null) => {
6464
let browserifyOptions = cloneDeep(defaultOptions.browserifyOptions)
6565

6666
// allow user to override default options
@@ -82,6 +82,16 @@ const getBrowserifyOptions = (entry, userBrowserifyOptions = {}, typescriptPath
8282
})
8383

8484
if (typescriptPath) {
85+
if (typeof typescriptPath !== 'string') {
86+
throw new Error(`The 'typescript' option must be a string. You passed: ${typescriptPath}`)
87+
}
88+
89+
const pathExists = await fs.pathExists(typescriptPath)
90+
91+
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}`)
93+
}
94+
8595
const transform = browserifyOptions.transform
8696
const hasTsifyTransform = transform.some(([name]) => name.includes('tsify'))
8797
const hastsifyPlugin = browserifyOptions.plugin.includes('tsify')
@@ -135,7 +145,7 @@ const preprocessor = (options = {}) => {
135145
// when running in the GUI, it will likely get called multiple times
136146
// with the same filePath, as the user could re-run the tests, causing
137147
// the supported file and spec file to be requested again
138-
return (file) => {
148+
return async (file) => {
139149
const filePath = file.filePath
140150

141151
debug('get:', filePath)
@@ -157,7 +167,7 @@ const preprocessor = (options = {}) => {
157167
debug('input:', filePath)
158168
debug('output:', outputPath)
159169

160-
const browserifyOptions = getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
170+
const browserifyOptions = await getBrowserifyOptions(filePath, options.browserifyOptions, options.typescript)
161171
const watchifyOptions = Object.assign({}, defaultOptions.watchifyOptions, options.watchifyOptions)
162172

163173
const bundler = browserify(browserifyOptions)

test/unit/index_spec.js

+37-5
Original file line numberDiff line numberDiff line change
@@ -424,23 +424,55 @@ describe('browserify preprocessor', function () {
424424
})
425425
})
426426

427-
describe('when typescript path and tsify are given together', function () {
428-
it('throws error when it is a plugin', function () {
427+
describe('validation', function () {
428+
const shouldntResolve = () => {
429+
throw new Error('Should error, should not resolve')
430+
}
431+
432+
it('throws error when typescript path is not a string', function () {
433+
this.options.typescript = true
434+
435+
return this.run()
436+
.then(shouldntResolve)
437+
.catch((err) => {
438+
expect(err.message).to.equal(`The 'typescript' option must be a string. You passed: true`)
439+
})
440+
})
441+
442+
it('throws error when nothing exists at typescript path', function () {
443+
this.options.typescript = '/nothing/here'
444+
445+
return this.run()
446+
.then(shouldntResolve)
447+
.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`)
449+
})
450+
})
451+
452+
it('throws error when typescript path and tsify plugin are specified', function () {
429453
this.options.browserifyOptions = {
430454
plugin: ['tsify'],
431455
}
432456

433-
expect(this.run).to.throw('This may cause conflicts')
457+
return this.run()
458+
.then(shouldntResolve)
459+
.catch((err) => {
460+
expect(err.message).to.include('This may cause conflicts')
461+
})
434462
})
435463

436-
it('throws error when it is a transform', function () {
464+
it('throws error when typescript path and tsify transform are specified', function () {
437465
this.options.browserifyOptions = {
438466
transform: [
439467
['path/to/tsify', {}],
440468
],
441469
}
442470

443-
expect(this.run).to.throw('This may cause conflicts')
471+
return this.run()
472+
.then(shouldntResolve)
473+
.catch((err) => {
474+
expect(err.message).to.include('This may cause conflicts')
475+
})
444476
})
445477
})
446478
})

0 commit comments

Comments
 (0)