Mocking a JavaScript class with Jest: mocking vs. dependency injection

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • Playwright

    Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

  • Checkout https://playwright.dev

  • testing-library-docs

    docs site for @testing-library/*

  • Hmm, I'm not sure I follow the part of static imports, but I tend to only mock code that perform side effects (usually, HTTP requests), and I explicitly isolate those to separate modules that I can mock. Other than that, they're more integration tests using testing-library [1] to simulate user interactions and verifying that they have the expected effects.

    When there's complex business logic, I'll separate that out into a separate pure function, and I write tests for that function without needing mocks.

    [1] https://testing-library.com/

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
  • abstract-blob-store

    A test suite and interface you can use to implement streaming file (blob) storage modules for various storage backends and platforms

  • One approach I’ve found useful is publishing a test suite as a module for use in these kind of “fakes”. For example, a file system blob store that passes the same test suite as an S3 blob store can be used as a reliable fake.

    https://github.com/maxogden/abstract-blob-store

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts