Skip to content

Commit cc274f7

Browse files
committed
refactor unit tests to be a bit more dry
1 parent afc2d51 commit cc274f7

File tree

2 files changed

+26
-45
lines changed

2 files changed

+26
-45
lines changed

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

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,18 @@ describe('webpack-batteries-included-preprocessor', () => {
3333

3434
context('#getTSCompilerOptionsForUser', () => {
3535
const mockTsconfigPath = '/path/to/tsconfig.json'
36+
let readFileTsConfigMock
3637
let preprocessor
38+
let readFileTsConfigStub
39+
let webpackOptions
3740

3841
beforeEach(() => {
3942
const tsConfigPathSpy = sinon.spy()
4043

44+
readFileTsConfigMock = () => {
45+
throw new Error('Could not read file!')
46+
}
47+
4148
mock('tsconfig-paths-webpack-plugin', tsConfigPathSpy)
4249
mock('@cypress/webpack-preprocessor', (options) => {
4350
return (file) => undefined
@@ -48,16 +55,14 @@ describe('webpack-batteries-included-preprocessor', () => {
4855
sinon.stub(tsconfig, 'findSync').callsFake(() => mockTsconfigPath)
4956

5057
preprocessor = require('../../index')
51-
})
5258

53-
afterEach(() => {
54-
// Remove the mock
55-
mock.stop('tsconfig-paths-webpack-plugin')
56-
mock.stop('@cypress/webpack-preprocessor')
57-
})
59+
const fs = require('fs-extra')
5860

59-
it('always returns compilerOptions even if there is an error discovering the user\'s tsconfig.json', () => {
60-
const webpackOptions = {
61+
readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => {
62+
return readFileTsConfigMock()
63+
})
64+
65+
webpackOptions = {
6166
module: {
6267
rules: [],
6368
},
@@ -66,17 +71,26 @@ describe('webpack-batteries-included-preprocessor', () => {
6671
plugins: [],
6772
},
6873
}
74+
})
75+
76+
afterEach(() => {
77+
// Remove the mock
78+
mock.stop('tsconfig-paths-webpack-plugin')
79+
mock.stop('@cypress/webpack-preprocessor')
80+
})
81+
82+
it('always returns compilerOptions even if there is an error discovering the user\'s tsconfig.json', () => {
6983
const preprocessorCB = preprocessor({
7084
typescript: true,
7185
webpackOptions,
7286
})
7387

74-
// will not be able to find the user's tsconfig
7588
preprocessorCB({
7689
filePath: 'foo.ts',
7790
outputPath: '.js',
7891
})
7992

93+
sinon.assert.calledOnce(readFileTsConfigStub)
8094
const tsLoader = webpackOptions.module.rules[0].use[0]
8195

8296
expect(tsLoader.loader).to.contain('ts-loader')
@@ -95,8 +109,6 @@ describe('webpack-batteries-included-preprocessor', () => {
95109
})
96110

97111
it('turns inlineSourceMaps on by default even if none are configured', () => {
98-
const fs = require('fs-extra')
99-
100112
// make json5 compat schema
101113
const mockTsConfig = `{
102114
"compilerOptions": {
@@ -105,19 +117,8 @@ describe('webpack-batteries-included-preprocessor', () => {
105117
}
106118
}`
107119

108-
const readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => {
109-
return mockTsConfig
110-
})
120+
readFileTsConfigMock = () => mockTsConfig
111121

112-
const webpackOptions = {
113-
module: {
114-
rules: [],
115-
},
116-
resolve: {
117-
extensions: [],
118-
plugins: [],
119-
},
120-
}
121122
const preprocessorCB = preprocessor({
122123
typescript: true,
123124
webpackOptions,
@@ -142,8 +143,6 @@ describe('webpack-batteries-included-preprocessor', () => {
142143
})
143144

144145
it('turns on sourceMaps and disables inlineSourceMap and inlineSources if the sourceMap configuration option is set by the user', () => {
145-
const fs = require('fs-extra')
146-
147146
// make json5 compat schema
148147
const mockTsConfig = `{
149148
"compilerOptions": {
@@ -152,19 +151,8 @@ describe('webpack-batteries-included-preprocessor', () => {
152151
}
153152
}`
154153

155-
const readFileTsConfigStub = sinon.stub(fs, 'readFileSync').withArgs(mockTsconfigPath, 'utf8').callsFake(() => {
156-
return mockTsConfig
157-
})
154+
readFileTsConfigMock = () => mockTsConfig
158155

159-
const webpackOptions = {
160-
module: {
161-
rules: [],
162-
},
163-
resolve: {
164-
extensions: [],
165-
plugins: [],
166-
},
167-
}
168156
const preprocessorCB = preprocessor({
169157
typescript: true,
170158
webpackOptions,

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6524,14 +6524,7 @@
65246524
dependencies:
65256525
type-detect "4.0.8"
65266526

6527-
"@sinonjs/commons@^3.0.0":
6528-
version "3.0.0"
6529-
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.0.tgz#beb434fe875d965265e04722ccfc21df7f755d72"
6530-
integrity sha512-jXBtWAF4vmdNmZgD5FoKsVLv3rPgDnLgPbU84LIJ3otV44vJlDRokVng5v8NFJdCf/da9legHcKaRuZs4L7faA==
6531-
dependencies:
6532-
type-detect "4.0.8"
6533-
6534-
"@sinonjs/commons@^3.0.1":
6527+
"@sinonjs/commons@^3.0.0", "@sinonjs/commons@^3.0.1":
65356528
version "3.0.1"
65366529
resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-3.0.1.tgz#1029357e44ca901a615585f6d27738dbc89084cd"
65376530
integrity sha512-K3mCHKQ9sVh8o1C9cxkwxaOmXoAMlDxC1mYyHrjqOWEcBjYr76t96zL2zlj5dUGZ3HSw240X1qgH3Mjf1yJWpQ==

0 commit comments

Comments
 (0)