Skip to content

Commit

Permalink
Use arrow functions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jessehansen committed Jan 8, 2016
1 parent da033dc commit 95282d4
Showing 1 changed file with 76 additions and 61 deletions.
137 changes: 76 additions & 61 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,108 +6,123 @@ const _ = require('lodash');
const Promise = require('bluebird');
const consul = require('consul');

function execute(commandLine) {
return new Promise(function(resolve, reject) {
const execute = (commandLine) => {
return new Promise((resolve, reject) => {
exec(commandLine, {
cwd: __dirname,
env: process.env
}, function(err, stdout, stderr) {
}, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout: stdout, stderr: stderr });
resolve({
stdout: stdout,
stderr: stderr
});
}
});
});
}
};

describe('consul-kv-sync', function() {
var config = {
describe('consul-kv-sync', () => {
const config = {
host: process.env.CONSUL_HOST || 'consul.service.consul',
port: process.env.CONSUL_PORT || '8500',
secure: process.env.CONSUL_SECURE === 'true'
};

var client = consul(config);
const client = consul(config);
Promise.promisifyAll(client.kv);

after(function(){
return client.kv.delAsync({ key:'service', recurse:true })
.then(function(){
return client.kv.delAsync({ key:'service2', recurse:true });
})
.then(function(){
return client.kv.delAsync({ key:'other_service', recurse:true });
after(() => {
return client.kv.delAsync({
key: 'service',
recurse: true
})
.then(() => {
return client.kv.delAsync({
key: 'service2',
recurse: true
});
})
.then(() => {
return client.kv.delAsync({
key: 'other_service',
recurse: true
});
});
});

describe('#validation', function() {
it('should return an error when an empty config file is added', function() {
describe('#validation', () => {
it('should return an error when an empty config file is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./empty.json')
.then(function(){
.then(() => {
throw new Error('Expected failure, got success');
})
.catch(function(err) {
.catch((err) => {
expect(err.code).to.eql(99);
});
});
it('should return an error when a config file with multiple top-level nodes is added', function() {
it('should return an error when a config file with multiple top-level nodes is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./full.json')
.then(function(){
.then(() => {
throw new Error('Expected failure, got success');
})
.catch(function(err) {
.catch((err) => {
expect(err.code).to.eql(99);
});
});
it('should return an error when an different service\'s config file is added', function() {
it('should return an error when an different service\'s config file is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./other.json')
.then(function(){
.then(() => {
throw new Error('Expected failure, got success');
})
.catch(function(err) {
.catch((err) => {
expect(err.code).to.eql(99);
});
});
});

describe('#run', function() {
var response;
describe('first run scenario', function(){
before(function(){
return client.kv.delAsync({ key:'service', recurse:true })
.then(function(){
return execute('node ../consul-kv-sync.js ./one.json ./two.json');
}).then(function(){
return client.kv.getAsync({
key: 'service',
recurse: true
});
}).then(function(res){
response = res;
describe('#run', () => {
let response;
describe('first run scenario', () => {
before(() => {
return client.kv.delAsync({
key: 'service',
recurse: true
})
.then(() => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json');
}).then(() => {
return client.kv.getAsync({
key: 'service',
recurse: true
});
}).then((res) => {
response = res;
});
});

it('should set value to correct value', function() {
var item = response.find(function(item) {
it('should set value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/two';
});

expect(item).to.be.ok;
expect(item.Value).to.eql('value 2');
});

it('should set overridden value to correct value', function() {
var item = response.find(function(item) {
it('should set overridden value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/one';
});

expect(item).to.be.ok;
expect(item.Value).to.eql('value from file two');
});

it('should set array parameters correctly', function() {
var items = _.filter(response, function(item) {
it('should set array parameters correctly', () => {
let items = _.filter(response, (item) => {
return /^service\/arrayparam/.test(item.Key);
});

Expand All @@ -120,24 +135,24 @@ describe('consul-kv-sync', function() {
});
});

describe('second run scenario', function(){
before(function() {
describe('second run scenario', () => {
before(() => {
return client.kv.setAsync({
key: 'service/four',
value: 'value for removal'
}).then(function() {
}).then(() => {
return client.kv.setAsync({
key: 'service2/item',
value: 'this value should stay'
});
}).then(function() {
}).then(() => {
return client.kv.setAsync({
key: 'service/one',
value: 'this value should be changed'
});
}).then(function() {
}).then(() => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json');
}).then(function() {
}).then(() => {
return client.kv.getAsync({
key: 'service',
recurse: true
Expand All @@ -147,26 +162,26 @@ describe('consul-kv-sync', function() {
});
});

it('should set value to correct value', function() {
var item = response.find(function(item) {
it('should set value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/two';
});

expect(item).to.be.ok;
expect(item.Value).to.eql('value 2');
});

it('should set overridden value to correct value', function() {
var item = response.find(function(item) {
it('should set overridden value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/one';
});

expect(item).to.be.ok;
expect(item.Value).to.eql('value from file two');
});

it('should set array parameters correctly', function() {
var items = _.filter(response, function(item) {
it('should set array parameters correctly', () => {
let items = _.filter(response, (item) => {
return /^service\/arrayparam/.test(item.Key);
});

Expand All @@ -178,18 +193,18 @@ describe('consul-kv-sync', function() {
expect(items[3].Value).to.eql('4');
});

it('should remove existing keys that are not in config file', function() {
var items = _.filter(response, function(item) {
it('should remove existing keys that are not in config file', () => {
let items = _.filter(response, (item) => {
return item.Key === 'service/four';
});

expect(items.length).to.eql(0);
});

it('should not impact keys for a different service', function() {
it('should not impact keys for a different service', () => {
return client.kv.getAsync({
key: 'service2/item'
}).then(function(item) {
}).then((item) => {
expect(item.Value).to.eql('this value should stay');
});
});
Expand Down

0 comments on commit 95282d4

Please sign in to comment.