-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add unit tests with Jest * Convert code to ESNext * Replace all callbacks with Promises * Improve command API
- Loading branch information
Showing
21 changed files
with
8,435 additions
and
1,985 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
{ | ||
"extends": [ | ||
"plugin:shopify/es5", | ||
"plugin:shopify/node", | ||
"plugin:shopify/mocha" | ||
"plugin:shopify/esnext", | ||
"plugin:shopify/node" | ||
], | ||
"rules": { | ||
"no-console": 0, | ||
"comma-dangle": 0 | ||
}, | ||
"env": { | ||
"jest": true | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module.exports = { | ||
spawn: jest.fn(() => ({ | ||
on: jest.fn((evt, cb) => { | ||
if (evt === 'close') { | ||
return cb(); | ||
} | ||
return () => { /* noop*/ }; | ||
}), | ||
stdout: { | ||
setEncoding: jest.fn(), | ||
on: jest.fn() | ||
}, | ||
stderr: { | ||
on: jest.fn() | ||
}, | ||
addListener: jest.fn() | ||
})) | ||
}; |
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,35 @@ | ||
const themekit = require('../lib/themekit'); | ||
const runExecutable = require('../lib/run-executable'); | ||
|
||
jest.mock('../lib/run-executable'); | ||
|
||
describe('command', () => { | ||
test('forces no-update-notifier flag', async () => { | ||
const args = ['version', '--some-flag', '--no-update-notifier']; | ||
|
||
await themekit.command('version', { | ||
someFlag: true | ||
}); | ||
|
||
expect(runExecutable).toBeCalledWith(args, expect.any(String), null); | ||
}); | ||
|
||
test('passes cwd and logLevel properly to runExecutable', async () => { | ||
const cwd = process.cwd(); | ||
const logLevel = 'info'; | ||
|
||
await themekit.command('version', null, {cwd, logLevel}); | ||
|
||
expect(runExecutable).toBeCalledWith(expect.any(Array), cwd, logLevel); | ||
}); | ||
|
||
test('does not mutate input param', async () => { | ||
const flags = {someFlag: true}; | ||
const flagsCopy = JSON.parse(JSON.stringify(flags)); | ||
|
||
await themekit.command('version', flags); | ||
|
||
expect(flags).toMatchObject(flagsCopy); | ||
}); | ||
}); | ||
|
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,8 @@ | ||
const themekit = require('../lib/themekit'); | ||
const {version} = require('../lib/config'); | ||
|
||
test('successfully runs binary', async () => { | ||
global.process.stdout.write = jest.fn(); | ||
await themekit.command('version'); | ||
expect(global.process.stdout.write).toHaveBeenLastCalledWith(expect.stringContaining(version)); | ||
}); |
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,20 @@ | ||
const path = require('path'); | ||
|
||
const cfg = require('../lib/config'); | ||
const runExecutable = require('../lib/run-executable'); | ||
|
||
jest.mock('child_process'); | ||
|
||
describe('runExecutable', () => { | ||
test('spawns child process with correct arguments', async () => { | ||
const {spawn} = require('child_process'); | ||
|
||
const pathToExecutable = path.join(cfg.destination, cfg.binName); | ||
const args = ['arg1', 'arg2', 'arg3']; | ||
const cwd = process.cwd(); | ||
|
||
await runExecutable(args, cwd); | ||
|
||
expect(spawn).toBeCalledWith(pathToExecutable, args, {cwd}); | ||
}); | ||
}); |
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,111 @@ | ||
const fsMock = require('mock-fs'); | ||
const fs = require('fs'); | ||
const {cleanFile, getFlagArrayFromObject} = require('../lib/utils'); | ||
|
||
describe('getFlagArrayFromObject', () => { | ||
test('converts string flags correctly', () => { | ||
const input = { | ||
flagOne: 'value1', | ||
flagTwo: 'value2', | ||
}; | ||
const expectedOutput = ['--flag-one', 'value1', '--flag-two', 'value2']; | ||
|
||
const output = getFlagArrayFromObject(input); | ||
|
||
expect(output).toBeInstanceOf(Array); | ||
expect(output).toHaveLength(expectedOutput.length); | ||
expect(output).toEqual(expect.arrayContaining(expectedOutput)); | ||
}); | ||
|
||
test('converts boolean flags correctly', () => { | ||
const input = { | ||
flagOne: true, | ||
flagTwo: false, | ||
flagThree: true, | ||
}; | ||
const expectedOutput = ['--flag-one', '--flag-three']; | ||
|
||
const output = getFlagArrayFromObject(input); | ||
|
||
expect(output).toBeInstanceOf(Array); | ||
expect(output).toHaveLength(expectedOutput.length); | ||
expect(output).toEqual(expect.arrayContaining(expectedOutput)); | ||
}); | ||
|
||
test('correctly deconstructs ignoredFiles flag', () => { | ||
const input = { | ||
ignoredFiles: ['file1', 'file2'] | ||
}; | ||
const expectedOutput = ['--ignored-file', 'file1', '--ignored-file', 'file2']; | ||
|
||
const output = getFlagArrayFromObject(input); | ||
|
||
expect(output).toBeInstanceOf(Array); | ||
expect(output).toHaveLength(expectedOutput.length); | ||
expect(output).toEqual(expect.arrayContaining(expectedOutput)); | ||
}); | ||
|
||
test('correctly deconstructs files flag', () => { | ||
const input = { | ||
files: ['file1', 'file2'] | ||
}; | ||
const expectedOutput = ['file1', 'file2']; | ||
|
||
const output = getFlagArrayFromObject(input); | ||
|
||
expect(output).toBeInstanceOf(Array); | ||
expect(output).toHaveLength(expectedOutput.length); | ||
expect(output).toEqual(expect.arrayContaining(expectedOutput)); | ||
}); | ||
}); | ||
|
||
describe('cleanFile', () => { | ||
afterEach(() => { | ||
fsMock.restore(); | ||
}); | ||
|
||
test('successfully removes file if it exists', () => { | ||
fsMock({ | ||
'path/to/executable': { | ||
'my-exec': '...', | ||
} | ||
}); | ||
|
||
const unlink = jest.spyOn(fs, 'unlinkSync'); | ||
const pathToExecutable = 'path/to/executable/my-exec'; | ||
|
||
function removeFile() { | ||
cleanFile(pathToExecutable); | ||
} | ||
function removeFileAndAccess() { | ||
cleanFile(pathToExecutable); | ||
fs.statSync(pathToExecutable); | ||
} | ||
|
||
expect(removeFile).not.toThrow(); | ||
expect(removeFileAndAccess).toThrow('ENOENT'); | ||
expect(unlink).toBeCalledWith(pathToExecutable); | ||
}); | ||
|
||
test('does not throw if path does not exist', () => { | ||
fsMock({ | ||
'path/to/executable': { | ||
'not-my-exec': '...', | ||
} | ||
}); | ||
|
||
const unlink = jest.spyOn(fs, 'unlinkSync'); | ||
const pathToExecutable = 'path/to/executable/my-exec'; | ||
|
||
function removeFile() { | ||
cleanFile(pathToExecutable); | ||
} | ||
function access() { | ||
fs.statSync(pathToExecutable); | ||
} | ||
|
||
expect(unlink).toBeCalledWith(pathToExecutable); | ||
expect(removeFile).not.toThrow(); | ||
expect(access).toThrow('ENOENT'); | ||
}); | ||
}); |
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,3 +1 @@ | ||
module.exports = { | ||
command: require('./lib/command') | ||
}; | ||
module.exports = require('./lib/themekit'); |
Oops, something went wrong.