Skip to content

Commit b31db00

Browse files
committed
Improve test reliability.
1 parent ec9e0dc commit b31db00

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

test/utilities/anki-connect.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import { yankiModels } from '../../src/lib/model/model'
44
import { requestPermission } from '../../src/lib/utilities/anki-connect'
55
import { PLATFORM } from '../../src/lib/utilities/platform'
66

7+
/**
8+
* Launches Anki
9+
*/
10+
export async function openAnki(): Promise<void> {
11+
if (PLATFORM !== 'mac') {
12+
throw new Error('This function only works on Mac')
13+
}
14+
15+
const client = new YankiConnect({
16+
autoLaunch: 'immediately',
17+
})
18+
19+
// Sanity
20+
await client.miscellaneous.version()
21+
}
22+
723
/**
824
* Closes Anki by sending a quit command to the application.
925
* This function will wait until Anki is unreachable.
@@ -30,14 +46,16 @@ export async function closeAnki(): Promise<void> {
3046
await execa('sh', [
3147
'-c',
3248
"launchctl stop $(launchctl list | grep ankiweb | awk '{print $3}')",
33-
])
49+
]).catch(() => {
50+
// Ignore
51+
})
3452
})
3553
}
3654

3755
// Spin until it's done...
3856
while (permissionStatus !== 'ankiUnreachable') {
3957
await new Promise((resolve) => {
40-
setTimeout(resolve, 1000)
58+
setTimeout(resolve, 250)
4159
})
4260
permissionStatus = await requestPermission(client)
4361
}

test/utilities/global-setup.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { closeAnki, openAnki } from './anki-connect'
2+
3+
/**
4+
* Ensure Anki is running before tests run
5+
*/
6+
export async function setup() {
7+
await openAnki()
8+
}
9+
10+
/**
11+
* Clean up
12+
*/
13+
export async function teardown() {
14+
await closeAnki()
15+
}

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig({
1515
// Running tests concurrently across files can create race conditions in
1616
// total note counts that will cause assertions to fail.
1717
fileParallelism: false,
18+
globalSetup: './test/utilities/global-setup.ts',
1819
maxConcurrency: 1,
1920
maxWorkers: 1,
2021
projects: [

0 commit comments

Comments
 (0)