Skip to content

Commit a7d6c78

Browse files
authored
build(tests): type check packages/neovim/*.test.ts #411
1 parent a334f3d commit a7d6c78

File tree

7 files changed

+41
-28
lines changed

7 files changed

+41
-28
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env jest */
22
import * as path from 'node:path';
3+
import assert from 'node:assert';
34
import * as testUtil from '../testUtil';
45

56
describe('Neovim API', () => {
@@ -25,7 +26,7 @@ describe('Neovim API', () => {
2526

2627
// switch buffers
2728
[nvim.buffer] = buffers;
28-
expect(await nvim.buffer.name).toMatch(/hello\.txt$/);
29+
expect(await (await nvim.buffer).name).toMatch(/hello\.txt$/);
2930
});
3031

3132
it('can list runtimepaths', async () => {
@@ -218,7 +219,7 @@ describe('Neovim API', () => {
218219
const numWindows = (await nvim.windows).length;
219220
const buffer = await nvim.createBuffer(false, false);
220221
expect(await nvim.buffers).toHaveLength(numBuffers + 1);
221-
222+
assert(typeof buffer !== 'number');
222223
const floatingWindow = await nvim.openWindow(buffer, true, {
223224
relative: 'editor',
224225
row: 5,
@@ -227,22 +228,23 @@ describe('Neovim API', () => {
227228
height: 50,
228229
});
229230
expect(await nvim.windows).toHaveLength(numWindows + 1);
230-
231+
assert(typeof floatingWindow !== 'number');
231232
await nvim.windowClose(floatingWindow, true);
232233
expect(await nvim.windows).toHaveLength(numWindows);
233234
});
234235

235236
it('resizes a window', async () => {
236237
const numWindows = (await nvim.windows).length;
237238
const buffer = await nvim.createBuffer(false, false);
238-
239+
assert(typeof buffer !== 'number');
239240
const floatingWindow = await nvim.openWindow(buffer, true, {
240241
relative: 'editor',
241242
row: 5,
242243
col: 5,
243244
width: 10,
244245
height: 10,
245246
});
247+
assert(typeof floatingWindow !== 'number');
246248
expect(await nvim.windows).toHaveLength(numWindows + 1);
247249
expect(await floatingWindow.height).toBe(10);
248250
expect(await floatingWindow.width).toBe(10);

packages/neovim/src/api/Neovim.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export class Neovim extends BaseApi {
169169
/**
170170
* Sets the current buffer
171171
*/
172-
set buffer(buffer: AsyncBuffer) {
172+
set buffer(buffer: AsyncBuffer | Buffer) {
173173
this.request(`${this.prefix}set_current_buf`, [buffer]);
174174
}
175175

@@ -232,7 +232,7 @@ export class Neovim extends BaseApi {
232232
/**
233233
* Sets the current tabpage
234234
*/
235-
set tabpage(tabpage: AsyncTabpage) {
235+
set tabpage(tabpage: AsyncTabpage | Tabpage) {
236236
this.request(`${this.prefix}set_current_tabpage`, [tabpage]);
237237
}
238238

@@ -259,7 +259,7 @@ export class Neovim extends BaseApi {
259259
*
260260
* @param win Window handle
261261
*/
262-
set window(win: AsyncWindow) {
262+
set window(win: AsyncWindow | Window) {
263263
if (win instanceof Window) this.setWindow(win);
264264
else win.then(win => this.setWindow(win));
265265
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-env jest */
22
import * as testUtil from '../testUtil';
3+
import type { Tabpage } from './Tabpage';
34

45
describe('Tabpage API', () => {
56
let nvim: ReturnType<typeof testUtil.startNvim>[1];
@@ -18,7 +19,7 @@ describe('Tabpage API', () => {
1819
});
1920

2021
describe('Normal API calls', () => {
21-
let tabpage;
22+
let tabpage: Tabpage;
2223

2324
beforeEach(async () => {
2425
tabpage = await nvim.tabpage;
@@ -68,10 +69,10 @@ describe('Tabpage API', () => {
6869
it('logs an error when calling `getOption`', () => {
6970
const spy = jest.spyOn(tabpage.logger, 'error');
7071

71-
tabpage.getOption('option');
72+
tabpage.getOption();
7273
expect(spy.mock.calls.length).toBe(1);
7374

74-
tabpage.setOption('option', 'value');
75+
tabpage.setOption();
7576
expect(spy.mock.calls.length).toBe(2);
7677
spy.mockClear();
7778
});

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-env jest */
2+
import assert from 'node:assert';
23
import * as testUtil from '../testUtil';
4+
import type { Window } from './Window';
35

46
describe('Window API', () => {
57
let nvim: ReturnType<typeof testUtil.startNvim>[1];
@@ -24,7 +26,7 @@ describe('Window API', () => {
2426
});
2527

2628
describe('Normal API calls', () => {
27-
let win;
29+
let win: Window;
2830

2931
beforeEach(async () => {
3032
win = await nvim.window;
@@ -51,7 +53,7 @@ describe('Window API', () => {
5153
});
5254

5355
it('has same cursor position after appending a line to buffer', async () => {
54-
await win.buffer.append(['test']);
56+
await (await win.buffer).append(['test']);
5557
expect(await win.buffer.lines).toEqual(['', 'test']);
5658
expect(await win.cursor).toEqual([1, 0]);
5759
});
@@ -94,7 +96,7 @@ describe('Window API', () => {
9496
});
9597

9698
it('has the right window positions in display cells', async () => {
97-
let windows;
99+
let windows: Awaited<typeof nvim.windows>;
98100
nvim.command('vsplit');
99101

100102
// XXX If we re-use `win` without a new call to `nvim.window`,
@@ -126,6 +128,7 @@ describe('Window API', () => {
126128
expect(await win.getOption('list')).toBe(true);
127129
win.setOption('list', false);
128130
expect(await win.getOption('list')).toBe(false);
131+
assert(list !== undefined);
129132
// Restore option
130133
win.setOption('list', list);
131134
expect(await win.getOption('list')).toBe(list);
@@ -167,7 +170,7 @@ describe('Window API', () => {
167170
});
168171

169172
it.skip('gets current lines in buffer', async () => {
170-
expect(await nvim.window.buffer.lines).toEqual(['test']);
173+
expect(await (await nvim.window.buffer).lines).toEqual(['test']);
171174
});
172175
});
173176
});

packages/neovim/src/host/NvimPlugin.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ describe('NvimPlugin', () => {
128128
() => {},
129129
getFakeNvimClient()
130130
);
131+
// @ts-expect-error Intentionally passing empty array for command arguments.
131132
plugin.registerCommand('MyCommand', [], {});
132133
expect(Object.keys(plugin.commands)).toHaveLength(0);
133134
});
@@ -175,7 +176,7 @@ describe('NvimPlugin', () => {
175176
() => {},
176177
getFakeNvimClient()
177178
);
178-
const fn = arg => arg;
179+
const fn = (arg: any) => arg;
179180

180181
plugin.registerAutocmd('BufWritePre', fn, { pattern: '*', sync: true });
181182
plugin.registerCommand('MyCommand', fn, { sync: true });

packages/neovim/src/plugin/plugin.test.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { command as Command } from './command';
66
import { autocmd as Autocmd } from './autocmd';
77
import { getFakeNvimClient } from '../testUtil';
88

9-
const instantiateOrRun = (Fn, ...args) => {
9+
const instantiateOrRun = (Fn: ReturnType<typeof Plugin>, ...args: any[]) => {
1010
try {
1111
return new Fn(...args);
1212
} catch (err) {
@@ -36,13 +36,16 @@ describe('Plugin class decorator', () => {
3636
});
3737

3838
it('decorates class methods', () => {
39-
class MyClass {}
40-
MyClass.prototype.testF = () => {};
41-
MyClass.prototype.testC = () => {};
42-
MyClass.prototype.testA = () => {};
39+
class MyClass {
40+
testF() {}
41+
42+
testC() {}
43+
44+
testA() {}
45+
}
4346

4447
// This is how (closeish) babel applies decorators
45-
FunctionDecorator('TestF', { eval: 'test', range: 'test' })(
48+
FunctionDecorator('TestF', { eval: 'test', range: [1, 10] })(
4649
MyClass.prototype,
4750
'testF'
4851
);
@@ -85,15 +88,18 @@ describe('Plugin class decorator', () => {
8588
expect(pluginObject.registerFunction).toHaveBeenCalledWith(
8689
'TestF',
8790
[instance, MyClass.prototype.testF],
88-
{ sync: false, eval: 'test', range: 'test' }
91+
{ sync: false, eval: 'test', range: [1, 10] }
8992
);
9093
});
9194

9295
it('generates specs from decorated methods', () => {
93-
class MyClass {}
94-
MyClass.prototype.testF = () => {};
95-
MyClass.prototype.testC = () => {};
96-
MyClass.prototype.testA = () => {};
96+
class MyClass {
97+
testF() {}
98+
99+
testC() {}
100+
101+
testA() {}
102+
}
97103

98104
// This is how (closeish) babel applies decorators
99105
FunctionDecorator('TestF')(MyClass.prototype, 'testF');

packages/neovim/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
"compilerOptions": {
44
"outDir": "./lib"
55
},
6-
"include": ["src", "**/api/Buffer.test.ts"],
7-
"exclude": ["node_modules", "__tests__", "**/*.test.ts", "examples", "lib"]
6+
"include": ["src"],
7+
"exclude": ["node_modules", "examples", "lib"]
88
}

0 commit comments

Comments
 (0)