fs.test.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132
  1. import { CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, CodamaError } from '@codama/errors';
  2. import { expect, test } from 'vitest';
  3. import { createDirectory, deleteDirectory, readJson, writeFile } from '../src';
  4. if (__NODEJS__) {
  5. test('it reads JSON objects from files', () => {
  6. const result = readJson('./test/fs.test.json');
  7. expect(result).toEqual({ key: 'value' });
  8. });
  9. } else {
  10. test('it fails to call readJson', () => {
  11. expect(() => readJson('./path')).toThrow(
  12. new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'readFileSync' }),
  13. );
  14. });
  15. test('it fails to call createDirectory', () => {
  16. expect(() => createDirectory('./path')).toThrow(
  17. new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'mkdirSync' }),
  18. );
  19. });
  20. test('it fails to call deleteDirectory', () => {
  21. expect(() => deleteDirectory('./path')).toThrow(
  22. new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'rmSync' }),
  23. );
  24. });
  25. test('it fails to call writeFile', () => {
  26. expect(() => writeFile('./path', 'content')).toThrow(
  27. new CodamaError(CODAMA_ERROR__NODE_FILESYSTEM_FUNCTION_UNAVAILABLE, { fsFunction: 'writeFileSync' }),
  28. );
  29. });
  30. }