diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 38550268f..5c3e8529f 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -119,6 +119,27 @@ jobs: npm start --prefix test/bundlers/rollup-test npm start --prefix test/bundlers/webpack-test + mock-support: + name: Mock support + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Use Node.js 14.x + uses: actions/setup-node@v1 + with: + node-version: 14.x + + - name: Install + run: | + npm install + npm install --prefix test/mock + + - name: Run test + run: | + npm test --prefix test/mock + code-coverage: name: Code coverage runs-on: ubuntu-latest diff --git a/test/mock/index.js b/test/mock/index.js new file mode 100644 index 000000000..d9525299d --- /dev/null +++ b/test/mock/index.js @@ -0,0 +1,71 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +'use strict' + +const { test } = require('tap') +const { Client, errors } = require('../../') +const Mock = require('@elastic/elasticsearch-mock') + +test('Mock should work', async t => { + t.plan(1) + + const mock = new Mock() + const client = new Client({ + node: 'http://localhost:9200', + Connection: mock.getConnection() + }) + + mock.add({ + method: 'GET', + path: '/_cat/indices' + }, () => { + return { status: 'ok' } + }) + + const response = await client.cat.indices() + t.same(response.body, { status: 'ok' }) +}) + +test('Return an error', async t => { + t.plan(1) + + const mock = new Mock() + const client = new Client({ + node: 'http://localhost:9200', + Connection: mock.getConnection() + }) + + mock.add({ + method: 'GET', + path: '/_cat/indices' + }, () => { + return new errors.ResponseError({ + body: { errors: {}, status: 500 }, + statusCode: 500 + }) + }) + + try { + await client.cat.indices() + t.fail('Should throw') + } catch (err) { + t.ok(err instanceof errors.ResponseError) + } +}) diff --git a/test/mock/package.json b/test/mock/package.json new file mode 100644 index 000000000..0017d01cd --- /dev/null +++ b/test/mock/package.json @@ -0,0 +1,18 @@ +{ + "name": "mock", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "standard && tap index.js" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "@elastic/elasticsearch": "file:../..", + "@elastic/elasticsearch-mock": "^0.3.1", + "standard": "^16.0.3", + "tap": "^15.0.9" + } +}