Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up mocking code a bit #275

Merged
merged 2 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
},
"jest": {
"preset": "ts-jest",
"resetMocks": true,
"collectCoverageFrom": [
"src/**/*.ts"
],
Expand Down
26 changes: 10 additions & 16 deletions src/cli/git-status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,53 +13,47 @@ jest.setMock('is-git-clean', {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const checkGitStatus = require('./git-status').default

const _process: any = process
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation()
jest.spyOn(console, 'warn').mockImplementation()
jest.spyOn(process, 'exit').mockImplementation()
})

it('does not exit and output any logs when git repo is clean', () => {
gitStatusReturnValue = true
console.log = jest.fn()
_process.exit = jest.fn()
checkGitStatus()
expect(console.log).not.toHaveBeenCalled()
expect(_process.exit).not.toHaveBeenCalled()
expect(process.exit).not.toHaveBeenCalled()
})

it('does not exit and output any logs when not a git repo', () => {
const err = new Error() as any
err.stderr = 'Not a git repository'
gitStatusReturnValue = err
console.log = jest.fn()
_process.exit = jest.fn()
checkGitStatus()
expect(console.log).not.toHaveBeenCalled()
expect(_process.exit).not.toHaveBeenCalled()
expect(process.exit).not.toHaveBeenCalled()
})

it('exits and output logs when git repo is dirty', () => {
gitStatusReturnValue = false
console.log = jest.fn()
_process.exit = jest.fn()
checkGitStatus()
expect(console.log).toHaveBeenCalled()
expect(_process.exit).toHaveBeenCalled()
expect(process.exit).toHaveBeenCalled()
})

it('exits and output logs when git detection fail', () => {
gitStatusReturnValue = new Error('bum')
console.log = jest.fn()
_process.exit = jest.fn()
checkGitStatus()
expect(console.log).toHaveBeenCalled()
expect(_process.exit).toHaveBeenCalled()
expect(process.exit).toHaveBeenCalled()
})

it('does not exit when git repo is dirty and force flag is given', () => {
gitStatusReturnValue = false
console.log = jest.fn()
_process.exit = jest.fn()
checkGitStatus(true)
expect(console.log).toHaveBeenCalledWith(
'WARNING: Git directory is not clean. Forcibly continuing.\n'
)
expect(_process.exit).not.toHaveBeenCalled()
expect(process.exit).not.toHaveBeenCalled()
})
9 changes: 4 additions & 5 deletions src/cli/transformers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import {
transformerDirectory,
} from './transformers'

beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation()
})

it('finds transformer directory', () => {
fs.lstatSync(transformerDirectory)
})
Expand All @@ -25,7 +29,6 @@ it('finds jscodeshift executable', () => {

it('runs jscodeshift for the given transformer', () => {
execaReturnValue = { stderr: null }
console.log = jest.fn()
executeTransformations({
files: 'src',
flags: {},
Expand All @@ -42,7 +45,6 @@ it('runs jscodeshift for the given transformer', () => {

it('supports jscodeshift flags', () => {
execaReturnValue = { stderr: null }
console.log = jest.fn()
executeTransformations({
files: 'folder',
flags: { dry: true },
Expand All @@ -59,7 +61,6 @@ it('supports jscodeshift flags', () => {

it('supports typescript parser', () => {
execaReturnValue = { stderr: null }
console.log = jest.fn()
executeTransformations({
files: 'folder',
flags: { dry: true },
Expand All @@ -76,7 +77,6 @@ it('supports typescript parser', () => {

it('supports jscodeshift custom arguments', () => {
execaReturnValue = { stderr: null }
console.log = jest.fn()
executeTransformations({
files: 'folder',
flags: { dry: true },
Expand All @@ -95,7 +95,6 @@ it('supports jscodeshift custom arguments', () => {
it('rethrows jscodeshift errors', () => {
const transformerError = new Error('bum')
execaReturnValue = { stderr: transformerError }
console.log = jest.fn()
expect(() => {
executeTransformations({
files: 'src',
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/ava.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/chai-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/chai-should.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/expect-js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/expect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/jasmine-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let consoleWarnings = []

beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

test('spyOn', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/mocha.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/should.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput) {
Expand Down
2 changes: 1 addition & 1 deletion src/transformers/tape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const wrappedPlugin = wrapPlugin(plugin)
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function expectTransformation(source, expectedOutput, options = {}) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/finale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chalk.level = 0
let consoleWarnings = []
beforeEach(() => {
consoleWarnings = []
console.warn = (v) => consoleWarnings.push(v)
jest.spyOn(console, 'warn').mockImplementation((v) => consoleWarnings.push(v))
})

function testChanged(msg, source, expectedOutput, options = {}, expectedErrors = []) {
Expand Down
35 changes: 14 additions & 21 deletions src/utils/proxyquire.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-env jest */
import jscodeshift from 'jscodeshift'

import * as _logger from './logger'
import proxyquireTransformer from './proxyquire'

const mockedLogger = jest.fn()
jest.mock('./logger', () => (args) => mockedLogger(args))
jest.spyOn(_logger, 'default')
const logger = _logger.default

const j = jscodeshift
const fileInfo = { path: 'a.test.js' }

Expand All @@ -24,7 +26,7 @@ it('rewrites proxyquire without noCallThru', () => {
jest.mock('common/Foo', () => () => <div />);
const { mapStateToProps, default: Page } = require('./PageContainer');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('rewrites proxyquire with noCallThru', () => {
Expand All @@ -39,7 +41,7 @@ it('rewrites proxyquire with noCallThru', () => {
jest.mock('common/Foo', () => () => <div />);
const Page = require('./PageContainer');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('rewrites proxyquire with require statement noCallThru', () => {
Expand All @@ -54,7 +56,7 @@ it('rewrites proxyquire with require statement noCallThru', () => {
jest.mock('lib', () => () => {});
const tracking = require('./tracking');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('supports variable reference object', () => {
Expand Down Expand Up @@ -94,7 +96,7 @@ it('supports variable reference object', () => {
t.end();
});
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('supports empty noCallThru', () => {
Expand All @@ -108,7 +110,7 @@ it('supports empty noCallThru', () => {
jest.mock('b', () => 'c');
const a = require('a');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('supports the `load` method', () => {
Expand All @@ -121,7 +123,7 @@ it('supports the `load` method', () => {
jest.mock('b', () => 'c');
const a = require('a');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('supports a chained `noCallThru().load()` call', () => {
Expand All @@ -134,7 +136,7 @@ it('supports a chained `noCallThru().load()` call', () => {
jest.mock('b', () => 'c');
const a = require('a');
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('supports named imports scoped to the variable name', () => {
Expand All @@ -151,7 +153,7 @@ it('supports named imports scoped to the variable name', () => {
const something = require('a');
});
`)
expect(mockedLogger).not.toHaveBeenCalled()
expect(logger).not.toHaveBeenCalled()
})

it('logs error when proxyquire mocks are not defined in the file', () => {
Expand All @@ -166,16 +168,7 @@ it('logs error when proxyquire mocks are not defined in the file', () => {
import mockedDeps from './someFile';
proxyquire.noCallThru()('./index', mockedDeps);
`)
expect(mockedLogger).toHaveBeenCalled()
})

it('logs error when type of mock is not known', () => {
const ast = j(`
import proxyquire from 'proxyquire';
proxyquire.noCallThru()('./index', () => {});
`)
proxyquireTransformer(fileInfo, j, ast)
expect(mockedLogger).toHaveBeenCalled()
expect(logger).toHaveBeenCalled()
})

it('logs error with multiple proxyquire to same file', () => {
Expand All @@ -196,5 +189,5 @@ it('logs error with multiple proxyquire to same file', () => {
'../page': mockedFailingRender,
});
`)
expect(mockedLogger).toHaveBeenCalled()
expect(logger).toHaveBeenCalled()
})