Socket
Book a DemoInstallSign in
Socket

@codeceptjs/mock-server-helper

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codeceptjs/mock-server-helper

Mock Server helper for CodeceptJS

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
1
-50%
Maintainers
0
Weekly downloads
 
Created
Source

MockServer

Table of Contents

  • config
  • Configuration
  • MockService
  • Methods

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

MockService

MockServer

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: {
      // default mock server config
      port: 9393,
      host: 'mock-service.test',
    },
  }
}
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.

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.

Happy testing with MockServer in CodeceptJS! 🚀

Methods

Parameters

  • passedConfig

startMockServer

Start the mock server

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 (CodeceptJS.MockInteraction | object) add behavior to the mock server

Returns any void

FAQs

Package last updated on 04 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts