Skip to content

Commit e64af92

Browse files
clarify oxlint fixture ownership
Co-authored-by: Kent C. Dodds <me+github@kentcdodds.com>
1 parent 3df948d commit e64af92

2 files changed

Lines changed: 52 additions & 41 deletions

File tree

lint-rules/oxlint-test-utils.js

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -65,45 +65,60 @@ export async function writeOxlintFixture({
6565
}
6666

6767
export async function runOxlint(input) {
68-
const fixture =
69-
'filePath' in input && 'configPath' in input
70-
? input
71-
: await writeOxlintFixture(input)
72-
73-
const args = ['--config', fixture.configPath, '--format', 'json', fixture.filePath]
74-
75-
if (fixture.typeAware) args.unshift('--type-aware')
76-
77-
const result = await new Promise((resolve, reject) => {
78-
const child = spawn(oxlintBinary, args, {
79-
cwd: rootDirectory,
80-
stdio: ['ignore', 'pipe', 'pipe'],
68+
const fixtureWasProvided = 'filePath' in input && 'configPath' in input
69+
const fixture = fixtureWasProvided ? input : await writeOxlintFixture(input)
70+
71+
try {
72+
const args = [
73+
'--config',
74+
fixture.configPath,
75+
'--format',
76+
'json',
77+
fixture.filePath,
78+
]
79+
80+
if (fixture.typeAware) args.unshift('--type-aware')
81+
82+
const result = await new Promise((resolve, reject) => {
83+
const child = spawn(oxlintBinary, args, {
84+
cwd: rootDirectory,
85+
stdio: ['ignore', 'pipe', 'pipe'],
86+
})
87+
let stdout = ''
88+
let stderr = ''
89+
90+
child.stdout.on('data', (chunk) => {
91+
stdout += chunk
92+
})
93+
child.stderr.on('data', (chunk) => {
94+
stderr += chunk
95+
})
96+
child.on('error', reject)
97+
child.on('close', (exitCode) => {
98+
resolve({ exitCode, stderr, stdout })
99+
})
81100
})
82-
let stdout = ''
83-
let stderr = ''
84101

85-
child.stdout.on('data', (chunk) => {
86-
stdout += chunk
87-
})
88-
child.stderr.on('data', (chunk) => {
89-
stderr += chunk
90-
})
91-
child.on('error', reject)
92-
child.on('close', (exitCode) => {
93-
resolve({ exitCode, stderr, stdout })
94-
})
95-
})
102+
if (result.stderr) {
103+
throw new Error(result.stderr)
104+
}
96105

97-
if (result.stderr) {
98-
throw new Error(result.stderr)
99-
}
106+
if (result.exitCode && result.exitCode !== 0) {
107+
throw new Error(result.stdout || `Oxlint exited with code ${result.exitCode}`)
108+
}
100109

101-
if (result.exitCode && result.exitCode !== 0) {
102-
throw new Error(result.stdout || `Oxlint exited with code ${result.exitCode}`)
110+
const stdout = result.stdout.trim()
111+
return stdout ? JSON.parse(stdout) : { diagnostics: [] }
112+
} finally {
113+
if (!fixtureWasProvided) {
114+
await disposeOxlintFixture(fixture)
115+
}
103116
}
117+
}
104118

105-
const stdout = result.stdout.trim()
106-
return stdout ? JSON.parse(stdout) : { diagnostics: [] }
119+
export async function disposeOxlintFixture(fixture) {
120+
// oxlint-disable-next-line epic-web/no-manual-dispose
121+
await fixture[Symbol.asyncDispose]()
107122
}
108123

109124
export async function readLockfile() {

lint-rules/prefer-dispose-in-tests.test.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { describe, expect, it } from 'vitest'
22

33
import {
4+
disposeOxlintFixture,
45
runOxlint,
56
writeOxlintFixture,
67
} from './oxlint-test-utils.js'
78

8-
async function cleanupFixture(fixture) {
9-
// oxlint-disable-next-line epic-web/no-manual-dispose
10-
await fixture[Symbol.asyncDispose]()
11-
}
12-
139
describe('epic-web/prefer-dispose-in-tests', () => {
1410
it('reports lifecycle hooks that can move into a test body', async () => {
1511
const fixture = await writeOxlintFixture({
@@ -38,7 +34,7 @@ describe('epic-web/prefer-dispose-in-tests', () => {
3834
})
3935
expect(result.diagnostics[0].message).toContain('instead of beforeEach')
4036
} finally {
41-
await cleanupFixture(fixture)
37+
await disposeOxlintFixture(fixture)
4238
}
4339
})
4440

@@ -76,7 +72,7 @@ describe('epic-web/prefer-dispose-in-tests', () => {
7672
const result = await runOxlint(fixture)
7773
expect(result.diagnostics).toHaveLength(0)
7874
} finally {
79-
await cleanupFixture(fixture)
75+
await disposeOxlintFixture(fixture)
8076
}
8177
})
8278

@@ -106,7 +102,7 @@ describe('epic-web/prefer-dispose-in-tests', () => {
106102
expect(result.diagnostics).toHaveLength(1)
107103
expect(result.diagnostics[0].message).toContain('instead of beforeEach')
108104
} finally {
109-
await cleanupFixture(fixture)
105+
await disposeOxlintFixture(fixture)
110106
}
111107
})
112108
})

0 commit comments

Comments
 (0)