Skip to content

Commit dd63b00

Browse files
authored
Fix code coverage (#61)
Killing a child process prevents Node.js from collecting code coverage. The language server process can be stopped gracefully when using stdio, but not when using ipc.
1 parent 752400a commit dd63b00

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

test/index.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import assert from 'node:assert/strict'
66
import {spawn} from 'node:child_process'
7+
import process from 'node:process'
78
import fs from 'node:fs/promises'
89
import {afterEach, test} from 'node:test'
910
import {fileURLToPath} from 'node:url'
@@ -773,23 +774,35 @@ function cleanStack(stack, max) {
773774
*
774775
* @param {string} serverFilePath The path to the language server relative to
775776
* this test file.
776-
* @param cwd The cwd to use for the process relative to this test file.
777+
* @param relativeCwd The cwd to use for the process relative to this test file.
777778
*/
778-
function startLanguageServer(serverFilePath, cwd = './') {
779-
const proc = spawn(
780-
'node',
781-
[fileURLToPath(new URL(serverFilePath, import.meta.url)), '--node-ipc'],
782-
{
783-
cwd: new URL(cwd, import.meta.url),
779+
function startLanguageServer(serverFilePath, relativeCwd = './') {
780+
const bin = fileURLToPath(new URL(serverFilePath, import.meta.url))
781+
const cwd = new URL(relativeCwd, import.meta.url)
782+
783+
// Using ipc is useful for debugging. This allows logging in the language
784+
// server.
785+
// Enabling this breaks code coverage
786+
// https://github.com/bcoe/c8/issues/189
787+
if (process.argv.includes('--ipc')) {
788+
const proc = spawn('node', [bin, '--node-ipc'], {
789+
cwd,
784790
stdio: [null, 'inherit', 'inherit', 'ipc']
785-
}
786-
)
787-
connection = createProtocolConnection(
788-
new IPCMessageReader(proc),
789-
new IPCMessageWriter(proc)
790-
)
791+
})
792+
connection = createProtocolConnection(
793+
new IPCMessageReader(proc),
794+
new IPCMessageWriter(proc)
795+
)
796+
connection.onDispose(() => {
797+
proc.kill()
798+
})
799+
} else {
800+
const proc = spawn('node', [bin, '--stdio'], {cwd})
801+
connection = createProtocolConnection(proc.stdout, proc.stdin)
802+
}
803+
791804
connection.onDispose(() => {
792-
proc.kill()
805+
connection.end()
793806
})
794807
connection.listen()
795808
}

0 commit comments

Comments
 (0)