Skip to content

Commit

Permalink
refactor unit tests to be a bit more dry
Browse files Browse the repository at this point in the history
  • Loading branch information
AtofStryker committed May 30, 2024
1 parent 5b4650e commit 273f6df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 45 deletions.
62 changes: 25 additions & 37 deletions npm/webpack-batteries-included-preprocessor/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: [],
},
Expand All @@ -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')
Expand All @@ -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": {
Expand All @@ -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,
Expand All @@ -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": {
Expand All @@ -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,
Expand Down
9 changes: 1 addition & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand Down

0 comments on commit 273f6df

Please sign in to comment.