Giter Site home page Giter Site logo

clo4 / deno_mock_fetch Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 11 KB

Use URLPattern syntax to mock fetch responses

Home Page: https://doc.deno.land/https/deno.land/x/mock_fetch/mod.ts

License: MIT License

TypeScript 100.00%
deno fetch-mock mock-fetch typescript

deno_mock_fetch's Introduction

Hi there ๐Ÿ‘‹

I'm a developer based in Sydney. I'm interested in basically anything obscure but cool, currently obsessing over Nix.

Previously I was the lead support engineer at Fig and a maintainer of withfig/autocomplete โ€” and a very frequent contributor to it too! I wrote possibly the fastest (?) command-line argument parser for JS/TS but abandoned it because, in general, it's a worse experience for the user overall when the CLI isn't written in a compiled language.

I also wrote the best* API wrapper for Jamf School, and a bunch of other libraries necessary for it including deno_mock_fetch and apple_device_identifiers.

(* = it has no competition, but I think it's great)

There's also a couple of old projects on PyPI that I won't link to, but the total downloads are nearly 80,000 ๐Ÿ˜ฌ

deno_mock_fetch's People

Contributors

clo4 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

eliassjogreen

deno_mock_fetch's Issues

Mock once feature

Hey, it would be very convient to have a utility function to mock the route and remove the route straight after it gets 1 call. Maybe even a mock_n one.

Update deps!

Hiya! Randomly stumbled upon this when I needed a mock-fetch module for work and this looks awesome! Thanks for the credit btw, was kinda shook when I read the README and all of the sudden my name and project pops up in the credits ๐Ÿ˜„. But yeah, like the title says it would be nice if you would update router to version 0.0.12.

New API

Some requirements for the new API

  • Needs to track how many times a route is fetched
    • An assert to ensure it's fetched exactly, at least, or at most, N times
  • Needs ability to un-mock a route after fetching it N times

It might be easier to vendor the dependency on router for this so there's no duplication! Can also use a different syntax, instead of GET@/... it can use GET /...

Can you simulate an `AbortController` timeout?

I'm trying to use this to test my retry fetch util, but it doesn't seem to be working. I can see the timeout is being called, but it doesn't seem to timeout the request. Do you know if this is possible to test?

Code

const {timeout = 10000} = options;

const controller = new AbortController();
const id = setTimeout(() => {
    controller.abort();
}, timeout);

res = await fetch(url, {
    ...options,
    signal: controller.signal
});
clearTimeout(id);

Test

let called = 0;

mf.mock("GET@/api/hello/:name", async (_req: Request, params: any) => {
    if (called++ === 0) {
        await sleep(2);
        return new Response();
    }

    return new Response(`Hello, ${params["name"]} ${called}!`, {
        status: 200,
    });
});

const res = await retryFetch("https://localhost:1234/api/hello/SeparateRecords", {timeout: 1000});
assertEquals(2, called);
assertEquals('Hello, SeparateRecords 2!', await res.text());

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.