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

@codeceptjs/pactum-mock-server

v1.0.0

Published

Mock Server helper for CodeceptJS - powered by pactum

Downloads

4

Readme

MockServer

Table of Contents

config

Configuration

This helper should be configured in codecept.conf.(js|ts)

Type: object

Properties

  • port number? Mock server port
  • host string? Mock server host
  • httpsOpts object? key & cert values are the paths to .key and .crt files

MockServer

MockServer powered by pactum

The MockServer Helper in CodeceptJS empowers you to mock any server or service via HTTP or HTTPS, making it an excellent tool for simulating REST endpoints and other HTTP-based APIs.

Examples

You can seamlessly integrate MockServer with other helpers like REST or Playwright. Here's a configuration example inside the codecept.conf.js file:

{
  helpers: {
    REST: {...},
    MockServer: {
      require: '@codeceptjs/pactum-mock-server',
      // default mock server config
      port: 9393,
      host: '0.0.0.0',
      httpsOpts: {
        key: '',
        cert: '',
      },
    },
  }
}
Adding Interactions

Interactions add behavior to the mock server. Use the I.addInteractionToMockServer() method to include interactions. It takes an interaction object as an argument, containing request and response details.

I.addInteractionToMockServer({
   request: {
     method: 'GET',
     path: '/api/hello'
   },
   response: {
     status: 200,
     body: {
       'say': 'hello to mock server'
     }
   }
});
Request Matching

When a real request is sent to the mock server, it matches the received request with the interactions. If a match is found, it returns the specified response; otherwise, a 404 status code is returned.

  • Strong match on HTTP Method, Path, Query Params & JSON body.
  • Loose match on Headers.
Strong Match on Query Params

You can send different responses based on query parameters:

I.addInteractionToMockServer({
  request: {
    method: 'GET',
    path: '/api/users',
    queryParams: {
      id: 1
    }
  },
  response: {
    status: 200,
    body: 'user 1'
  }
});

I.addInteractionToMockServer({
  request: {
    method: 'GET',
    path: '/api/users',
    queryParams: {
      id: 2
    }
  },
  response: {
    status: 200,
    body: 'user 2'
  }
});
  • GET to /api/users?id=1 will return 'user 1'.
  • GET to /api/users?id=2 will return 'user 2'.
  • For all other requests, it returns a 404 status code.
Loose Match on Body

When strict is set to false, it performs a loose match on query params and response body:

I.addInteractionToMockServer({
  strict: false,
  request: {
    method: 'POST',
    path: '/api/users',
    body: {
      name: 'john'
    }
  },
  response: {
    status: 200
  }
});
  • POST to /api/users with the body containing name as 'john' will return a 200 status code.
  • POST to /api/users without the name property in the body will return a 404 status code.

Happy testing with MockServer in CodeceptJS! 🚀

Methods

Parameters

  • passedConfig

startMockServer

Start the mock server

Parameters
  • port number? start the mock server with given port

Returns any void

stopMockServer

Stop the mock server

Returns any void

addInteractionToMockServer

An interaction adds behavior to the mock server

I.addInteractionToMockServer({
   request: {
     method: 'GET',
     path: '/api/hello'
   },
   response: {
     status: 200,
     body: {
       'say': 'hello to mock server'
     }
   }
});
// with query params
I.addInteractionToMockServer({
   request: {
     method: 'GET',
     path: '/api/hello',
     queryParams: {
      id: 2
    }
   },
   response: {
     status: 200,
     body: {
       'say': 'hello to mock server'
     }
   }
});
Parameters
  • interaction object add behavior to the mock server

Returns any void