Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@swimlane/cy-mockapi
Advanced tools
Changelog
2.0.2 (2021-11-09)
(none)
Readme
Easily mock your REST API in Cypress by putting responses in the fixtures directory tree.
npm install --save-dev @swimlane/cy-mockapi
Import and setup the plugin in cypress/plugins/index.js
:
import { installPlugin } from '@swimlane/cy-mockapi/plugin'
module.exports = (on, config) => {
installPlugin(on, config);
}
Import the Cypress commands in cypress/support/index.js
import '@swimlane/cy-mockapi/commands';
Add one or more folders in cypress/fixtures
to act as the mock API. Within this folder add files with the naming scheme:
{API PATH}/{METHOD}.{EXT}
For example:
cypress/fixtures/mocks
└── user
├── get.json
├── post.json
└── messages
├── get.json
├── delete.json
└── put.json
In your test files:
describe('Some Feature', () => {
beforeEach(() => {
cy.server();
cy.mockApi({
apiPath: '/api/',
mocksFolder: './mocks/',
cache: true
});
});
it('should do the thing', () => {
// Your test code
});
})
In this example, @swimlane/cy-mockapi
will read the cypress/fixtures/mocks
folder and stub responses to the API requests; functionally equivalent to:
cy.route('GET', '/api/user', 'fixture:mocks/user/get.json').as('GET:user');
cy.route('POST', '/api/user', 'fixture:mocks/user/post.json').as('POST:user');
cy.route('GET', '/api/user/messages', 'fixture:mocks/user/messages/get.json').as('GET:user/messages');
cy.route('DELETE', '/api/user/messages', 'fixture:mocks/user/messages/delete.json').as('DELETE:user/messages');
cy.route('PUT', '/api/user/messages', 'fixture:mocks/user/messages/put.json').as('PUT:user/messages');
cy.mockApi
Reads and (optionally) caches the mocksFolder
tree and sets up stubs. Note that this does not read the fixtures themselves; instead it sets up the stubbing (internally using cy.route
). cy.mockApi
may be called multiple times to mock different sets of fixtures or combine sets of fixtures.
apiPath
- The API root path to mock.mocksFolder
- The fixtures folder to use.cache
- Boolean to use caching (caching is per unique mocksFolder
)cy.logExtraApiCalls
Call cy.logExtraApiCalls(apiPath)
after cy.mockApi
to log requests and responses that are not stubbed by cy.mockApi
. This is useful for capturing responses while building out your mock files.
cy.failExtraApiCalls
Call cy.failExtraApiCalls(apiPath)
after cy.mockApi
to log and fail on requests that are not stubbed by cy.mockApi
.
Fixture files (located within mocksFolder
) in general should be named {method}.{ext}
(for now only json
and txt
are supported). The API path will be generated from the path as described in the example above.
To match against a route with a wildcard create a directory named or containing __
(double underscore) in place of the wildcard (*
). For example, to mock the response of a GET request to /api/user/*/profile
, create the fixture user/__/profile/get.json
.
To match against Query string parameters create a directory or file containing --
(double hyphen) in place of the ?
. For example, to mock the response of a GET request to /api/user?id=1
, create fixture user--id=1/get.json
or user/--id=1.get.json
.
options
filesFor additional flexibility create options.json
files within the apiPath
. These files are read by cy.mockApi
and passed to cy.route
. For example, the following file:
[
{
"url": "user?userid=*",
"response": "delete-user.json",
"method": "DELETE",
"alias": "deleteUser",
}
]
will setup file following route and stubbing:
cy.route({
url: '/api/user?userid=*',
response: 'fx:mocks/delete-user.json',
method: 'DELETE',
}).as('deleteUser');
See the Cypress Documentation for more details on the options available.
Inspired in part by https://github.com/namshi/mockserver.
cy-mockapi
is a Swimlane open-source project; we believe in giving back to the open-source community by sharing some of the projects we build for our application. Swimlane is an automated cyber security operations and incident response platform that enables cyber security teams to leverage threat intelligence, speed up incident response and automate security operations.
FAQs
Easily mock your REST API in Cypress using fixtures
The npm package @swimlane/cy-mockapi receives a total of 164 weekly downloads. As such, @swimlane/cy-mockapi popularity was classified as not popular.
We found that @swimlane/cy-mockapi demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 50 open source maintainers collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.