From 557d20ff9837a1095cf09399c27031eb9a7ddefb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Fri, 7 Aug 2026 13:55:32 +0200 Subject: [PATCH] test: isolate on-disk storage dirs of parallel test files Vitest runs test files in parallel worker processes, but every Configuration defaulted to the same cwd-relative ./storage dir, so one file's purge-on-init raced against another file's reads/writes and intermittently failed random tests with "I/O error: No such file or directory (os error 2)" from the fs storage backend. A vitest setup file now points CRAWLEE_STORAGE_DIR at a per-test-file temp dir (removed again after the file finishes). --- test/isolateStorageDir.ts | 16 ++++++++++++++++ vitest.config.mts | 3 +++ 2 files changed, 19 insertions(+) create mode 100644 test/isolateStorageDir.ts 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: [