diff --git a/test/isolateStorageDir.ts b/test/isolateStorageDir.ts new file mode 100644 index 0000000000..a7cbafbab5 --- /dev/null +++ b/test/isolateStorageDir.ts @@ -0,0 +1,16 @@ +import { mkdtemp, rm } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; + +import { afterAll } from 'vitest'; + +// Vitest runs test files in parallel worker processes, but every Configuration +// defaults to the same cwd-relative `./storage` dir — one file's purge-on-init +// races against another file's reads/writes (ENOENT from the fs backend), so +// this setup file (wired in vitest.config.mts) points each file at its own dir. +const storageDir = await mkdtemp(join(tmpdir(), 'apify-sdk-test-storage-')); +process.env.CRAWLEE_STORAGE_DIR = storageDir; + +afterAll(async () => { + await rm(storageDir, { recursive: true, force: true }); +}); diff --git a/vitest.config.mts b/vitest.config.mts index ef0a94ea85..401e05f82a 100644 --- a/vitest.config.mts +++ b/vitest.config.mts @@ -19,6 +19,9 @@ export default defineConfig({ }, clearMocks: true, restoreMocks: true, + // Give each test file its own storage dir - parallel workers must not + // share (and purge) the same on-disk `./storage`. + setupFiles: ['./test/isolateStorageDir.ts'], testTimeout: 60_000, hookTimeout: 60_000, alias: [