Documentation / zerotal / storage / FakeDisk
Class: FakeDisk
Defined in: packages/core/src/storage/FakeDisk.ts:32
An in-memory storage disk for tests.
Writing real files to test an upload leaves the suite dependent on a
filesystem it has to clean up — and a test that forgets to passes the second
time for the wrong reason. This keeps everything in a Map, so each test
starts empty by construction, and adds the assertions that a plain driver has
no reason to carry.
Install it with Storage.fake(), which swaps it in for a named disk and
returns it.
Example
const disk = Storage.fake();
await app.multipart('/avatar', { avatar: fakeFile.image('me.png') });
disk.assertExists('avatars/me.png');
disk.assertCount(1);
Implements
Constructors
Constructor
new FakeDisk(
baseUrl?):FakeDisk
Defined in: packages/core/src/storage/FakeDisk.ts:39
Parameters
baseUrl?
string = "/storage"
Prefix returned by url, so a test can assert on the URL a controller hands back.
Returns
FakeDisk
Accessors
count
Get Signature
get count():
number
Defined in: packages/core/src/storage/FakeDisk.ts:133
Number of files currently stored.
Returns
number
Methods
put()
put(
path,content,options?):Promise<void>
Defined in: packages/core/src/storage/FakeDisk.ts:43
Parameters
path
string
content
string | Blob | Uint8Array<ArrayBufferLike>
options?
PutOptions = {}
Returns
Promise<void>
Implementation of
append()
append(
path,content):Promise<void>
Defined in: packages/core/src/storage/FakeDisk.ts:57
Append to the end of a file, creating it when absent.
Only a filesystem can do this. An object store rewrites the whole object on every write, so emulating an append there would quietly turn one line of a log into a full download-and-upload of the day's file — S3Driver throws UnsupportedOperationError rather than pretend. Use it for things that are genuinely local and line-oriented, like the log trail.
Parameters
path
string
content
string | Uint8Array<ArrayBufferLike>
Returns
Promise<void>
Implementation of
get()
get(
path):Promise<string|null>
Defined in: packages/core/src/storage/FakeDisk.ts:66
Parameters
path
string
Returns
Promise<string | null>
Implementation of
stream()
stream(
path):Promise<Blob|null>
Defined in: packages/core/src/storage/FakeDisk.ts:71
A lazy handle for path, or null when absent.
Optional. When a backend can hand back something streamable, serving a file does not have to read it into memory first — which is the difference between serving a 2 GB video and falling over. StorageFilesMiddleware uses it when present and falls back to getBuffer when not.
Parameters
path
string
Returns
Promise<Blob | null>
Implementation of
getBuffer()
getBuffer(
path):Promise<Uint8Array<ArrayBufferLike> |null>
Defined in: packages/core/src/storage/FakeDisk.ts:76
Parameters
path
string
Returns
Promise<Uint8Array<ArrayBufferLike> | null>
Implementation of
exists()
exists(
path):Promise<boolean>
Defined in: packages/core/src/storage/FakeDisk.ts:80
Parameters
path
string
Returns
Promise<boolean>
Implementation of
delete()
delete(
path):Promise<void>
Defined in: packages/core/src/storage/FakeDisk.ts:84
Parameters
path
string
Returns
Promise<void>
Implementation of
url()
url(
path):string
Defined in: packages/core/src/storage/FakeDisk.ts:88
Return a public URL for the given path.
Parameters
path
string
Returns
string
Implementation of
copy()
copy(
source,destination):Promise<void>
Defined in: packages/core/src/storage/FakeDisk.ts:92
Copy a file within the same disk.
Parameters
source
string
destination
string
Returns
Promise<void>
Implementation of
move()
move(
source,destination):Promise<void>
Defined in: packages/core/src/storage/FakeDisk.ts:102
Move (rename) a file within the same disk.
Parameters
source
string
destination
string
Returns
Promise<void>
Implementation of
size()
size(
path):Promise<number|null>
Defined in: packages/core/src/storage/FakeDisk.ts:107
Return the file size in bytes, or null if not found.
Parameters
path
string
Returns
Promise<number | null>
Implementation of
lastModified()
lastModified(
path):Promise<number|null>
Defined in: packages/core/src/storage/FakeDisk.ts:111
Return the last-modified timestamp (ms since epoch), or null if not found.
Parameters
path
string
Returns
Promise<number | null>
Implementation of
temporaryUrl()
temporaryUrl(
path,expiresInSeconds):Promise<string>
Defined in: packages/core/src/storage/FakeDisk.ts:115
Return a temporary URL valid for expiresInSeconds seconds.
For local driver this is a signed token URL; for S3 it is a presigned URL.
Parameters
path
string
expiresInSeconds
number
Returns
Promise<string>
Implementation of
paths()
paths():
string[]
Defined in: packages/core/src/storage/FakeDisk.ts:123
Every stored path, in insertion order.
Returns
string[]
file()
file(
path):FakeStoredFile|undefined
Defined in: packages/core/src/storage/FakeDisk.ts:128
The full record for a stored file, or undefined.
Parameters
path
string
Returns
FakeStoredFile | undefined
clear()
clear():
void
Defined in: packages/core/src/storage/FakeDisk.ts:138
Forget everything stored so far.
Returns
void
assertExists()
assertExists(
path,contents?):this
Defined in: packages/core/src/storage/FakeDisk.ts:145
Assert a file exists at path, optionally matching its exact contents.
Parameters
path
string
contents?
string | Uint8Array<ArrayBufferLike>
Returns
this
assertMissing()
assertMissing(
path):this
Defined in: packages/core/src/storage/FakeDisk.ts:167
Assert nothing is stored at path.
Parameters
path
string
Returns
this
assertExistsMatching()
assertExistsMatching(
pattern):this
Defined in: packages/core/src/storage/FakeDisk.ts:181
Assert a file exists whose path matches pattern — the way to assert on an
upload stored under a generated name.
Parameters
pattern
RegExp
Returns
this
Example
disk.assertExistsMatching(/^avatars/[0-9a-f-]+\.png$/);
assertContentType()
assertContentType(
path,contentType):this
Defined in: packages/core/src/storage/FakeDisk.ts:191
Assert the file at path was stored with the given content type.
Parameters
path
string
contentType
string
Returns
this
assertCount()
assertCount(
expected):this
Defined in: packages/core/src/storage/FakeDisk.ts:204
Assert exactly expected files are stored.
Parameters
expected
number
Returns
this
assertNothingStored()
assertNothingStored():
this
Defined in: packages/core/src/storage/FakeDisk.ts:214
Assert nothing at all was stored.
Returns
this