mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[dev-utils/withProcRunner] fix test that swallows promise rejection (#22342)
* [dev-utils/withProcRunner] fix tests that can silently fail * test async/sync withProcRunner functions
This commit is contained in:
parent
c7d451c9a2
commit
2700654940
3 changed files with 79 additions and 45 deletions
|
@ -1,3 +0,0 @@
|
|||
#! /usr/bin/node
|
||||
|
||||
console.log(process.argv.join(' '));
|
|
@ -1,42 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { ToolingLog } from '../../tooling_log';
|
||||
import { withProcRunner } from '../with_proc_runner';
|
||||
|
||||
describe('proc runner', () => {
|
||||
function runProc({ thing = '', procs }) {
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
procs.run('proc', {
|
||||
cmd: './proc',
|
||||
args: ['these', 'are', 'words'],
|
||||
});
|
||||
resolve(thing);
|
||||
}, 500);
|
||||
});
|
||||
}
|
||||
|
||||
it('passes procs to a function', async () => {
|
||||
await withProcRunner(new ToolingLog(), async procs => {
|
||||
await runProc({ procs });
|
||||
await procs.stop('proc');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch B.V. under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch B.V. licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
import { ToolingLog } from '../tooling_log';
|
||||
import { withProcRunner } from './with_proc_runner';
|
||||
import { ProcRunner } from './proc_runner';
|
||||
|
||||
it('passes proc runner to a function', async () => {
|
||||
await withProcRunner(new ToolingLog(), proc => {
|
||||
expect(proc).toBeInstanceOf(ProcRunner);
|
||||
});
|
||||
});
|
||||
|
||||
it('calls procRunner.teardown() if function returns synchronously', async () => {
|
||||
let teardownSpy;
|
||||
await withProcRunner(new ToolingLog(), proc => {
|
||||
teardownSpy = jest.spyOn(proc, 'teardown');
|
||||
});
|
||||
|
||||
expect(teardownSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('calls procRunner.teardown() if function throw synchronous error, and rejects with the error', async () => {
|
||||
const error = new Error('foo');
|
||||
let teardownSpy;
|
||||
|
||||
await expect(
|
||||
withProcRunner(new ToolingLog(), proc => {
|
||||
teardownSpy = jest.spyOn(proc, 'teardown');
|
||||
throw error;
|
||||
})
|
||||
).rejects.toThrowError(error);
|
||||
|
||||
expect(teardownSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('waits for promise to resolve before tearing down proc', async () => {
|
||||
let teardownSpy;
|
||||
|
||||
await withProcRunner(new ToolingLog(), async proc => {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
teardownSpy = jest.spyOn(proc, 'teardown');
|
||||
});
|
||||
|
||||
expect(teardownSpy).not.toBe(undefined);
|
||||
expect(teardownSpy).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('waits for promise to reject before tearing down proc and rejecting with the error', async () => {
|
||||
const error = new Error('foo');
|
||||
let teardownSpy;
|
||||
|
||||
await expect(
|
||||
withProcRunner(new ToolingLog(), async proc => {
|
||||
await new Promise(resolve => setTimeout(resolve, 500));
|
||||
teardownSpy = jest.spyOn(proc, 'teardown');
|
||||
throw error;
|
||||
})
|
||||
).rejects.toThrowError(error);
|
||||
|
||||
expect(teardownSpy).not.toBe(undefined);
|
||||
expect(teardownSpy).toHaveBeenCalled();
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue