-
-
Save nfarina/90ba99a5187113900c86289e67586aaa to your computer and use it in GitHub Desktop.
| // | |
| // Quick & Dirty Google Cloud Storage emulator for tests. Requires | |
| // `stream-buffers` from npm. Use it like this: | |
| // | |
| // `new MockStorage().bucket('my-bucket').file('my_file').createWriteStream()` | |
| // | |
| class MockStorage { | |
| buckets: {[name: string]: MockBucket}; | |
| constructor() { | |
| this.buckets = {}; | |
| } | |
| bucket(name: string) { | |
| return this.buckets[name] || | |
| (this.buckets[name] = new MockBucket(name)); | |
| } | |
| } | |
| class MockBucket { | |
| name: string; | |
| files: {[path: string]: MockFile}; | |
| constructor(name: string) { | |
| this.name = name; | |
| this.files = {}; | |
| } | |
| file(path: string) { | |
| return this.files[path] || | |
| (this.files[path] = new MockFile(path)); | |
| } | |
| } | |
| class MockFile { | |
| path: string; | |
| contents: Buffer; | |
| metadata: Object; | |
| constructor(path: string) { | |
| this.path = path; | |
| this.contents = new Buffer(0); | |
| this.metadata = {}; | |
| } | |
| get() { | |
| return [this, this.metadata]; | |
| } | |
| setMetadata(metadata: Object) { | |
| const customMetadata = {...this.metadata.metadata, ...metadata.metadata}; | |
| this.metadata = {...this.metadata, ...metadata, metadata: customMetadata}; | |
| } | |
| createReadStream() { | |
| const streamBuffers = require('stream-buffers'); | |
| const readable = new streamBuffers.ReadableStreamBuffer(); | |
| readable.put(this.contents); | |
| readable.stop(); | |
| return readable; | |
| } | |
| createWriteStream({metadata}: Object) { | |
| this.setMetadata(metadata); | |
| const streamBuffers = require('stream-buffers'); | |
| const writable = new streamBuffers.WritableStreamBuffer(); | |
| writable.on('finish', () => { | |
| this.contents = writable.getContents(); | |
| }); | |
| return writable; | |
| } | |
| delete() { | |
| return Promise.resolve(); | |
| } | |
| } |
Hi, how do you actually inject it into
jestmocking? Thanks!
I personally run all my Firebase calls through a single getFirebaseApp() sort of function. When running under test mode, it returns a mock App that stubs out the getters for mock Storage, Firestore, etc.
Should be able to use https://jestjs.io/docs/mock-functions#mocking-modules, or https://www.npmjs.com/package/proxyquire. Both should be able to mock out the actual call to '@google-cloud/storage'
I am making some progress with this approach:
function mockedStorage() {
return class MockStorage {
public buckets: MockBucketList;
public constructor() {
this.buckets = {};
}
public bucket(name: string): MockBucket {
if (this.buckets[name] === undefined) {
this.buckets[name] = new MockBucket(name);
}
return this.buckets[name];
}
};
}
jest.mock('@google-cloud/storage', () => ({
Storage: mockedStorage()
}));Hi everyone! Just wanted to share that I recently published a small package to help mock @google-cloud/storage written in TypeScript. Check out mock-gcs: https://github.com/aldipermanaetikaputra/mock-gcs. Hope it helps!
@aldipermanaetikaputra I have a Datastore one if you'd like that as well. I couldn't find this packages repo so I couldn't file issues or contribute back. https://www.npmjs.com/package/datastore-mock?activeTab=readme So I forked it to a script. Would be nice if it could get maintained and updated.
I have a rather-insane Firestore one as well :) actually works...but it only implements the "compat" API
Hi, how do you actually inject it into
jestmocking? Thanks!