Skip to Content
Engineering11 Documentation 🔥

SFTP Testing API

Mock SFTP and FTP clients for Jest testing without real servers.

What It Does

The SFTP Testing API provides in-memory mock implementations of ssh2-sftp-client and basic-ftp libraries for testing SFTP/FTP functionality without requiring actual server connections. It enables fast, reliable, deterministic tests with a simple one-line setup.

Key Capabilities

  • Dual Protocol Mocking: Both SFTP and FTP support
  • In-Memory File System: Fast, deterministic tests
  • Jest Integration: Automatic mocking with fock() function
  • Stream Support: Bidirectional stream handling
  • File Operations: Upload, download, list, delete
  • Directory Navigation: Full hierarchical directory support
  • Test Isolation: Easy file system reset between tests

Main Component

fock() Function

const client = fock() client.add('/test/file.txt', Buffer.from('content')) client.clearFiles()

Sets up mocks for both ssh2-sftp-client and basic-ftp.

MockClient (SFTP)

Mock implementation of ssh2-sftp-client:

  • connect(), end(): Connection management
  • list(path): Directory listing
  • get(path): File download
  • put(input, path): File upload
  • delete(path): File deletion
  • cwd(): Current working directory

MockFtpClient (FTP)

Mock implementation of basic-ftp:

  • connect(), access(), login(): Connection
  • pwd(): Print working directory
  • list(path): Directory listing
  • size(path): File size
  • uploadFrom(source, dest): Upload
  • download(dest, source): Download
  • remove(path): Delete

InternalFileSystem

Core in-memory file system:

  • Hierarchical directory structure
  • Buffer and file path storage
  • Stream conversion
  • Path navigation (absolute, relative, ., ..)

Usage Example

import {fock} from '@engineering11/sftp-testing' describe('SFTP Operations', () => { const client = fock() beforeEach(() => { client.add('/data/file1.txt', Buffer.from('test')) client.add('/data/file2.csv', './test-data/sample.csv') }) afterEach(() => { client.clearFiles() }) it('should list files', async () => { const sftpClient = new SFTPClient() await sftpClient.connect({...}) const files = await sftpClient.list('/data') expect(files).toHaveLength(2) }) })

Common Use Cases

  • Testing SFTP upload/download logic
  • Testing FTP client integrations
  • Testing file processing pipelines
  • Unit testing without external dependencies
  • Integration testing for file transfer

What Customers Don’t Have to Build

  • Mock SFTP server
  • Mock FTP server
  • In-memory file system
  • Stream handling mocks
  • Jest mocking configuration
  • File metadata generation
  • Directory navigation logic
  • Test isolation infrastructure
Last updated on