-
Notifications
You must be signed in to change notification settings - Fork 25
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
Add more complete unit tests #28
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d88af0
Add tests to validate config setting per platform
erickzhao 09fc8b7
Add test to validate installer
erickzhao 6391342
Fix logger not working with multiple pararmeters
erickzhao 311e03d
Add logger mocks and tests
erickzhao 04f7782
Change name of logger param
erickzhao 9ed37c2
Clean tests up a bit more
erickzhao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-disable no-use-before-define */ | ||
const src = jest.fn(() => binWrapperObject); | ||
const dest = jest.fn(() => binWrapperObject); | ||
const path = jest.fn(() => binWrapperObject); | ||
const run = jest.fn((_, cb) => { | ||
cb(); | ||
return binWrapperObject; | ||
}); | ||
const use = jest.fn(() => binWrapperObject); | ||
|
||
const binWrapperObject = {src, dest, path, run, use}; | ||
|
||
module.exports = jest.fn().mockImplementation(() => binWrapperObject); | ||
module.exports.src = src; | ||
module.exports.dest = dest; | ||
module.exports.path = path; | ||
module.exports.run = run; | ||
module.exports.use = use; |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
let oldPlatform; | ||
|
||
describe('config', () => { | ||
beforeEach(() => { | ||
oldPlatform = process.platform; | ||
}); | ||
|
||
afterEach(() => { | ||
Object.defineProperty(process, 'platform', { | ||
value: oldPlatform | ||
}); | ||
}); | ||
|
||
test('appends .exe to binary if win32 platform', () => { | ||
Object.defineProperty(process, 'platform', { | ||
value: 'win32' | ||
}); | ||
|
||
// require config after setting mock platform | ||
const config = require('../lib/config'); | ||
|
||
expect(config.binName).toBe('theme.exe'); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// these exports only exist in the mock | ||
const {src, dest, path, run, use} = require('bin-wrapper'); | ||
|
||
const install = require('../lib/install'); | ||
|
||
jest.mock('../lib/utils'); | ||
jest.mock('../lib/logger', () => { | ||
return () => ({ | ||
error: jest.fn(), | ||
info: jest.fn(), | ||
silly: jest.fn() | ||
}); | ||
}); | ||
jest.mock('../lib/config', () => { | ||
return { | ||
baseURL: 'example.com', | ||
version: '0.0.0', | ||
destination: 'myDir', | ||
binName: 'bin' | ||
}; | ||
}); | ||
|
||
describe('install', () => { | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('returns a promise with undefined return value', () => { | ||
expect(install()).resolves.toBeUndefined(); | ||
}); | ||
|
||
test('correctly sources bin files', async () => { | ||
await install(); | ||
|
||
expect(src).toHaveBeenCalledWith('example.com/v0.0.0/darwin-amd64/theme', 'darwin'); | ||
expect(src).toHaveBeenCalledWith('example.com/v0.0.0/linux-386/theme', 'linux'); | ||
expect(src).toHaveBeenCalledWith('example.com/v0.0.0/linux-amd64/theme', 'linux', 'x64'); | ||
expect(src).toHaveBeenCalledWith('example.com/v0.0.0/windows-386/theme.exe', 'win32'); | ||
expect(src).toHaveBeenCalledWith('example.com/v0.0.0/windows-amd64/theme.exe', 'win32', 'x64'); | ||
expect(src).toHaveBeenCalledTimes(5); | ||
}); | ||
|
||
test('correctly applies destination', async () => { | ||
await install(); | ||
|
||
expect(dest).toHaveBeenCalledWith('myDir'); | ||
expect(dest).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('correctly applies binary name', async () => { | ||
await install(); | ||
expect(use).toHaveBeenCalledWith('bin'); | ||
expect(use).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('gets path', async () => { | ||
await install(); | ||
expect(path).toHaveBeenCalledWith(); | ||
expect(path).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('runs after installation', async () => { | ||
await install(); | ||
expect(run).toHaveBeenCalledWith(['version'], expect.any(Function)); | ||
expect(run).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('runs after installation', async () => { | ||
await install(); | ||
expect(run).toHaveBeenCalledWith(['version'], expect.any(Function)); | ||
expect(run).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test('rejects promise if error in installation run', () => { | ||
const errorMessage = 'some err'; | ||
run.mockImplementationOnce((_, cb) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of |
||
cb(errorMessage); | ||
}); | ||
|
||
expect(install()).rejects.toMatch(errorMessage); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const logger = require('../lib/logger'); | ||
|
||
const originalConsole = global.console; | ||
|
||
describe('logger', () => { | ||
beforeAll(() => { | ||
global.console = {log: jest.fn()}; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
global.console = originalConsole; | ||
}); | ||
|
||
test('imported function returns logger functions', () => { | ||
const log = logger(); | ||
expect(log).toHaveProperty('error', expect.any(Function)); | ||
expect(log).toHaveProperty('info', expect.any(Function)); | ||
expect(log).toHaveProperty('silly', expect.any(Function)); | ||
}); | ||
|
||
test('logs error at log level 0', () => { | ||
const log = logger('silent'); | ||
const str = 'hello'; | ||
const {error, info, silly} = log; | ||
|
||
error(str); | ||
info(str, str); | ||
silly(str, str, str); | ||
|
||
expect(log.level).toBe(0); | ||
expect(console.log).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test('logs error at log level 1', () => { | ||
const log = logger('error'); | ||
const str = 'hello'; | ||
const {error, info, silly} = log; | ||
|
||
error(str); | ||
expect(console.log).toHaveBeenNthCalledWith(1, str); | ||
info(str, str); | ||
silly(str, str, str); | ||
expect(console.log).toHaveBeenCalledTimes(1); | ||
|
||
expect(log.level).toBe(1); | ||
}); | ||
|
||
test('logs error and info at log level 2', () => { | ||
const log = logger('info'); | ||
const str = 'hello'; | ||
const {error, info, silly} = log; | ||
|
||
error(str); | ||
expect(console.log).toHaveBeenNthCalledWith(1, str); | ||
info(str, str); | ||
expect(console.log).toHaveBeenNthCalledWith(2, str, str); | ||
silly(str, str, str); | ||
expect(console.log).toHaveBeenCalledTimes(2); | ||
|
||
expect(log.level).toBe(2); | ||
}); | ||
|
||
test('logs error, info, and silly at log level 3', () => { | ||
const log = logger('silly'); | ||
const str = 'hello'; | ||
const {error, info, silly} = log; | ||
|
||
error(str); | ||
expect(console.log).toHaveBeenNthCalledWith(1, str); | ||
info(str, str); | ||
expect(console.log).toHaveBeenNthCalledWith(2, str, str); | ||
silly(str, str, str); | ||
expect(console.log).toHaveBeenNthCalledWith(3, str, str, str); | ||
expect(console.log).toHaveBeenCalledTimes(3); | ||
|
||
expect(log.level).toBe(3); | ||
}); | ||
|
||
test('defaults to level 2 without input parameters', () => { | ||
const log = logger(); | ||
const str = 'hello'; | ||
const {error, info, silly} = log; | ||
|
||
error(str); | ||
expect(console.log).toHaveBeenNthCalledWith(1, str); | ||
info(str, str); | ||
expect(console.log).toHaveBeenNthCalledWith(2, str, str); | ||
silly(str, str, str); | ||
expect(console.log).toHaveBeenCalledTimes(2); | ||
|
||
expect(log.level).toBe(2); | ||
}); | ||
}); |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't binWrapperObject throw a ReferenceError at this point since
const
variables are not hoisted and is binWrapperObject is declared below?Or I guess it is not referenced until this callback is called... in which case binWrapperObject is declared in the parent scope? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly not sure at all. I just tried stuff until it worked for this one, which was tricky since I needed the mutually recursive definitions. In any case, the mock seems to behave as expected. 🤷🏻♂️