Skip to content

Commit f6180fa

Browse files
committed
Add test for mocking library (#1545)
1 parent 642b15c commit f6180fa

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

.github/workflows/nodejs.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ jobs:
119119
npm start --prefix test/bundlers/rollup-test
120120
npm start --prefix test/bundlers/webpack-test
121121
122+
mock-support:
123+
name: Mock support
124+
runs-on: ubuntu-latest
125+
126+
steps:
127+
- uses: actions/checkout@v2
128+
129+
- name: Use Node.js 14.x
130+
uses: actions/setup-node@v1
131+
with:
132+
node-version: 14.x
133+
134+
- name: Install
135+
run: |
136+
npm install
137+
npm install --prefix test/mock
138+
139+
- name: Run test
140+
run: |
141+
npm test --prefix test/mock
142+
122143
code-coverage:
123144
name: Code coverage
124145
runs-on: ubuntu-latest

test/mock/index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
'use strict'
21+
22+
const { test } = require('tap')
23+
const { Client, errors } = require('../../')
24+
const Mock = require('@elastic/elasticsearch-mock')
25+
26+
test('Mock should work', async t => {
27+
t.plan(1)
28+
29+
const mock = new Mock()
30+
const client = new Client({
31+
node: 'http://localhost:9200',
32+
Connection: mock.getConnection()
33+
})
34+
35+
mock.add({
36+
method: 'GET',
37+
path: '/_cat/indices'
38+
}, () => {
39+
return { status: 'ok' }
40+
})
41+
42+
const response = await client.cat.indices()
43+
t.same(response.body, { status: 'ok' })
44+
})
45+
46+
test('Return an error', async t => {
47+
t.plan(1)
48+
49+
const mock = new Mock()
50+
const client = new Client({
51+
node: 'http://localhost:9200',
52+
Connection: mock.getConnection()
53+
})
54+
55+
mock.add({
56+
method: 'GET',
57+
path: '/_cat/indices'
58+
}, () => {
59+
return new errors.ResponseError({
60+
body: { errors: {}, status: 500 },
61+
statusCode: 500
62+
})
63+
})
64+
65+
try {
66+
await client.cat.indices()
67+
t.fail('Should throw')
68+
} catch (err) {
69+
t.ok(err instanceof errors.ResponseError)
70+
}
71+
})

test/mock/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "mock",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "standard && tap index.js"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"@elastic/elasticsearch": "file:../..",
14+
"@elastic/elasticsearch-mock": "^0.3.1",
15+
"standard": "^16.0.3",
16+
"tap": "^15.0.9"
17+
}
18+
}

0 commit comments

Comments
 (0)