Pistolet
Pistolet (pronounced pistol-eh) is a Javascript testing tool to create mock API responses.
Installation
$ npm install pistolet --save-dev
Available Plugins
Pistolet does not come with any integration out-of-the-box, but has plugins to do so:
General Usage
;
Note: This example uses Jasmine, although this is not a requirement.
You are more than welcome to use it with other test suites, and contributing examples would be greatly appreciated.
Override existing scenarios
Pistolet allows to override the scenarios previously defined.
These overrides have a shorter lifespan and will be discarded after calling reset()
.
; ;
Writing scenarios
JSON Files
JSON scenarios are the easiest to write and use.
Make sure to set the dir
property in the configuration for Pistolet to know where to find these files.
Javascript/Typescript Scenarios
; ;
At the end of next()
, you can return:
- A
Mock
object (same type as thematch
parameter), which will be sent immediately - A
Promise<Mock>
object, which will be sent when the promise resolves true
, to indicate the scenario has a custom logic and will handle the responsefalse
orundefined
, to indicate that there was no match
JSON format
URL vs path & query
URL matching will perform a simple string comparison:
You can also use path
and query
to the same effect:
Using query parameters will perform an object comparison, which is not sensitive to the order in which the parameters come in the URL.
URLs, paths and query parameters can use regular expressions:
Delayed responses
Pistolet will return the reponse after delay
milliseconds if the property is present: