Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Mocking Deno APIs with ease
License
hasundue/amber
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A library for mockingDeno APIs with ease.
Warning
Alpha version. Not tested extensively or documented well yet.
- Built upon and compatible with@std/testing
- Consistent interfaces across submodules
import*ascmdfrom"jsr:@chiezo/amber/cmd";
Replace Deno.Command as a side effect:
cmd.mock();assert(Deno.Command!==Original);
Stub any command by default:
cmd.mock();awaitnewDeno.Command("echo").output();assertNotRun("echo");
Replace Deno.Command inside the callback:
constecho=cmd.spy("echo");cmd.use(()=>newDeno.Command("echo"));assertSpyCalls(echo,1);
Create a spy for a command:
constecho=cmd.spy("echo");cmd.use(()=>newDeno.Command("echo"));assertSpyCalls(echo,1);
Create multiple spies for different commands separately:
constecho=cmd.spy("echo");constls=cmd.spy("ls");cmd.use(()=>{newDeno.Command("echo");assertSpyCalls(echo,1);assertSpyCalls(ls,0);newDeno.Command("ls");assertSpyCalls(echo,1);assertSpyCalls(ls,1);});
Stub a command with the default dummy:
constecho=cmd.stub("echo");awaitcmd.use(()=>newDeno.Command("echo").output());assertNotRun("echo");assertSpyCalls(echo,1);
Stub a command with a given fake:
cmd.stub("echo",classextendsDeno.Command{constructor(command:string|URL){super(command);thrownewError();}},);cmd.use(()=>assertThrows(()=>newDeno.Command("echo")));
Restore Deno.Command:
cmd.mock();cmd.restore();assert(Deno.Command===Original);
Not dispose spies created:
constecho=cmd.spy("echo");cmd.restore();cmd.use(()=>newDeno.Command("echo"));assertSpyCalls(echo,1);
Restore Deno.Command:
cmd.mock();cmd.dispose();assert(Deno.Command===Original);
Dispose spies created:
constecho=cmd.spy("echo");cmd.dispose();cmd.use(()=>newDeno.Command("echo"));assertSpyCalls(echo,0);
import*asfsfrom"jsr:@chiezo/amber/fs";
Replace file system functions as side effects:
fs.mock();assert(Deno.readTextFile!==original.readTextFile);assert(Deno.readTextFileSync!==original.readTextFileSync);
Stub the current working directory by default:
fs.mock();awaitfs.use(()=>Deno.writeTextFile("./test.txt","amber"));assertRejects(()=>Deno.readTextFile("./test.txt"));
Replace file system functions within the callback:
fs.use(()=>{assert(Deno.readTextFile!==original.readTextFile);assert(Deno.readTextFileSync!==original.readTextFileSync);});
Spy file system functions:
constspy=fs.spy(".");awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(spy.readTextFile,1);
Spy multiple paths separately:
constcwd=fs.spy(".");constsrc=fs.spy("./src");awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(cwd.readTextFile,1);assertSpyCalls(src.readTextFile,0);
Accept a URL:
constspy=fs.spy(newURL("..",import.meta.url));awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(spy.readTextFile,1);
Not write to the original path:
conststub=fs.stub("./test.txt");awaitfs.use(()=>Deno.writeTextFile("./test.txt","amber"));assertEquals((awaitDeno.permissions.query({name:"write",path:"./test.txt"})).state,"prompt",);assertSpyCalls(stub.writeTextFile,1);
Make the original file readable initially (readThrough):
conststub=fs.stub(".");awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(stub.readTextFile,1);
Make the updated content readable after being written:
fs.stub(".");awaitfs.use(async()=>{awaitDeno.writeTextFile("./README.md","amber");assertEquals(awaitDeno.readTextFile("./README.md"),"amber",);});
Throw on a file that has not been written if readThrough is disabled:
fs.stub(".",{readThrough:false});fs.use(()=>assertThrows(()=>Deno.readTextFileSync("./README.md")));
Stub multiple paths separately:
constcwd=fs.stub(".");constsrc=fs.stub("./src");awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(cwd.readTextFile,1);assertSpyCalls(src.readTextFile,0);
Restore file system functions:
fs.mock();fs.restore();assert(Deno.readTextFile===original.readTextFile);assert(Deno.readTextFileSync===original.readTextFileSync);
Not dispose spies created:
constspy=fs.spy(".");fs.restore();awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(spy.readTextFile,1);
Restore file system functions:
fs.mock();fs.dispose();assert(Deno.readTextFile===original.readTextFile);assert(Deno.readTextFileSync===original.readTextFileSync);
Dispose spies created:
constspy=fs.spy(".");fs.dispose();awaitfs.use(()=>Deno.readTextFile("./README.md"));assertSpyCalls(spy.readTextFile,0);
import{all}from"jsr:@chiezo/amber/util";
Mock multiple modules simultaneously:
constecho=cmd.stub("echo");constroot=fs.stub("../");all(cmd,fs).mock();newDeno.Command("echo");assertSpyCalls(echo,1);awaitDeno.readTextFile("../README.md");assertSpyCalls(root.readTextFile,1);
Use multiple modules simultaneously:
constecho=cmd.stub("echo");constroot=fs.stub("../");awaitall(cmd,fs).use(async()=>{newDeno.Command("echo");assertSpyCalls(echo,1);awaitDeno.writeTextFile("../test.txt","amber");assertSpyCalls(root.writeTextFile,1);assertEquals(awaitDeno.readTextFile("../test.txt"),"amber",);assertSpyCalls(root.readTextFile,1);});
Restore multiple modules simultaneously:
all(cmd,fs).mock();all(cmd,fs).restore();assert(Deno.Command===original.Command);assert(Deno.readTextFile===original.readTextFile);
Dispose multiple modules simultaneously:
constecho=cmd.spy("echo");constroot=fs.spy("../");all(cmd,fs).mock();all(cmd,fs).dispose();newDeno.Command("echo");assertSpyCalls(echo,0);awaitDeno.readTextFile("../README.md");assertSpyCalls(root.readTextFile,0);
About
Mocking Deno APIs with ease
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.