Skip to content

Commit 8d354f2

Browse files
fix(test): set cache path in test setup
Co-authored-by: me <me@kentcdodds.com>
1 parent 563442f commit 8d354f2

4 files changed

Lines changed: 18 additions & 12 deletions

File tree

app/utils/cache.server.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import fs from 'node:fs'
2-
import os from 'node:os'
32
import path from 'node:path'
4-
import { threadId } from 'node:worker_threads'
53
import { DatabaseSync } from 'node:sqlite'
64
import {
75
cachified as baseCachified,
@@ -22,19 +20,11 @@ import { getInstanceInfo, getInstanceInfoSync } from './litefs.server.ts'
2220
import { cachifiedTimingReporter, type Timings } from './timing.server.ts'
2321

2422
const CACHE_DATABASE_PATH = process.env.CACHE_DATABASE_PATH
25-
const IS_TEST = process.env.NODE_ENV === 'test' || process.env.CI === 'true'
26-
const TEST_WORKER_ID = process.env.VITEST_WORKER_ID ?? String(threadId)
27-
const CACHE_DATABASE_PATH_FOR_TESTS = IS_TEST
28-
? path.join(
29-
os.tmpdir(),
30-
`epic-stack-cache-${process.pid}-${TEST_WORKER_ID}.db`,
31-
)
32-
: CACHE_DATABASE_PATH
3323

3424
const cacheDb = remember('cacheDb', createDatabase)
3525

3626
function createDatabase(tryAgain = true): DatabaseSync {
37-
const databasePath = CACHE_DATABASE_PATH_FOR_TESTS
27+
const databasePath = CACHE_DATABASE_PATH
3828
if (!databasePath) {
3929
throw new Error('CACHE_DATABASE_PATH is not set')
4030
}
@@ -57,7 +47,7 @@ function createDatabase(tryAgain = true): DatabaseSync {
5747
`)
5848
} catch (error: unknown) {
5949
try {
60-
fs.unlinkSync(databasePath)
50+
fs.rmSync(databasePath, { force: true })
6151
} catch (unlinkError) {
6252
if (
6353
typeof unlinkError !== 'object' ||

tests/setup/global-setup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'node:path'
22
import { execaCommand } from 'execa'
33
import fsExtra from 'fs-extra'
44
import 'dotenv/config'
5+
import './test-env.ts'
56
import '#app/utils/env.server.ts'
67

78
export const BASE_DATABASE_PATH = path.join(

tests/setup/setup-test-env.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dotenv/config'
2+
import './test-env.ts'
23
import './db-setup.ts'
34
import '#app/utils/env.server.ts'
45
// we need these to be imported first 👆

tests/setup/test-env.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os from 'node:os'
2+
import path from 'node:path'
3+
import { threadId } from 'node:worker_threads'
4+
5+
const workerId =
6+
process.env.VITEST_WORKER_ID ??
7+
process.env.VITEST_POOL_ID ??
8+
String(threadId)
9+
10+
process.env.LITEFS_DIR ??= path.join(os.tmpdir(), 'epic-stack-litefs')
11+
process.env.CACHE_DATABASE_PATH ??= path.join(
12+
os.tmpdir(),
13+
`epic-stack-cache-${process.pid}-${workerId}.db`,
14+
)

0 commit comments

Comments
 (0)