-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b08258
commit e746d54
Showing
2 changed files
with
115 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,119 @@ | ||
const { mock: mockChildProcess } = require('child_process') | ||
const { start } = require('./start') | ||
const { join } = require('path') | ||
const start = require('./start') | ||
|
||
jest.mock('ps-tree', () => (_, cb) => cb(null, [{ | ||
PID: 1 | ||
}, { | ||
PID: 2 | ||
}])) | ||
jest.spyOn(process, 'kill') | ||
|
||
describe('src/start', () => { | ||
let job | ||
let returnUrlAnswerAfter // ms | ||
const VALID_URL = 'http://localhost/valid' | ||
const INVALID_URL = 'http://localhost/invalid' | ||
let startExecuted = false | ||
|
||
beforeEach(() => { | ||
job = {} | ||
process.kill.mockClear() | ||
returnUrlAnswerAfter = 500 // ms | ||
job = { | ||
cwd: __dirname, | ||
startTimeout: 5000, | ||
start: 'start', | ||
url: [VALID_URL] | ||
} | ||
mockChildProcess({ | ||
api: 'exec', | ||
scriptPath: 'start', | ||
exec: () => { startExecuted = true }, | ||
close: false | ||
}) | ||
let firstFetch | ||
global.fetch = async (url) => { | ||
if (firstFetch === undefined) { | ||
firstFetch = Date.now() | ||
throw new Error('E_CONNECT failed') | ||
} else if (Date.now() - firstFetch < returnUrlAnswerAfter) { | ||
throw new Error('E_CONNECT failed') | ||
} | ||
if (url === VALID_URL) { | ||
return { status: 200 } | ||
} | ||
if (url === INVALID_URL) { | ||
return { status: 404 } | ||
} | ||
} | ||
}) | ||
|
||
it('detects equivalent script cwd\'s package.json', () => {}) | ||
it('runs command', async () => { | ||
await start(job) | ||
expect(startExecuted).toStrictEqual(true) | ||
}) | ||
|
||
it('runs command despite cwd\'s package.json', () => {}) | ||
it('detects equivalent script cwd\'s package.json', async () => { | ||
job.cwd = join(__dirname, '..') | ||
job.start = 'test' | ||
let executed = false | ||
mockChildProcess({ | ||
api: 'exec', | ||
scriptPath: 'npm', | ||
args: ['run', 'test'], | ||
exec: () => { executed = true }, | ||
close: false | ||
}) | ||
await start(job) | ||
expect(executed).toStrictEqual(true) | ||
}) | ||
|
||
it('runs command', () => {}) | ||
it('runs command despite cwd\'s package.json', async () => { | ||
job.cwd = join(__dirname, '..') | ||
job.start = 'test2' | ||
let executed = false | ||
mockChildProcess({ | ||
api: 'exec', | ||
scriptPath: 'test2', | ||
exec: () => { executed = true }, | ||
close: false | ||
}) | ||
await start(job) | ||
expect(executed).toStrictEqual(true) | ||
}) | ||
|
||
describe('waiting for URL to be available', () => { | ||
it('detects command termination and fails', () => {}) | ||
it('detects command termination and fails', async () => { | ||
job.start = 'close' | ||
mockChildProcess({ | ||
api: 'exec', | ||
scriptPath: 'close', | ||
exec: () => {}, | ||
close: true | ||
}) | ||
returnUrlAnswerAfter = 5000 | ||
job.startTimeout = 5000 | ||
await expect(start(job)).rejects.toThrowError(/Start command failed with exit code/) | ||
}) | ||
|
||
it('times out after expected limit and fails', () => {}) | ||
it('times out after expected limit and fails', async () => { | ||
returnUrlAnswerAfter = 5000 | ||
job.startTimeout = 500 | ||
await expect(start(job)).rejects.toThrowError(/Timeout while waiting for/) | ||
}) | ||
|
||
it('succeeds when URL is available', () => {}) | ||
it('times out after expected limit and fails (wrong URL)', async () => { | ||
job.startTimeout = 1000 | ||
job.url = [INVALID_URL] | ||
await expect(start(job)).rejects.toThrowError(/Timeout while waiting for/) | ||
}) | ||
}) | ||
|
||
it('stops the command by killing all children processes', () => {}) | ||
it('stops the command by killing all children processes', async () => { | ||
const started = await start(job) | ||
await started.stop() | ||
expect(process.kill).toHaveBeenCalledTimes(2) | ||
expect(process.kill).toHaveBeenCalledWith(1, 'SIGKILL') | ||
expect(process.kill).toHaveBeenCalledWith(2, 'SIGKILL') | ||
}) | ||
}) |