Skip to content

Commit 4dcdc39

Browse files
committed
test(attach): improve disconnect test
1 parent e28b0c6 commit 4dcdc39

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

packages/neovim/src/attach/attach.test.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,27 @@ describe('Nvim API', () => {
154154
expect(newLines).toEqual(['line1', 'line2']);
155155
});
156156

157-
it('emits "disconnect" after quit', done => {
157+
it('emits "disconnect" after quit', async () => {
158158
const disconnectMock = jest.fn();
159159
nvim.on('disconnect', disconnectMock);
160160
nvim.quit();
161161

162-
proc.on('close', () => {
163-
expect(disconnectMock.mock.calls.length).toBe(1);
164-
done();
162+
const closePromise = new Promise(resolve => {
163+
proc.on('close', resolve);
165164
});
166165

167-
// Event doesn't actually emit when we quit nvim, but when the child process is killed
168-
if (proc && proc.connected) {
169-
proc.disconnect();
170-
}
171-
});
166+
let timeout: NodeJS.Timeout;
167+
168+
await Promise.race([
169+
closePromise.then(() => clearTimeout(timeout)),
170+
new Promise((_, reject) => {
171+
timeout = setTimeout(
172+
() => reject(new Error('Timeout waiting for nvim to quit')),
173+
5000
174+
);
175+
}),
176+
]);
177+
178+
expect(disconnectMock).toHaveBeenCalledTimes(1);
179+
}, 10000);
172180
});

0 commit comments

Comments
 (0)