Skip to content

Commit

Permalink
[keystore] Fix openHandle in Jest tests (#96671) (#96973)
Browse files Browse the repository at this point in the history
```
[2021-04-07T00:19:27Z] Jest did not exit one second after the test run has completed.
[2021-04-07T00:19:27Z]
[2021-04-07T00:19:27Z] This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
```

Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co>

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>

Co-authored-by: Tyler Smalley <tyler.smalley@elastic.co>
  • Loading branch information
kibanamachine and Tyler Smalley authored Apr 13, 2021
1 parent 2f85297 commit 89f5f61
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/cli_keystore/utils/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function question(question, options = {}) {
});

rl.question(questionPrompt, (value) => {
rl.close();
resolve(value);
});
});
Expand Down
36 changes: 14 additions & 22 deletions src/cli_keystore/utils/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
* Side Public License, v 1.
*/

import sinon from 'sinon';
import { PassThrough } from 'stream';

import { confirm, question } from './prompt';

describe('prompt', () => {
const sandbox = sinon.createSandbox();

let input;
let output;

Expand All @@ -23,30 +20,27 @@ describe('prompt', () => {
});

afterEach(() => {
sandbox.restore();
input.end();
output.end();
});

describe('confirm', () => {
it('prompts for question', async () => {
const onData = sandbox.stub(output, 'write');

confirm('my question', { output });
const write = jest.spyOn(output, 'write');

sinon.assert.calledOnce(onData);
process.nextTick(() => input.write('Y\n'));
await confirm('my question', { input, output });

const { args } = onData.getCall(0);
expect(args[0]).toEqual('my question [y/N] ');
expect(write).toHaveBeenCalledWith('my question [y/N] ');
});

it('prompts for question with default true', async () => {
const onData = sandbox.stub(output, 'write');

confirm('my question', { output, default: true });
const write = jest.spyOn(output, 'write');

sinon.assert.calledOnce(onData);
process.nextTick(() => input.write('Y\n'));
await confirm('my question', { input, output, default: true });

const { args } = onData.getCall(0);
expect(args[0]).toEqual('my question [Y/n] ');
expect(write).toHaveBeenCalledWith('my question [Y/n] ');
});

it('defaults to false', async () => {
Expand Down Expand Up @@ -87,14 +81,12 @@ describe('prompt', () => {

describe('question', () => {
it('prompts for question', async () => {
const onData = sandbox.stub(output, 'write');

question('my question', { output });
const write = jest.spyOn(output, 'write');

sinon.assert.calledOnce(onData);
process.nextTick(() => input.write('my answer\n'));
await question('my question', { input, output });

const { args } = onData.getCall(0);
expect(args[0]).toEqual('my question: ');
expect(write).toHaveBeenCalledWith('my question: ');
});

it('can be answered', async () => {
Expand Down

0 comments on commit 89f5f61

Please sign in to comment.