All Projects β†’ dempfi β†’ Ava Playback

dempfi / Ava Playback

Licence: apache-2.0
πŸ“Ό πŸš€ Record and playback http requests from your ava tests

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Ava Playback

dubbo-mock
dubbo mock web server
Stars: ✭ 62 (-50%)
Mutual labels:  mock, testing-tools
Nsubstitute
A friendly substitute for .NET mocking libraries.
Stars: ✭ 1,646 (+1227.42%)
Mutual labels:  testing-tools, mock
deckard
DNS test harness
Stars: ✭ 28 (-77.42%)
Mutual labels:  mock, testing-tools
ts-mock-imports
Intuitive mocking library for Typescript class imports
Stars: ✭ 103 (-16.94%)
Mutual labels:  mock, testing-tools
Webmockr
R library for stubbing and setting expectations on HTTP requests
Stars: ✭ 37 (-70.16%)
Mutual labels:  testing-tools, mock
main
Mocks Server monorepo
Stars: ✭ 109 (-12.1%)
Mutual labels:  mock, testing-tools
Retromock
Java library for mocking responses in a Retrofit service.
Stars: ✭ 48 (-61.29%)
Mutual labels:  mock, testing-tools
Charlatan
Go Interface Mocking Tool
Stars: ✭ 195 (+57.26%)
Mutual labels:  testing-tools, mock
Lyrebird
η§»εŠ¨εΊ”η”¨ζ’δ»ΆεŒ–ζ΅‹θ―•ε·₯作台
Stars: ✭ 663 (+434.68%)
Mutual labels:  testing-tools, mock
Miragejs
A client-side server to build, test and share your JavaScript app
Stars: ✭ 4,384 (+3435.48%)
Mutual labels:  mock, testing-tools
mocat
🐈 Mocat is a mocking toolbar that allows you to interactively develop and test network requests.
Stars: ✭ 27 (-78.23%)
Mutual labels:  mock, testing-tools
Mongodb Memory Server
Spinning up mongod in memory for fast tests. If you run tests in parallel this lib helps to spin up dedicated mongodb servers for every test file in MacOS, *nix, Windows or CI environments (in most cases with zero-config).
Stars: ✭ 1,376 (+1009.68%)
Mutual labels:  testing-tools, mock
ruby-dns-mock
DNS mock server written on πŸ’Ž Ruby. Mimic any DNS records for your test environment with fake DNS server.
Stars: ✭ 50 (-59.68%)
Mutual labels:  mock, testing-tools
laika
Log, test, intercept and modify Apollo Client's operations
Stars: ✭ 99 (-20.16%)
Mutual labels:  mock, testing-tools
Mockito Scala
Mockito for Scala language
Stars: ✭ 231 (+86.29%)
Mutual labels:  testing-tools, mock
mockafka
A testing DSL for kafka-streams
Stars: ✭ 14 (-88.71%)
Mutual labels:  mock, testing-tools
Mockito
Most popular Mocking framework for unit tests written in Java
Stars: ✭ 12,453 (+9942.74%)
Mutual labels:  testing-tools, mock
Mocktopus
Mocking framework for Rust
Stars: ✭ 179 (+44.35%)
Mutual labels:  testing-tools, mock
DeepfakeHTTP
DeepfakeHTTP is a web server that uses HTTP dumps as a source for responses.
Stars: ✭ 373 (+200.81%)
Mutual labels:  mock, testing-tools
Suman
πŸŒ‡ πŸŒ† πŸŒ‰ Advanced, user-friendly, language-agnostic, super-high-performance test runner. http://sumanjs.org
Stars: ✭ 57 (-54.03%)
Mutual labels:  testing-tools, ava


Dealing with HTTP requests in the tests can be painful and what is more important error prone, since we have to stub everything by hand. There are few libraries to help with this situation, like node-nock. But their setup is something complicated (I didn't manage to setup node-nock to record requests and play them back) and they require a lot of manual steps to write single test.

πŸ“Ό ava-playback is here to help. In record mode, when you write your test, you just allow your app to call real APIs and when you ready, you just switch from record to playback mode and it's done πŸŽ‰. In background ava-playback will record new requests and use already existing playbacks for the rest.

Installation

First things first

yarn add ava-playback

or

npm i ava-playback --save

Then in you package.json where you store your ava config just add a requirement of ava-playback.

  // ...
  "ava": {
    "require": [
      "ava-playback"
    ]
  }
  // ...

πŸŽ‰ that's it.

Playbacks location

By default playbacks will be stored in the root of your project in /playbacks folder, if you want to change the location just add playbacks settings in your package.json.

  // ...
  "ava": {
    "require": [
      "ava-playback"
    ],
    "playbacks": "tests/fixtures"
  },
  // ...

Modes

ava-playback uses env variable AVA_PLAYBACK to get information how it should run. If AVA_PLAYBACK isn't set, ava-playback will not do anything.

  • AVA_PLAYBACK=record record all new outgoing requests from your tests
  • AVA_PLAYBACK=play turn off HTTP/HTTPS connection and use playbacks to reply to the outgoing requests from the tests

Usage

With ava-playback the flow of writing actual test can look like this.

  1. You write a new test and don't activate ava-playback in any mode.
  2. When the test is ready you run it one more time with AVA_PLAYBACK=record env variable. ava-playback will record only missing playbacks to playbacks location.
  3. You edit new playbacks according to your needs (wildcard auth tokens in the body or in the queries).
  4. Check all tests with AVA_PLAYBACK=play mode to verify they pass.
  5. Done πŸš€

To illustrate the flow take a look at this example

# Write new test file
NODE_ENV=test ava --watch 'new-test-file.js'

# Record all playbacks required for 'new-test-file.js'
NODE_ENV=test AVA_PLAYBACK=record ava 'new-test-file.js'

# Check all tests together
NODE_ENV=test AVA_PLAYBACK=play ava

Wildcards and security

By default ava-playback stores and plays back your requests by taking into account all queries and body. However, you can change this option to hide security information, like tokens in queries.

For example, Slack API allows tokens to be in query params, like slack.com/api/users.list?token=xoxb-XXXXXXXXXX-Z7RKNoLIKOXLPKqtxUy5IhJ5. It's totally fine unless you don't want this token to be stored in git or be available in Travis-ci. For those cases, you can use wildcards feature of ava-playback.

After recording the playback, in you playbacks folder you can find the file matching that request and edit a path entry from

"/api/users.list?token=xoxb-XXXXXXXXXX-Z7RKNoLIKOXLPKqtxUy5IhJ5"

to

"/api/users.list?token=*"

so the file will look like this

{
  "body": "",
  "method": "POST",
  "path": "/api/users.list?token=*",
  "scope": "https://slack.com:443",
  "status": 200,
}

and all future requests to slack API with anything in place of the actual token will be caught by ava-playback.

Wildcard substitution rules

ava-playback support wildcards only for queries, however, this may change over. Wildcards can only catch whole strings as values or as part of an array like in the examples below.

Whole word match

"/api/users.list?token=*"

will match these paths

"/api/users.list?token=34"
"/api/users.list?token=xoxb-XXXXXXXXXX-Z7RKNoLIKOXLPKqtxUy5IhJ5"

In array match

"/api/users.list?tokens=78&tokens=*&tokens=some-token"

will match anything in the same position, like

"/api/users.list?tokens=78&tokens=anything-here&tokens=some-token"
made with ❀️ by @dempfi
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].