Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

1.0 KB
Raw Blame
import { existsSync } from 'node:fs';
import { writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { describe, expect, test } from 'vitest';
import { withTempDir } from './temp-dir.test-helper.ts';

describe('withTempDir', () => {
  test('provides a usable dir, returns fn result, and removes the dir after', async () => {
    let captured = '';
    const result = await withTempDir('temp-dir-test-', async (dir) => {
      captured = dir;
      expect(existsSync(dir)).toBe(true);
      await writeFile(join(dir, 'f.txt'), 'x');
      return 42;
    });
    expect(result).toBe(42);
    expect(captured).not.toBe('');
    expect(existsSync(captured)).toBe(false);
  });

  test('removes the directory even when fn throws', async () => {
    let captured = '';
    await expect(
      withTempDir('temp-dir-test-', (dir) => {
        captured = dir;
        throw new Error('boom');
      }),
    ).rejects.toThrow('boom');
    expect(captured).not.toBe('');
    expect(existsSync(captured)).toBe(false);
  });
});