diff --git a/npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js b/npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js index 3d5d9eec5404..53eaab36a467 100644 --- a/npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js +++ b/npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js @@ -33,11 +33,18 @@ describe('webpack-batteries-included-preprocessor', () => { context('#getTSCompilerOptionsForUser', () => { const mockTsconfigPath = '/path/to/tsconfig.json' + let readFileTsConfigMock let preprocessor + let readFileTsConfigStub + let webpackOptions beforeEach(() => { const tsConfigPathSpy = sinon.spy() + readFileTsConfigMock = () => { + throw new Error('Could not read file!') + } + mock('tsconfig-paths-webpack-plugin', tsConfigPathSpy) mock('@cypress/webpack-preprocessor', (options) => { return (file) => undefined @@ -48,16 +55,14 @@ describe('webpack-batteries-included-preprocessor', () => { sinon.stub(tsconfig, 'findSync').callsFake(() => mockTsconfigPath) preprocessor = require('../../index') - }) - afterEach(() => { - // Remove the mock - mock.stop('tsconfig-paths-webpack-plugin') - mock.stop('@cypress/webpack-preprocessor') - }) + const fs = require('fs-extra') - it('always returns compilerOptions even if there is an error discovering the user\'s tsconfig.json', () => { - const webpackOptions = { + readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => { + return readFileTsConfigMock() + }) + + webpackOptions = { module: { rules: [], }, @@ -66,17 +71,26 @@ describe('webpack-batteries-included-preprocessor', () => { plugins: [], }, } + }) + + afterEach(() => { + // Remove the mock + mock.stop('tsconfig-paths-webpack-plugin') + mock.stop('@cypress/webpack-preprocessor') + }) + + it('always returns compilerOptions even if there is an error discovering the user\'s tsconfig.json', () => { const preprocessorCB = preprocessor({ typescript: true, webpackOptions, }) - // will not be able to find the user's tsconfig preprocessorCB({ filePath: 'foo.ts', outputPath: '.js', }) + sinon.assert.calledOnce(readFileTsConfigStub) const tsLoader = webpackOptions.module.rules[0].use[0] expect(tsLoader.loader).to.contain('ts-loader') @@ -95,8 +109,6 @@ describe('webpack-batteries-included-preprocessor', () => { }) it('turns inlineSourceMaps on by default even if none are configured', () => { - const fs = require('fs-extra') - // make json5 compat schema const mockTsConfig = `{ "compilerOptions": { @@ -105,19 +117,8 @@ describe('webpack-batteries-included-preprocessor', () => { } }` - const readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => { - return mockTsConfig - }) + readFileTsConfigMock = () => mockTsConfig - const webpackOptions = { - module: { - rules: [], - }, - resolve: { - extensions: [], - plugins: [], - }, - } const preprocessorCB = preprocessor({ typescript: true, webpackOptions, @@ -142,8 +143,6 @@ describe('webpack-batteries-included-preprocessor', () => { }) it('turns on sourceMaps and disables inlineSourceMap and inlineSources if the sourceMap configuration option is set by the user', () => { - const fs = require('fs-extra') - // make json5 compat schema const mockTsConfig = `{ "compilerOptions": { @@ -152,19 +151,8 @@ describe('webpack-batteries-included-preprocessor', () => { } }` - const readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => { - return mockTsConfig - }) + readFileTsConfigMock = () => mockTsConfig - const webpackOptions = { - module: { - rules: [], - }, - resolve: { - extensions: [], - plugins: [], - }, - } const preprocessorCB = preprocessor({ typescript: true, webpackOptions, diff --git a/yarn.lock b/yarn.lock index 728f8c62b5af..7672483a6275 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6524,14 +6524,7 @@ dependencies: type-detect "4.0.8" -"@sinonjs/commons@^3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.0.tgz#beb434fe875d965265e04722ccfc21df7f755d72" - integrity sha512-jXBtWAF4vmdNmZgD5FoKsVLv3rPgDnLgPbU84LIJ3otV44vJlDRokVng5v8NFJdCf/da9legHcKaRuZs4L7faA== - dependencies: - type-detect "4.0.8" - -"@sinonjs/commons@^3.0.1": +"@sinonjs/commons@^3.0.0", "@sinonjs/commons@^3.0.1": version "3.0.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.1.tgz#1029357e44ca901a615585f6d27738dbc89084cd" integrity sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==