Skip to content

A mock server for the Algolia search engine allowing integration testing without the need to hit Algolia's actual servers

License

Notifications You must be signed in to change notification settings

davesag/mock-algolia

Repository files navigation

mock-algolia

A mock server that mimics the Algolia Search and Indexing APIs.

Docker

Links

Current Status

This is very much version 0.0.x and a hack, not a fully fledged system.

  • Very minimal support for adding, updating, and removing data from indices.
  • No search queries supported yet (coming when I need them/get around to them)
  • I've not written unit or integration tests for the main routes.

To Use

I am going to assume you are using the Algolia Javascript API Client here, but if not then similar patterns will apply.

Prep the client

Normally you'd do this

const client = algoliasearch(applicationId, apiKey)
const index = client.initIndex('some-index-name')

await index.addObjects(arrayOfObjects)

To fool the algoliasearch client into talking to this mock server, do this instead:

const client = algoliasearch(applicationId, apiKey, { protocol: 'http:' })
client.hosts = {
  read: ['http://localhost:3000'],
  write: ['http://localhost:3000']
}

const index = client.initIndex('some-index-name')

await index.addObjects(arrayOfObjects)

Then all the client requests will go to the mock server (assuming it's running on port 3000)

Checking what was sent

You can get a quick overview of what has been sent to the mock server via get `/requests'.

Resetting the mock server

You can reset the request log by sending [delete /requests]

Configuration

Set the following environment variables

Variable Default Notes
PORT 3000 The port the server listens on

Development

Branches

Branch Tests Code Coverage Audit Comments
develop CircleCI codecov Vulnerabilities Work in progress
main CircleCI codecov Vulnerabilities Latest Production Release

Prerequisites

  • NodeJS. I use nvm to manage Node versions — brew install nvm.
  • Docker if you want to use the Swagger Editor. (Use Docker for Mac, not the homebrew version)

To build and run locally

Clone this (or better yet, fork it then clone your fork)

npm install
npm start

Go to localhost:3000/docs to see the docs.

.env file

You can put environment variables in a .env file.

Testing

  • npm test to run the unit tests
  • npm run test:server will run the integration tests
  • npm run lint will lint it
  • npm run prettier will prettify it
  • npm run test:unit:cov will run the unit tests with code coverage

Contributing

Please see the contributing notes.

About

A mock server for the Algolia search engine allowing integration testing without the need to hit Algolia's actual servers

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published