New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mock-gcs

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-gcs

Mock implementation of the Google Cloud Storage SDK for Node.js

  • 1.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
decreased by-29.81%
Maintainers
1
Weekly downloads
 
Created
Source

mock-gcs

Mock implementation of the Google Cloud Storage SDK written in TypeScript. It is designed for testing and development purposes and allows you to test your code that interacts with Google Cloud Storage without making actual API calls or incurring charges.

The implementation provides mock objects for Storage, Bucket, and File classes in the @google-cloud/storage package. While the implementation is incomplete, most basic features are supported.

Available:

  • storage.bucket()
  • bucket.file()
  • bucket.upload()
  • bucket.getFiles() (promise-based only)
  • file.delete() (promise-based only)
  • file.exists() (promise-based only)
  • file.download() (promise-based only)
  • file.save() (promise-based only)
  • file.getSignedUrl() (promise-based only)
  • file.setMetadata() (promise-based only)
  • file.getMetadata() (promise-based only)
  • file.createWriteStream()
  • file.createReadStream()

If you need another method to be available, you can ask me by creating an issue or submitting your pull request.

Install

# using npm
npm install mock-gcs
# using yarn
yarn add mock-gcs

Usage

To use the mock storage in your tests, you can import the MockStorage class from the package.

// in ESM
import { MockStorage } from 'mock-gcs';
// in CommonJS
const { MockStorage } = require('mock-gcs');

Then, create an instance of it:

const storage = new MockStorage();

The storage object provides the same interface as the Storage class from the @google-cloud/storage package, including the bucket() method to create a mock bucket object.

const bucket = storage.bucket('my-bucket');

The bucket object provides the same interface as the Bucket class from the @google-cloud/storage package, including the file() method to create a mock file object.

const file = bucket.file('my-file.txt');

The file object provides the same interface as the File class from the @google-cloud/storage package, including methods for uploading, downloading, and deleting files.

await file.save('Hello, world!');

const [data] = await file.download();
console.log(data.toString()); // "Hello, world!"

const [exists] = await file.exists();
console.log(exists); // true

const [url] = await file.getSignedUrl();
console.log(url); // "https://storage.googleapis.com/my-bucket/my-file.txt?X-Goog-Algorithm=MOCKED"

await file.delete();

You also can simulate a custom error by using mockErrorOnce() method.

// Simulate an error during saving
const error = new Error('Failed to save');
file.mockErrorOnce('save', error);

try {
  await file.save('Will not be saved!'); // An error thrown
} catch (error) {
  console.error(error.message); // Failed to save
}
console.log('exists:', await file.exists()); // exists: [false]

await file.save('Will definitely be saved!'); // No error thrown
console.log('exists:', await file.exists()); // exists: [true]

We also provide IStorage, IBucket, and IFile interfaces that define a set of methods and properties that can be implemented by both @google-cloud/storage and mock-gcs with TypeScript.

import { IStorage, IBucket, IFile, MockStorage } from 'mock-gcs';
import { Storage } from '@google-cloud/storage';

class StorageConsumer {
  public bucket: IBucket;

  constructor(storage: IStorage) {
    this.bucket = storage.bucket('my-bucket');
  }

  public async isExists(path: string) {
    const file: IFile = this.bucket.file(path);
    const [exists] = await file.exists();
    return exists;
  }
}

const mockConsumer = new StorageConsumer(new MockStorage()); // use mocked version
const realConsumer = new StorageConsumer(new Storage()); // use real GCS SDK

API

Here are additional methods that only mock objects have:

bucket.put(name: string, contents?: string | Buffer, metadata?: Metadata): Promise<MockFile>

It used to create or overwrite a file in the bucket with the given name and contents. If a metadata object is also provided, it will be set as the metadata for the file.

The method returns a promise that resolves to the MockFile object that was created or updated.

const file = await bucket.put('file.txt', 'Hello, world!');
const [files] = await bucket.getFiles();

console.log(files.map(file => file.name)); // [ 'file.txt' ]

file.mockErrorOnce(method: string, error: Error): void

It allows the user to simulate an error when invoking a specific method on the MockFile instance, and it will only throw an error once for that method.

For example, if you want to simulate an error when calling the getMetadata() method once, you can call mockErrorOnce('getMetadata', new Error('some error message')) on your MockFile instance. The next time you call getMetadata() on that instance, it will throw the provided error object.

The method is useful when you want to test how your code behaves when an error occurs during a particular operation. It allows you to simulate these errors in a controlled manner, without having to create complex test setups with external dependencies.

It's worth noting that mockErrorOnce() only affects the next call to the specified method. If you want to simulate an error for multiple calls, you will need to call mockErrorOnce() again for each call. Also, if you want to simulate errors for multiple methods, you will need to call mockErrorOnce() separately for each method.

Supported methods: delete(), exists(), download(), save(), getSignedUrl(), setMetadata(), getMetadata().

Testing

This library is well tested. You can test the code as follows:

# using npm
npm test
# using yarn
yarn test

Contribute

If you have anything to contribute, or functionality that you lack - you are more than welcome to participate in this!

License

Feel free to use this library under the conditions of the MIT license.

Keywords

FAQs

Package last updated on 08 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc