-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: directly writes to stdio so jest does not swallow
- Loading branch information
Showing
7 changed files
with
131 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,77 @@ | ||
import { spyThese } from '../__helpers__/mocks' | ||
import { debug, wrapWithDebug, __setup } from './debug' | ||
import { debug, wrapWithDebug, __setup, warn } from './debug' | ||
|
||
const consoleSpies = spyThese(console, { | ||
log: () => void 0, | ||
warn: () => void 0, | ||
}) | ||
const stdoutSpy = jest.spyOn(process.stdout, 'write') | ||
const stderrSpy = jest.spyOn(process.stderr, 'write') | ||
|
||
beforeEach(() => { | ||
delete process.env.TS_JEST_DEBUG | ||
consoleSpies.mockClear() | ||
stderrSpy.mockClear() | ||
stdoutSpy.mockClear() | ||
}) | ||
afterAll(() => { | ||
stderrSpy.mockRestore() | ||
stdoutSpy.mockRestore() | ||
}) | ||
|
||
describe('debug', () => { | ||
it('should log when TS_JEST_DEBUG is truthy', () => { | ||
it('should log to stdout when TS_JEST_DEBUG is truthy', () => { | ||
process.env.TS_JEST_DEBUG = '1' | ||
__setup() | ||
debug('foo') | ||
expect(consoleSpies.log).toHaveBeenCalledTimes(1) | ||
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest:', 'foo') | ||
expect(stdoutSpy).toHaveBeenCalledTimes(1) | ||
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n') | ||
}) | ||
it('should NOT log when TS_JEST_DEBUG is falsy', () => { | ||
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => { | ||
process.env.TS_JEST_DEBUG = '' | ||
__setup() | ||
debug('foo') | ||
expect(consoleSpies.log).not.toHaveBeenCalled() | ||
expect(stdoutSpy).not.toHaveBeenCalled() | ||
}) | ||
it('should NOT log when TS_JEST_DEBUG is not set', () => { | ||
it('should NOT log to stdout when TS_JEST_DEBUG is not set', () => { | ||
delete process.env.TS_JEST_DEBUG | ||
__setup() | ||
debug('foo') | ||
expect(consoleSpies.log).not.toHaveBeenCalled() | ||
expect(stdoutSpy).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
describe('warn', () => { | ||
it('should log to stderr when TS_JEST_DEBUG is truthy', () => { | ||
process.env.TS_JEST_DEBUG = '1' | ||
__setup() | ||
warn('foo') | ||
expect(stderrSpy).toHaveBeenCalledTimes(1) | ||
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n') | ||
}) | ||
it('should log to stderr even when TS_JEST_DEBUG is falsy', () => { | ||
delete process.env.TS_JEST_DEBUG | ||
__setup() | ||
warn('foo') | ||
expect(stderrSpy).toHaveBeenCalledTimes(1) | ||
expect(stderrSpy).toHaveBeenCalledWith('ts-jest: foo\n') | ||
}) | ||
}) | ||
|
||
describe('wrapWithDebug', () => { | ||
const subject = (val: string) => `hello ${val}` | ||
const wrapAndCall = (val: string) => wrapWithDebug('foo', subject)(val) | ||
|
||
it('should log when TS_JEST_DEBUG is truthy', () => { | ||
it('should log to stdout when TS_JEST_DEBUG is truthy', () => { | ||
process.env.TS_JEST_DEBUG = '1' | ||
__setup() | ||
expect(wrapAndCall('bar')).toBe('hello bar') | ||
expect(consoleSpies.log).toHaveBeenCalledTimes(1) | ||
expect(consoleSpies.log).toHaveBeenCalledWith('ts-jest:', 'foo') | ||
expect(stdoutSpy).toHaveBeenCalledTimes(1) | ||
expect(stdoutSpy).toHaveBeenCalledWith('ts-jest: foo\n') | ||
}) | ||
it('should NOT log when TS_JEST_DEBUG is falsy', () => { | ||
it('should NOT log to stdout when TS_JEST_DEBUG is falsy', () => { | ||
process.env.TS_JEST_DEBUG = '' | ||
__setup() | ||
expect(wrapAndCall('bar')).toBe('hello bar') | ||
expect(consoleSpies.log).not.toHaveBeenCalled() | ||
expect(stdoutSpy).not.toHaveBeenCalled() | ||
}) | ||
it('should NOT log when TS_JEST_DEBUG is not set', () => { | ||
it('should NOT log to stdout when TS_JEST_DEBUG is not set', () => { | ||
delete process.env.TS_JEST_DEBUG | ||
__setup() | ||
expect(wrapAndCall('bar')).toBe('hello bar') | ||
expect(consoleSpies.log).not.toHaveBeenCalled() | ||
expect(stdoutSpy).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.