npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bun-bagel

v1.1.0

Published

🥯 The Bun fetch mocker with a hole lot of flavor.

Downloads

4,030

Readme

🥯 bun-bagel: The Bun fetch mocker with a hole lot of flavor.

npm version GitHub license 🧪 Tests


bun-bagel is a mocking library specifically designed for Bun's fetch API. It enables developers to easily intercept fetch requests and provide custom mock responses, streamlining the development and testing process of Bun applications.

:warning: The library is yet only experimental and might change over time.

📖 Usage

import { mock } from "bun-bagel";

// Register the mock for the example URL.
mock("https://example.com/api/users/*", { data: { name: "Foo" } });

// Make a fetch request to the mocked URL
const response = await fetch("https://example.com/api/users/123");

// Print the response body
console.log(await response.json());

Output:

{ name: "Foo" }

🚀 Why bun-bagel?

  • Lightweight and easy to use: Get started in minutes with a simple, intuitive API.
  • Flexible and powerful: Handle a wide range of mocking scenarios with ease.
  • Built for Bun: Seamlessly integrates with Bun's runtime for a smooth developer experience.
  • Thoroughly tested: Comes with a comprehensive test suite to ensure reliability.

📚 Installation

bun install bun-bagel

🧪 Examples

Bun Unit Tests

import { describe, test, expect, afterEach } from "bun:test";
import { mock, clearMocks } from "bun-bagel";

describe("Unit Test", () => {

    afterEach(() => {
        clearMocks();
    });

    test("Mock Fetch", async () => {
        // Register the mock for the example URL.
        mock("https://example.com/api/users/*", { data: { name: "Foo" } });

        // Call a function that uses the fetch method.
        const response = await fetchSomeData();

        // Print the response body
        console.log(await response.json()); // => { name: "Foo" }
    });
});

Mock by headers and method

import { mock } from "bun-bagel";
import type { MockOptions } from "bun-bagel";

const options: MockOptions = {
    method: "POST",
    headers: { "x-foo-bar": "baz" },
    response: {
        data: { name: "Foo" },
    }
};

// Register the mock for the example URL.
mock("https://example.com/api/users/*", options);

// Make a fetch request to the mocked URL
const response = await fetch("https://example.com/api/users/123", { headers: { "x-foo-bar": "baz" } });

// Requests without the headers will not be matched.
const response2 = await fetch("https://example.com/api/users/123");

// Check the response body.
console.log(await response.json()); // => { name: "Foo" }

Mock response status and headers

import { mock } from "bun-bagel";
import type { MockOptions } from "bun-bagel";

const options: MockOptions = {
    response: {
        data: { name: "Foo" },
        status: 404,
        headers: { "x-foo-bar": "baz" },
    }
};

// Register the mock for the example URL.
mock("https://example.com/api/users/*", options);

// Make a fetch request to the mocked URL
const response = await fetch("https://example.com/api/users/123");

// Check the status and headers.
console.log(response.status); // => 404
console.log(response.headers); // => { "x-foo-bar": "baz" }

🤝 Contributing

Contributions are welcome! Please see the CONTRIBUTING.md file for details.

🔨 Development

To contribute to bun-bagel, follow these steps:

  1. Clone the repository: git clone https://github.com/DRFR0ST/bun-bagel.git
  2. Install dependencies: bun install
  3. Run tests: bun test
  4. Build the library: bun run build

[!NOTE] You can also play around with bun-bagel by making changes in the /sandbox directory and running bun run sandbox. Make sure to build the library after making changes in the /src directory.

🤝 Community

Join the discussion on the GitHub Discussions page.

📝 License

This project is licensed under the terms of the MIT license. See the LICENSE file for details.

📢 Thanks to all contributors for making this library better!

🤖 Thanks to Gemini for generating a part of the initial code and readme, and helped brainstorm the idea.