Skip to content

Commit 7007730

Browse files
committed
throw an error if typescript is installed, but a tsconfig.json cannot be found
1 parent 8fa0388 commit 7007730

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

npm/webpack-batteries-included-preprocessor/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const webpackPreprocessor = require('@cypress/webpack-preprocessor')
55

66
const debug = Debug('cypress:webpack-batteries-included-preprocessor')
77

8+
class TsConfigNotFoundError extends Error {
9+
constructor () {
10+
super('No tsconfig.json found, but typescript is installed. ts-loader needs a tsconfig.json file to work. Please add one to your project in either the root or the cypress directory.')
11+
this.name = 'TsConfigNotFoundError'
12+
}
13+
}
14+
815
const hasTsLoader = (rules) => {
916
return rules.some((rule) => {
1017
if (!rule.use || !Array.isArray(rule.use)) return false
@@ -40,7 +47,13 @@ const addTypeScriptConfig = (file, options) => {
4047
// returns null if tsconfig cannot be found in the path/parent hierarchy
4148
const configFile = getTsConfig.getTsconfig(file.filePath)
4249

43-
configFile ? debug(`found user tsconfig.json at ${configFile?.path} with compilerOptions: ${JSON.stringify(configFile?.config?.compilerOptions)}`) : debug('no user tsconfig.json found')
50+
if (!configFile && typescriptExtensionRegex.test(file.filePath)) {
51+
debug('no user tsconfig.json found. Throwing TsConfigNotFoundError')
52+
// @see https://github.com/cypress-io/cypress/issues/18938
53+
throw new TsConfigNotFoundError()
54+
}
55+
56+
debug(`found user tsconfig.json at ${configFile?.path} with compilerOptions: ${JSON.stringify(configFile?.config?.compilerOptions)}`)
4457

4558
webpackOptions.module.rules.push({
4659
test: /\.tsx?$/,

npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,21 @@ describe('webpack-batteries-included-preprocessor', () => {
104104
})
105105
})
106106

107-
it('always returns loader options even if there is an error discovering the user\'s tsconfig.json', () => {
107+
// @see https://github.com/cypress-io/cypress/issues/18938. ts-loader needs a tsconfig.json file to work.
108+
it('throws an error if the user\'s tsconfig.json is not found', () => {
108109
getTsConfigMock.returns(null)
109110

110111
const preprocessorCB = preprocessor({
111112
typescript: true,
112113
webpackOptions,
113114
})
114115

115-
preprocessorCB({
116-
filePath: 'foo.ts',
117-
outputPath: '.js',
118-
})
119-
120-
const tsLoader = webpackOptions.module.rules[0].use[0]
121-
122-
expect(tsLoader.loader).to.contain('ts-loader')
123-
124-
expect(tsLoader.options.compiler).to.be.true
125-
expect(tsLoader.options.logLevel).to.equal('error')
126-
expect(tsLoader.options.silent).to.be.true
127-
expect(tsLoader.options.transpileOnly).to.be.true
128-
129-
// compilerOptions are overridden (sourceMap=true) by `@cypress/webpack-preprocessor` if ts-loader is present
130-
expect(tsLoader.options.compilerOptions).to.be.undefined
116+
expect(() => {
117+
return preprocessorCB({
118+
filePath: 'foo.ts',
119+
outputPath: '.js',
120+
})
121+
}).to.throw('No tsconfig.json found, but typescript is installed. ts-loader needs a tsconfig.json file to work. Please add one to your project in either the root or the cypress directory.')
131122
})
132123
})
133124
})

0 commit comments

Comments
 (0)