Skip to content

Commit 7d1e1fe

Browse files
committed
refactor(tests): global test setup
1 parent 77282c5 commit 7d1e1fe

File tree

8 files changed

+59
-82
lines changed

8 files changed

+59
-82
lines changed

packages/neovim/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
"json",
8787
"node"
8888
],
89+
"setupFilesAfterEnv": [
90+
"<rootDir>/src/testSetup.ts"
91+
],
8992
"testEnvironment": "node",
9093
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
9194
"coverageDirectory": "./coverage/",

packages/neovim/src/api/Buffer.test.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env jest */
2-
import * as testUtil from '../testUtil';
2+
import * as testSetup from '../testSetup';
33

44
function wait(ms: number): Promise<void> {
55
return new Promise(resolve => {
@@ -10,7 +10,7 @@ function wait(ms: number): Promise<void> {
1010
}
1111

1212
describe('Buffer API', () => {
13-
let nvim: ReturnType<typeof testUtil.getNvim>;
13+
let nvim: ReturnType<typeof testSetup.getNvim>;
1414

1515
// utility to allow each test to be run in its
1616
// own buffer
@@ -32,12 +32,7 @@ describe('Buffer API', () => {
3232
}
3333

3434
beforeAll(async () => {
35-
testUtil.startNvim2();
36-
nvim = testUtil.getNvim();
37-
});
38-
39-
afterAll(() => {
40-
testUtil.stopNvim2();
35+
nvim = testSetup.getNvim();
4136
});
4237

4338
it(
@@ -384,15 +379,10 @@ describe('Buffer API', () => {
384379
});
385380

386381
describe('Buffer event updates', () => {
387-
let nvim: ReturnType<typeof testUtil.getNvim>;
382+
let nvim: ReturnType<typeof testSetup.getNvim>;
388383

389384
beforeAll(async () => {
390-
testUtil.startNvim2();
391-
nvim = testUtil.getNvim();
392-
});
393-
394-
afterAll(() => {
395-
testUtil.stopNvim2();
385+
nvim = testSetup.getNvim();
396386
});
397387

398388
beforeEach(async () => {

packages/neovim/src/api/Neovim.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
/* eslint-env jest */
22
import * as path from 'node:path';
33
import { Neovim } from './Neovim';
4-
import * as testUtil from '../testUtil';
4+
import * as testSetup from '../testSetup';
55

66
describe('Neovim API', () => {
7-
let nvim: ReturnType<typeof testUtil.getNvim>;
7+
let nvim: ReturnType<typeof testSetup.getNvim>;
88

99
beforeAll(async () => {
10-
testUtil.startNvim2();
11-
nvim = testUtil.getNvim();
12-
});
13-
14-
afterAll(() => {
15-
testUtil.stopNvim2();
10+
nvim = testSetup.getNvim();
1611
});
1712

1813
it('sets transport when initialized', () => {

packages/neovim/src/api/Tabpage.test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
/* eslint-env jest */
2-
import * as testUtil from '../testUtil';
2+
import * as testSetup from '../testSetup';
33

44
describe('Tabpage API', () => {
5-
let nvim: ReturnType<typeof testUtil.getNvim>;
5+
let nvim: ReturnType<typeof testSetup.getNvim>;
66

77
beforeAll(async () => {
8-
testUtil.startNvim2();
9-
nvim = testUtil.getNvim();
8+
nvim = testSetup.getNvim();
109
});
1110

12-
afterAll(() => {
13-
testUtil.stopNvim2();
14-
});
15-
16-
beforeEach(() => {});
17-
1811
it('gets the current Tabpage', async () => {
1912
const tabpage = await nvim.tabpage;
2013
expect(tabpage).toBeInstanceOf(nvim.Tabpage);

packages/neovim/src/api/Window.test.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
/* eslint-env jest */
2-
import * as testUtil from '../testUtil';
2+
import * as testSetup from '../testSetup';
33

44
describe('Window API', () => {
5-
let nvim: ReturnType<typeof testUtil.getNvim>;
5+
let nvim: ReturnType<typeof testSetup.getNvim>;
66

77
beforeAll(async () => {
8-
testUtil.startNvim2();
9-
nvim = testUtil.getNvim();
8+
nvim = testSetup.getNvim();
109
});
1110

12-
afterAll(() => {
13-
testUtil.stopNvim2();
14-
});
15-
16-
beforeEach(() => {});
17-
1811
it('gets the current Window', async () => {
1912
const win = await nvim.window;
2013
expect(win).toBeInstanceOf(nvim.Window);

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
import { attach } from './attach';
33
import { Logger } from '../utils/logger';
44
import * as testUtil from '../testUtil';
5+
import * as testSetup from '../testSetup';
56

67
describe('Nvim API', () => {
7-
let nvim: ReturnType<typeof testUtil.getNvim>;
8+
let nvim: ReturnType<typeof testSetup.getNvim>;
89
let requests;
910
let notifications;
1011

1112
beforeAll(async () => {
12-
testUtil.startNvim2();
13-
nvim = testUtil.getNvim();
13+
nvim = testSetup.getNvim();
1414

1515
nvim.on('request', (method, args, resp) => {
1616
requests.push({ method, args });
@@ -21,10 +21,6 @@ describe('Nvim API', () => {
2121
});
2222
});
2323

24-
afterAll(() => {
25-
testUtil.stopNvim2();
26-
});
27-
2824
beforeEach(() => {
2925
requests = [];
3026
notifications = [];
@@ -125,7 +121,7 @@ describe('Nvim API', () => {
125121
});
126122

127123
it('emits "disconnect" after quit', done => {
128-
const proc = testUtil.getNvimProc();
124+
const proc = testSetup.getNvimProc();
129125
const disconnectMock = jest.fn();
130126
nvim.on('disconnect', disconnectMock);
131127
nvim.quit();

packages/neovim/src/testSetup.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
11
// Global test setup. Runs before each test.
22

3-
import { findNvimOrFail } from './testUtil';
3+
import * as testUtil from './testUtil';
4+
import * as jest from '@jest/globals'
45

5-
findNvimOrFail();
6+
testUtil.findNvimOrFail();
7+
8+
let proc: ReturnType<typeof testUtil.startNvim>[0];
9+
let nvim: ReturnType<typeof testUtil.startNvim>[1];
10+
11+
/**
12+
* Gets the current Nvim client being used in the current test.
13+
*/
14+
export function getNvim() {
15+
return nvim!;
16+
}
17+
18+
export function getNvimProc() {
19+
return proc!;
20+
}
21+
22+
jest.beforeAll(() => {
23+
const testName = jest.expect.getState().testPath;
24+
console.log('xxxxxxxxxxxx BEFORE %O', testName);
25+
[proc, nvim] = testUtil.startNvim(true);
26+
});
27+
28+
jest.afterAll(() => {
29+
const testName = jest.expect.getState().currentTestName;
30+
console.log('xxxxxxxxxxxx AFTER %O', testName);
31+
testUtil.stopNvim(proc);
32+
testUtil.stopNvim(nvim);
33+
proc = undefined;
34+
nvim = undefined;
35+
});

packages/neovim/src/testUtil.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,9 @@ import { NeovimClient } from './api/client';
33
import { attach } from './attach';
44
import { findNvim } from './utils/findNvim';
55

6-
let proc: cp.ChildProcessWithoutNullStreams;
7-
let nvim: NeovimClient;
8-
9-
/**
10-
* Gets the current Nvim client being used in the current test.
11-
*/
12-
export function getNvim(): NeovimClient {
13-
return nvim;
14-
}
15-
16-
export function getNvimProc(): cp.ChildProcessWithoutNullStreams {
17-
return proc;
18-
}
19-
6+
export function startNvim(): [cp.ChildProcessWithoutNullStreams, NeovimClient]
7+
export function startNvim(doAttach: false): [cp.ChildProcessWithoutNullStreams, undefined]
8+
export function startNvim(doAttach: true): [cp.ChildProcessWithoutNullStreams, NeovimClient]
209
export function startNvim(
2110
doAttach: boolean = true
2211
): [cp.ChildProcessWithoutNullStreams, NeovimClient | undefined] {
@@ -33,23 +22,15 @@ export function startNvim(
3322
export function stopNvim(
3423
proc_: cp.ChildProcessWithoutNullStreams | NeovimClient
3524
) {
36-
if (proc_ instanceof NeovimClient) {
25+
if (!proc_) {
26+
return;
27+
} else if (proc_ instanceof NeovimClient) {
3728
proc_.quit();
3829
} else if (proc_ && proc_.connected) {
3930
proc_.disconnect();
4031
}
4132
}
4233

43-
export function startNvim2(): void {
44-
[proc, nvim] = startNvim();
45-
}
46-
47-
// TODO: use jest beforeAll/afterAll instead of requiring the tests to do this explicitly.
48-
export function stopNvim2(): void {
49-
stopNvim(proc);
50-
stopNvim(nvim);
51-
}
52-
5334
export function findNvimOrFail() {
5435
const minVersion = '0.9.5';
5536
const found = findNvim({ minVersion });
@@ -58,7 +39,3 @@ export function findNvimOrFail() {
5839
}
5940
return found.matches[0].path;
6041
}
61-
62-
// beforeAll(async () => {
63-
// [proc, nvim] = testUtil.startNvim();
64-
// });

0 commit comments

Comments
 (0)