Git Product home page Git Product logo

msw-trpc-example's Introduction

Example for mocking a tRPC API endpoint with msw-trpc

This repo contains a Next.js app using tRPC, and a unit test for a component that uses Mock Service Worker with msw-trpc to intercept network requests.

It is reduced to the bare minimum to focus only on the msw testing setup, and should not be used as a reference on how to build an entire app.

The files you'll probably want to look at are:

  • vitest.config.ts is a minimal Vitest config
  • src/__test__/setup.tsx creates our mocked tRPC client and a function to render components with the required providers
  • src/components/greeting.tsx exports the Greeting component that we are testing
  • src/components/greeting.test.tsx tests our Greeting component

To get started:

pnpm i
pnpm test

resources:

TODO:

  • some imports are not using ~ because vitest couldn't traverse the import alias for some reason. fix this.
  • some sort of setup (for example dependabot, github actions, etc) to update dependencies and check in CI that everything still works.

PRs welcome. Open an issue first if you're not sure, or contact me on twitter/discord/etc.

msw-trpc-example's People

Contributors

c-ehrlich avatar

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.