A simple low-dependency nodejs module that downloads, installs and starts elasticsearch v5.x locally for integration testing. Please note that this module currently only supports elasticsearch v5.x i.e. older versions are not supported.
The module attempts to download and unzip the package from the Official Elasticsearch artifacts source, if not already downloaded, and starts elasticsearch as a detached nodejs background process listening on the specified port.
To get started, npm install this module as a dev dependency:
npm install --save-dev node-es-local
This module has a single external dependency on the following module for unzipping:
- adm-zip - a pure Javascript (no-dependency) implementation of zip for nodejs
Once installed, you can use the module in your mocha tests or within gulp tasks.
To download and start a local instance of elasticsearch:
const ElasticsearchLocal = require('node-es-local')
const elasticsearchVersion = '5.2.0'
const elasticsearchPort = 9500
const cacheDirectory = './.cache'
const targetDirectory = './.installs'
new ElasticsearchLocal(elasticsearchVersion, elasticsearchPort, {
cacheDirectory: cacheDirectory,
installationDirectory: targetDirectory
}).start()
start() returns a promise.
To stop a local instance of elasticsearch started by the previous script:
const ElasticsearchLocal = require('node-es-local')
const elasticsearchVersion = '5.2.0'
const elasticsearchPort = 9500
const cacheDirectory = './.cache'
const targetDirectory = './.installs'
new ElasticsearchLocal(elasticsearchVersion, elasticsearchPort, {
cacheDirectory: cacheDirectory,
installationDirectory: targetDirectory
}).stop()
stop() returns a promise.
Port 9200 is the default port if not specified and ./.cachedArtifacts and ./.installs are the default cache and default installation directories. Hence, if you are ok with the defaults, the following script is all you need:
const ElasticsearchLocal = require('node-es-local')
new ElasticsearchLocal('5.2.0').start()
Simples!
The following example shows how you can use node-es-local with mocha to start a local instance of elasticsearch for integration tests:
const ElasticsearchLocal = require('node-es-local')
describe('Some feature eg search', () => {
before('given an index with some entries', () => {
return new ElasticsearchLocal('5.2.0').start() // return the promise
.then(() => {
// Put some data in...
})
})
after(() => {
return new ElasticsearchLocal('5.2.0').stop() // return the promise
})