Registry / testing / mock-gcs

mock-gcs

JSON →
library2.1.0jsnpmunverified

mock-gcs is a TypeScript library providing mock implementations of Google Cloud Storage SDK classes (Storage, Bucket, File) for testing and development. Version 2.1.0 supports most basic features like bucket and file operations, upload/download, signed URLs, and error simulation via mockErrorOnce. It also exports TypeScript interfaces (IStorage, IBucket, IFile) to facilitate type-safe code that works with both @google-cloud/storage and mock-gcs. The library is actively maintained and encourages community contributions for missing methods.

npm install mock-gcs
INSTALL
IMPORT
SIG · MOCK-GCS
M
mock-gcs
testingjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MockStorage
import { MockStorage } from 'mock-gcs'
const mockGcs = require('mock-gcs'); const MockStorage = mockGcs.MockStorage;
ESM-only; named export. CommonJS require can be used but is not tree-shakable.
IStorage
import { IStorage } from 'mock-gcs'
import { Storage } from '@google-cloud/storage'; // wrong, IStorage is the interface
Use IStorage for type-safe mocking instead of using the real Storage type.
Bucket
import { MockStorage } from 'mock-gcs'; const bucket = new MockStorage().bucket('name');
import { Bucket } from 'mock-gcs'; // Bucket is not exported directly
Bucket class is not exported; use storage.bucket() method.

Demonstrates how to use MockStorage in a class that accepts IStorage for type safety, then test with mock.

import { MockStorage } from 'mock-gcs'; import { Storage } from '@google-cloud/storage'; class FileProcessor { constructor(private storage: IStorage) {} async save(path: string, content: string) { const file = this.storage.bucket('my-bucket').file(path); await file.save(content); } async download(path: string): Promise<string> { const file = this.storage.bucket('my-bucket').file(path); const [data] = await file.download(); return data.toString(); } } // In test: const storage = new MockStorage(); const processor = new FileProcessor(storage); await processor.save('test.txt', 'hello'); const result = await processor.download('test.txt'); console.log(result); // 'hello'
Debug
Known issues
breakingVersion 2.x changed from CommonJS to ESM-only; require() may need dynamic import.
fix
Use import syntax or const { MockStorage } = await import('mock-gcs');
affects: >=2.0.0
gotchaNot all GCS methods are implemented; see README for list.
fix
Check available methods in documentation; submit PR for missing ones.
affects: >=0.0.0
gotchaPromise-based methods only; no callback support.
fix
Use async/await or Promise.then().
affects: >=0.0.0
gotchamockErrorOnce only works for methods that were previously called; subsequent calls are normal.
fix
Call mockErrorOnce before the method you want to fail.
affects: >=1.0.0
Errors
Common errors & fixes
Module not found: Can't resolve 'mock-gcs'
Package not installed or missing in package.json.
fix
npm install mock-gcs
Class constructor MockStorage cannot be invoked without 'new'
Importing as default or calling without new.
fix
Use: const storage = new MockStorage(); // not MockStorage() without new
Property 'mockErrorOnce' does not exist on type 'File'
mocked file object not created via MockStorage; using real @google-cloud/storage types.
fix
Use IFile type or cast to any: (file as any).mockErrorOnce('method', error)
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
@google-cloud/storageoptionalProvides the real GCS SDK interfaces being mocked; often used alongside mock-gcs in tests.
Agent activity
4 hits · last 30 days
node
4
Resources
mock-gcs — npm install mock-gcs · libregistry