-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(tests): drop "global test setup"
Can't do "global beforeAll" because jest sucks.
- Loading branch information
Showing
9 changed files
with
141 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,12 @@ | ||
// Global test setup. Runs before each test. | ||
|
||
import * as testUtil from './testUtil'; | ||
import * as jest from '@jest/globals' | ||
// import * as jest from '@jest/globals' | ||
|
||
testUtil.findNvimOrFail(); | ||
|
||
let proc: ReturnType<typeof testUtil.startNvim>[0]; | ||
let nvim: ReturnType<typeof testUtil.startNvim>[1]; | ||
|
||
/** | ||
* Gets the current Nvim client being used in the current test. | ||
*/ | ||
export function getNvim() { | ||
return nvim!; | ||
} | ||
|
||
export function getNvimProc() { | ||
return proc!; | ||
} | ||
|
||
jest.beforeAll(() => { | ||
const testName = jest.expect.getState().testPath; | ||
console.log('xxxxxxxxxxxx BEFORE %O', testName); | ||
[proc, nvim] = testUtil.startNvim(true); | ||
}); | ||
|
||
jest.afterAll(() => { | ||
const testName = jest.expect.getState().currentTestName; | ||
console.log('xxxxxxxxxxxx AFTER %O', testName); | ||
testUtil.stopNvim(proc); | ||
testUtil.stopNvim(nvim); | ||
proc = undefined; | ||
nvim = undefined; | ||
}); | ||
// TODO: this doesn't work because jest sucks. use mocha instead. | ||
// jest.beforeAll(() => { | ||
// }); | ||
// jest.afterAll(() => { | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,78 @@ | ||
import * as cp from 'node:child_process'; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import * as jest from '@jest/globals'; | ||
import * as fs from 'node:fs'; | ||
import * as path from 'node:path'; | ||
import { NeovimClient } from './api/client'; | ||
import { attach } from './attach'; | ||
import { findNvim } from './utils/findNvim'; | ||
|
||
export function startNvim(): [cp.ChildProcessWithoutNullStreams, NeovimClient] | ||
export function startNvim(doAttach: false): [cp.ChildProcessWithoutNullStreams, undefined] | ||
export function startNvim(doAttach: true): [cp.ChildProcessWithoutNullStreams, NeovimClient] | ||
export function findNvimOrFail() { | ||
const minVersion = '0.9.5'; | ||
const found = findNvim({ minVersion }); | ||
if (found.matches.length === 0) { | ||
throw new Error(`nvim ${minVersion} not found`); | ||
} | ||
return found.matches[0].path; | ||
} | ||
|
||
const nvimPath = findNvimOrFail(); | ||
|
||
let proc: cp.ChildProcessWithoutNullStreams; | ||
let nvim: NeovimClient; | ||
|
||
export function startNvim(): [cp.ChildProcessWithoutNullStreams, NeovimClient]; | ||
export function startNvim( | ||
doAttach: false | ||
): [cp.ChildProcessWithoutNullStreams, undefined]; | ||
export function startNvim( | ||
doAttach: true | ||
): [cp.ChildProcessWithoutNullStreams, NeovimClient]; | ||
export function startNvim( | ||
doAttach: boolean = true | ||
): [cp.ChildProcessWithoutNullStreams, NeovimClient | undefined] { | ||
const proc = cp.spawn('nvim', ['-u', 'NONE', '--embed', '-n', '--noplugin'], { | ||
const testFile = jest.expect.getState().testPath?.replace(/.*[\\/]/, ''); | ||
const msg = `startNvim in test: ${testFile}`; | ||
// console.log(msg); | ||
if (process.env.NVIM_NODE_LOG_FILE) { | ||
const logfile = path.resolve(process.env.NVIM_NODE_LOG_FILE); | ||
fs.writeFileSync(logfile, `${msg}\n`, { flag: 'a' }); | ||
} | ||
|
||
proc = cp.spawn(nvimPath, ['-u', 'NONE', '--embed', '-n', '--noplugin'], { | ||
cwd: __dirname, | ||
}); | ||
if (!doAttach) { | ||
return [proc, undefined]; | ||
} | ||
const nvim = attach({ proc }); | ||
nvim = attach({ proc }); | ||
return [proc, nvim]; | ||
} | ||
|
||
export function stopNvim( | ||
proc_: cp.ChildProcessWithoutNullStreams | NeovimClient | ||
proc_?: cp.ChildProcessWithoutNullStreams | NeovimClient | ||
) { | ||
// Stop all (proc + client). | ||
if (!proc_) { | ||
if (proc) { | ||
stopNvim(proc); | ||
} | ||
if (nvim) { | ||
stopNvim(nvim); | ||
} | ||
return; | ||
} else if (proc_ instanceof NeovimClient) { | ||
} | ||
|
||
if (proc_ instanceof NeovimClient) { | ||
proc_.quit(); | ||
} else if (proc_ && proc_.connected) { | ||
proc_.disconnect(); | ||
} | ||
} | ||
|
||
export function findNvimOrFail() { | ||
const minVersion = '0.9.5'; | ||
const found = findNvim({ minVersion }); | ||
if (found.matches.length === 0) { | ||
throw new Error(`nvim ${minVersion} not found`); | ||
} | ||
return found.matches[0].path; | ||
} | ||
// jest.beforeAll(async () => { | ||
// [proc, nvim] = startNvim(); | ||
// }); | ||
// jest.afterAll(() => { | ||
// stopNvim(); | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters