Skip to content

Commit 8f741ca

Browse files
committed
feat(tests/hooks): adds husky hooks, ava and tests
1 parent b277015 commit 8f741ca

File tree

4 files changed

+2742
-84
lines changed

4 files changed

+2742
-84
lines changed

index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,31 @@ const AWS = require('aws-sdk')
44
const elasticsearch = require('elasticsearch')
55
const httpAWSESClass = require('http-aws-es')
66

7-
// serverless-offline will set IS_OFFLINE based on whether we're offline
8-
const devMode = process.env.IS_OFFLINE === 'true'
7+
module.exports = {
8+
createClient: createClient,
9+
getOptions: getOptions
10+
}
11+
12+
function getOptions (options) {
13+
options = options || {}
14+
15+
// serverless-offline will set IS_OFFLINE based on whether we're offline
16+
const devMode = Boolean(process.env.IS_OFFLINE)
917

10-
function createClient (options) {
1118
const prefix = options.envPrefix || 'AWS'
1219
const region = options.region || process.env[`${prefix}_REGION`]
1320
const host = options.host || process.env[`${prefix}_HOST`]
1421

15-
if (!region) { throw new Error('region is required') }
16-
if (!host) { throw new Error('host is required') }
22+
delete (options.region) // this doesn't belong in ES options
23+
24+
if (!region) { throw new TypeError('region is required') }
25+
if (!host) { throw new TypeError('host is required') }
1726

1827
const credentials = options.credentials || new AWS.EnvironmentCredentials(prefix)
1928

2029
const config = Object.assign({}, options, {
2130
host: host,
31+
region: region,
2232
amazonES: {
2333
region,
2434
credentials
@@ -30,7 +40,9 @@ function createClient (options) {
3040
config.connectionClass = httpAWSESClass
3141
}
3242

33-
return new elasticsearch.Client(config)
43+
return config
3444
}
3545

36-
module.exports = createClient
46+
function createClient (options) {
47+
return new elasticsearch.Client(getOptions(options))
48+
}

index.test.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
'use strict'
2+
3+
const test = require('ava')
4+
5+
test('region is required', function (t) {
6+
const error = t.throws(() => {
7+
getOptionsWithEnv()
8+
}, TypeError)
9+
10+
t.is(error.message, 'region is required')
11+
})
12+
13+
test('host is required', function (t) {
14+
const error = t.throws(() => {
15+
getOptionsWithEnv({}, {region: 'foo'})
16+
}, TypeError)
17+
18+
t.is(error.message, 'host is required')
19+
})
20+
21+
22+
test('region is taken from options if available', function (t) {
23+
const env = {
24+
AWS_REGION: 'aws_region',
25+
AWS_DDB_REGION: 'aws_ddb_region'
26+
}
27+
28+
const options = getOptionsWithEnv(env, {region: 'option_region', host: 'foo'})
29+
30+
t.is(options.amazonES.region, 'option_region')
31+
})
32+
33+
test('region is taken from AWS_REGION by default', function (t) {
34+
const env = {
35+
AWS_REGION: 'aws_region'
36+
}
37+
38+
const options = getOptionsWithEnv(env, {host: 'foo'})
39+
40+
t.is(options.amazonES.region, 'aws_region')
41+
})
42+
43+
test('region prefers envPrefix variable', function (t) {
44+
const env = {
45+
AWS_REGION: 'aws_region',
46+
AWS_DDB_REGION: 'aws_ddb_region'
47+
}
48+
49+
const options = getOptionsWithEnv(env, {envPrefix: 'AWS_DDB', host: 'foo'})
50+
51+
t.is(options.amazonES.region, 'aws_ddb_region')
52+
})
53+
54+
test('connectionClass ignored in offline mode', function (t) {
55+
const env = {
56+
IS_OFFLINE: true
57+
}
58+
59+
const options = getOptionsWithEnv(env, {region: 'foo', host: 'bar'})
60+
61+
t.falsy(options.connectionClass)
62+
})
63+
64+
test('connectionClass set by default', function (t) {
65+
const options = getOptionsWithEnv(null, {region: 'foo', host: 'bar'})
66+
67+
t.is(typeof (options.connectionClass), 'function')
68+
})
69+
70+
71+
test('sets environment credentials by default', function (t) {
72+
const options = getOptionsWithEnv({}, {region: 'foo', host: 'bar'})
73+
74+
t.is(options.amazonES.credentials.constructor.name, 'EnvironmentCredentials')
75+
})
76+
77+
function getOptionsWithEnv (env, options) {
78+
process.env = env || {}
79+
options = options || {}
80+
81+
return require('./index').getOptions(options)
82+
}

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"description": "Elasticsearch client for AWS that plays nicely with serverless-offline and signed requests",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"precommit": "npm test",
8+
"prepush": "npm test",
9+
"preversion": "npm test",
10+
"test": "./node_modules/.bin/ava --verbose"
811
},
912
"repository": {
1013
"type": "git",
@@ -23,8 +26,12 @@
2326
},
2427
"homepage": "https://github.com/goldcaddy77/serverless-elasticsearch-client#readme",
2528
"dependencies": {
26-
"aws-sdk": "^2.20.0",
2729
"elasticsearch": "^12.1.3",
2830
"http-aws-es": "^1.1.3"
31+
},
32+
"devDependencies": {
33+
"ava": "^0.18.2",
34+
"aws-sdk": "^2.23.0",
35+
"husky": "^0.13.2"
2936
}
3037
}

0 commit comments

Comments
 (0)