Nock VS pact-js

Compare Nock vs pact-js and see what are their differences.

Nock

HTTP server mocking and expectations library for Node.js (by nock)

pact-js

JS version of Pact. Pact is a contract testing framework for HTTP APIs and non-HTTP asynchronous messaging systems. (by pact-foundation)
Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
Nock pact-js
21 9
12,519 1,543
0.4% 1.2%
8.3 8.7
7 days ago 21 days ago
JavaScript TypeScript
MIT License GNU General Public License v3.0 or later
The number of mentions indicates the total number of mentions that we've tracked plus the number of user suggested alternatives.
Stars - the number of stars that a project has on GitHub. Growth - month over month growth in stars.
Activity is a relative number indicating how actively a project is being developed. Recent commits have higher weight than older ones.
For example, an activity of 9.0 indicates that a project is amongst the top 10% of the most actively developed projects that we are tracking.

Nock

Posts with mentions or reviews of Nock. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2023-03-13.
  • Contract Testing?
    1 project | /r/softwaretesting | 1 May 2023
    So, why would you want a REAL server to mock request/reponses? You have a lot of intercepts today that sit on the network layer and you can define things like "If you send request to that endpoint, with that json, please return that Status" (for NodeJS example, Nock - https://github.com/nock/nock)
  • I made wirepig, a simple way to mock HTTP and TCP dependencies in tests.
    3 projects | /r/node | 13 Mar 2023
    That said, folks seem to like "recording" features in these sorts of tools (Ruby's VCR, nock, etc), so maybe there's a future where I add something similar. I've always just found the ergonomics of those features awkward to deal with, especially having to flip back and forth between tests and fixtures files to figure out what's wired to what, but maybe there's a clean solution... perhaps a "live request" mode that just prints mock code snippets of request/response pairs passing through your app.
  • Is there a better way to mock an axios call?
    2 projects | /r/reactjs | 28 Jan 2023
    While not mocking per say I usually use nock for http calls. You can use nock.recorder.rec() to capture the http call to play back during test, That way you are always using "live" code but not making real calls to servers.
  • How do you practice with React without setting up your own backend?
    6 projects | /r/reactjs | 3 Dec 2022
  • OSD600 - Telescope - Testing for feed URLs
    4 projects | dev.to | 20 Nov 2022
    I looked at the service which is used to get the feed URLs from a blog URL and noticed it takes the html response of the blog URL and gets the links ( tags) by checking the type attribute value against a list of valid feed values. So, I decided to use a similar approach by getting the html response for a provided URL and checking the Content-Type header against a list of valid MIME types for a feed. I ended up updating the logic to test if a URL is a feed URL, returning it if true. If the URL is found to not be a feed URL, it would try to get the feed URLs assuming the URL is a blog URL. I tested and confirmed that the new logic worked for both blog and feed URLs. Then, I added some tests for the new function I added to test for a feed URL. Testing this ended up being simpler than I expected as all I had to do was mock the response of a test url (using nock), and then check if the function returned the correct boolean value for a url. I created a PR and noticed that some of the tests in another file were now failing. While I was investigating this, I got a review on my PR, requesting me to add another test to the file which had the failing tests. That file tested the API service as a whole. I found out that nock only mocks a URL's response for one request by default. And since I was now checking for a feed URL as well, the function which returned the feed URLs from a blog URL was throwing an error since the nock for that was used up. To fix this, I had to specify in the nock statement to mock the URL response for two requests:
  • What features would you consider missing/nice to haves for backend web development in Rust?
    6 projects | /r/rust | 4 Nov 2022
  • Axios shipped a buggy version and it broke many productions apps. Let this be a lesson to pin your dependencies!
    5 projects | /r/node | 7 Oct 2022
    There are libraries like https://github.com/nock/nock to prevent mocking the whole axios.
  • How to test an endpoint that depends on external API?
    1 project | /r/node | 3 Oct 2022
    Use nock: https://github.com/nock/nock
  • How to mock a useQuery in jest?
    1 project | /r/learnreactjs | 22 Sep 2022
    Going based off the documentation I sent you in my last reply, there is an example that uses nock to emulate api responses. I haven't used nock myself, but the example seems pretty simple to use. You just need to take the example and change the response object to be the shape of what your getStuffFromDatabase function returns. That way your useCategory function runs as close to normally as possible, while providing a mock response value instead of hitting the database.
  • Is it acceptable to use mock servers, like Postman, for testing in Android?
    3 projects | /r/androiddev | 6 Sep 2022
    If you’re willing to venture into nodejs territory, then nock is a fantastic and simple to set up http mock server. https://github.com/nock/nock

pact-js

Posts with mentions or reviews of pact-js. We have used some of these posts to build our list of alternatives and similar projects. The last one was on 2023-12-22.
  • Enhancing Backend Microservices Ecosystem with Contract Testing [Spartans Summit 2024]
    1 project | dev.to | 16 Apr 2024
    First, he shows the official pact.io websites. Then, he clicks on the “View on Github” button by selecting Node JS and Javascript from the list of options.
  • Parsing AWS AppSync Responses, Elm GraphQL Libraries, and Only Doing Front-End
    3 projects | dev.to | 22 Dec 2023
    It just just enough abstraction over the basics of converting your HTTP calls to GraphQL queries and mutations, but ALL of the parsing of responses is on you. I’m well versed in parsing JSON in Elm. I’m also familiar with the compiler errors as well as runtime errors you get with JSON that doesn’t match up to what you designed. At some point I’ll probably have to move beyond the unit tests and add contact tests, maybe via Pact.js.
  • The Big TDD Misunderstanding
    1 project | news.ycombinator.com | 19 Nov 2023
    > I also wasn't aware that "unit" referred to an isolated test, not to the SUT.

    I'm with you. That claim is unsubstantiated. It seems to trace to the belief that the first unit tests were XUnit family, thus were SUnit for Scheme. But Kent Beck made it pretty clear that SUnit "units" were classes.

    https://web.archive.org/web/20150315073817/http://www.xprogr...

    There were unit tests before that. SUnit took its name from common parlance, not vice versa. It was a strange naming convention, given that the unit testing framework could be used to test anything and not just units. Much like the slightly older Test Anything Protocol (TAP) could.

    > [on unit tests] This does lead to a lot of work maintaining them whenever the implementation changes, but this is a necessary chore because of the value they provide.

    I disagree. Unit tests can still be behavioral. Then they change whenever the behavior changes. They should still work with a mere implementation change.

    > This is why I still think that the traditional test pyramid is the best model to follow.

    I'll disagree a little with that, too. I think a newer test pyramid that uses contract testing to verify integrations is better. The notion of contract tests is much newer than the pyramids and, properly applied, can speed up feedback by orders of magnitude while also cutting debugging time and maintenance by orders of magnitude.

    On that front, I love what Pact is doing and would like to see more competition in the area. Hottest thing in testing since Cypress/Playwright . . .

    https://pact.io

  • Ask HN: How do you test your microservices?
    1 project | news.ycombinator.com | 21 Jan 2023
    I've worked in places where Pact [0] was used for testing services developed by different teams (external) and teams themselves (internal)

    [0] https://pact.io/

  • A response to James Shore's Nullable pattern
    1 project | news.ycombinator.com | 16 Jan 2023
    I'd never heard anyone call those "integrity tests" before. I think "contract test" is more common.

    Assuming I understood you, that is.

    I've been telling everyone to look at Pact to make contract testing easier to organize and maintain and to make it easier to trigger in the other tests in CI when an interface's behavior changes. They haven't offered me a commission yet. ;-)

    https://pact.io

  • Gestionarea DTO-urilor intr-o arhitectura de tip Microservicii cu Event-Driven
    1 project | /r/programare | 15 Nov 2022
  • Can someone recommend technologies for testing automation for API application?
    2 projects | /r/softwaredevelopment | 11 Oct 2022
    We use pact and since introducing it we have significantly increased velocity and reduced test cycles as it catches things very early. For system tests we hand write them using whatever test frameworks the team is used to.
  • Advanced TypeScript Patterns: API Contracts
    3 projects | /r/javascript | 22 Aug 2022
    There is also Pact https://pact.io/ for a language agnostic pact testing.
  • Framework for end to end testing of microservices
    5 projects | /r/softwaretesting | 3 Jul 2022
    When you wish to focus on the contract ( which kind of field is required, ...), you shoud use contract testing frameworks. As you seem to leverage a microservices, a consumer driven contract testing approach with a framework like Pact.js is recommended.

What are some alternatives?

When comparing Nock and pact-js you can also consider the following projects:

msw - Seamless REST/GraphQL API mocking library for browser and Node.js.

Karate - Test Automation Made Simple

http-proxy - A full-featured http proxy for node.js

rust-wildbow-scraper - Automatically scrapes wildbow's web serials and compiles them into ebooks

node-fetch - A light-weight module that brings the Fetch API to Node.js

zod - TypeScript-first schema validation with static type inference

axios - Promise based HTTP client for the browser and node.js

Robot Framework - Generic automation framework for acceptance testing and RPA

superagent - Ajax for Node.js and browsers (JS HTTP client). Maintained for @forwardemail, @ladjs, @spamscanner, @breejs, @cabinjs, and @lassjs.

mockoon - Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.

miragejs - A client-side server to build, test and share your JavaScript app

io-ts - Runtime type system for IO decoding/encoding