Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
Convert to jest (#9)
Browse files Browse the repository at this point in the history
* Convert to jest

* Fix build
  • Loading branch information
johncmckim committed Mar 4, 2017
1 parent d60b7dc commit 379ca6d
Show file tree
Hide file tree
Showing 8 changed files with 1,462 additions and 336 deletions.
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins:
env:
mocha: true
node: true
jest: true

rules:
no-console: 0
Expand Down
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ node_js:
- '5.11'
- '6.2'

script: npm run lint && npm test
before_install:
- npm install -g yarn
install:
- yarn

script:
- yarn run lint
- yarn run test -- --coverage

after_success:
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls && rm -rf ./coverage
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
"version": "0.0.7",
"description": "",
"main": "src/index.js",
"scripts": {
"lint": "eslint .",
"lint-fix": "eslint . --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cover": "jest --coverage"
},
"repository": {
"url": "https://github.com/ACloudGuru/serverless-plugin-aws-alerts.git",
"type": "git"
},
"author": "John McKim <johncmckim@gmail.com>",
"license": "MIT",
"scripts": {
"lint": "eslint .",
"test": "istanbul cover node_modules/mocha/bin/_mocha test -- -R spec"
},
"dependencies": {
"lodash": "^4.16.6"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.16",
"eslint": "^3.15.0",
"eslint-config-standard": "^6.2.0",
"eslint-plugin-node": "^4.0.0",
"eslint-plugin-promise": "^3.4.1",
"eslint-plugin-standard": "^2.0.1",
"istanbul": "^0.4.5",
"mocha": "^3.1.2",
"sinon": "^1.17.6"
"jest": "^19.0.2"
}
}
115 changes: 53 additions & 62 deletions test/index.tests.js → src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

const path = require('path');

const expect = require('chai').expect;
const sinon = require('sinon');

const Plugin = require('../src');
const Plugin = require('./index');

const testServicePath = path.join(__dirname, '.tmp');

Expand Down Expand Up @@ -57,7 +54,7 @@ describe('#index', function () {
const expected = {};
const plugin = pluginFactory(expected);
const actual = plugin.getConfig();
expect(actual).to.equal(expected);
expect(actual).toEqual(expected);
});
});

Expand Down Expand Up @@ -88,7 +85,7 @@ describe('#index', function () {
const plugin = pluginFactory(config);
const actual = plugin.getDefinitions(config);

expect(actual).to.deep.equal({
expect(actual).toEqual({
functionInvocations: {
namespace: 'AWS/Lambda',
metric: 'Invocations',
Expand Down Expand Up @@ -162,7 +159,7 @@ describe('#index', function () {
const definitions = plugin.getDefinitions(config);
const actual = plugin.getFunctionAlarms({}, config, definitions);

expect(actual).to.deep.equal([{
expect(actual).toEqual([{
name: 'functionInvocations',
namespace: 'AWS/Lambda',
metric: 'Invocations',
Expand All @@ -181,7 +178,7 @@ describe('#index', function () {
alarms: []
}, config, definitions);

expect(actual).to.deep.equal([{
expect(actual).toEqual([{
name: 'functionInvocations',
namespace: 'AWS/Lambda',
metric: 'Invocations',
Expand All @@ -202,7 +199,7 @@ describe('#index', function () {
]
}, config, definitions);

expect(actual).to.deep.equal([{
expect(actual).toEqual([{
name: 'functionInvocations',
namespace: 'AWS/Lambda',
metric: 'Invocations',
Expand Down Expand Up @@ -239,7 +236,7 @@ describe('#index', function () {
}]
}, config, definitions);

expect(actual).to.deep.equal([{
expect(actual).toEqual([{
name: 'functionInvocations',
namespace: 'AWS/Lambda',
metric: 'Invocations',
Expand Down Expand Up @@ -268,7 +265,7 @@ describe('#index', function () {
alarms: [
'missingAlarm'
]
}, config, definitions)).to.throw(Error);
}, config, definitions)).toThrow(Error);
});
});

Expand All @@ -284,11 +281,11 @@ describe('#index', function () {
const config = plugin.getConfig();
const topics = plugin.compileAlertTopics(config);

expect(topics).to.be.deep.equal({
expect(topics).toEqual({
ok: topicArn
});

expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({});
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({});
});

it('should create SNS topic when name is passed', () => {
Expand All @@ -302,11 +299,11 @@ describe('#index', function () {
const config = plugin.getConfig();
const topics = plugin.compileAlertTopics(config);

expect(topics).to.be.deep.equal({
expect(topics).toEqual({
ok: { Ref: `AwsAlertsOk` }
});

expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({
'AwsAlertsOk': {
Type: 'AWS::SNS::Topic',
Properties: {
Expand All @@ -329,7 +326,7 @@ describe('#index', function () {

plugin.compileGlobalAlarms(config, definitions, alertTopics);

expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({
'GlobalFunctionThrottlesAlarm': {
Type: 'AWS::CloudWatch::Alarm',
Properties: {
Expand Down Expand Up @@ -373,7 +370,7 @@ describe('#index', function () {

plugin.compileGlobalAlarms(config, definitions, alertTopics);

expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({});
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({});
});

});
Expand All @@ -392,7 +389,7 @@ describe('#index', function () {

plugin.compileFunctionAlarms(config, definitions, alertTopics);

expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal({
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual({
'FooFunctionInvocationsAlarm': {
Type: 'AWS::CloudWatch::Alarm',
Properties: {
Expand Down Expand Up @@ -437,7 +434,7 @@ describe('#index', function () {
const alertTopics = plugin.compileAlertTopics(config);

plugin.compileFunctionAlarms(config, definitions, alertTopics);
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).to.deep.equal(
expect(plugin.serverless.service.provider.compiledCloudFormationTemplate.Resources).toEqual(
{
"FooBunyanErrorsAlarm": {
"Type": "AWS::CloudWatch::Alarm",
Expand Down Expand Up @@ -494,50 +491,44 @@ describe('#index', function () {
const stage = 'production';
let plugin = null;

let getConfigStub = null;
let getDefinitionsStub = null;
let compileAlertTopicsStub = null;
let compileGlobalAlarmsStub = null;
let compileFunctionAlarmsStub = null;

const expectCompiled = (config, definitions, alertTopics) => {
expect(getConfigStub.calledOnce).to.equal(true);
expect(plugin.getConfig.mock.calls.length).toEqual(1);

expect(getDefinitionsStub.calledOnce).to.equal(true);
expect(getDefinitionsStub.args[0][0]).to.equal(config);
expect(plugin.getDefinitions.mock.calls.length).toEqual(1);
expect(plugin.getDefinitions.mock.calls[0][0]).toEqual(config);

expect(compileAlertTopicsStub.calledOnce).to.equal(true);
expect(compileAlertTopicsStub.args[0][0]).to.equal(config);
expect(plugin.compileAlertTopics.mock.calls.length).toEqual(1);
expect(plugin.compileAlertTopics.mock.calls[0][0]).toEqual(config);

expect(compileGlobalAlarmsStub.calledOnce).to.equal(true);
expect(compileGlobalAlarmsStub.args[0][0]).to.equal(config);
expect(compileGlobalAlarmsStub.args[0][1]).to.equal(definitions);
expect(compileGlobalAlarmsStub.args[0][2]).to.equal(alertTopics);
expect(plugin.compileGlobalAlarms.mock.calls.length).toEqual(1);
expect(plugin.compileGlobalAlarms.mock.calls[0][0]).toEqual(config);
expect(plugin.compileGlobalAlarms.mock.calls[0][1]).toEqual(definitions);
expect(plugin.compileGlobalAlarms.mock.calls[0][2]).toEqual(alertTopics);

expect(compileFunctionAlarmsStub.calledOnce).to.equal(true);
expect(compileFunctionAlarmsStub.args[0][0]).to.equal(config);
expect(compileFunctionAlarmsStub.args[0][1]).to.equal(definitions);
expect(compileFunctionAlarmsStub.args[0][2]).to.equal(alertTopics);
expect(plugin.compileFunctionAlarms.mock.calls.length).toEqual(1);
expect(plugin.compileFunctionAlarms.mock.calls[0][0]).toEqual(config);
expect(plugin.compileFunctionAlarms.mock.calls[0][1]).toEqual(definitions);
expect(plugin.compileFunctionAlarms.mock.calls[0][2]).toEqual(alertTopics);
};

beforeEach(() => {
plugin = pluginFactory({}, stage);

getConfigStub = sinon.stub(plugin, 'getConfig');
getDefinitionsStub = sinon.stub(plugin, 'getDefinitions');
compileAlertTopicsStub = sinon.stub(plugin, 'compileAlertTopics');
compileGlobalAlarmsStub = sinon.stub(plugin, 'compileGlobalAlarms');
compileFunctionAlarmsStub = sinon.stub(plugin, 'compileFunctionAlarms');
plugin.getConfig = jest.fn();
plugin.getDefinitions = jest.fn();
plugin.compileAlertTopics = jest.fn();
plugin.compileGlobalAlarms = jest.fn();
plugin.compileFunctionAlarms = jest.fn();
});

it('should compile alarms - by default', () => {
const config = {};
const definitions = {};
const alertTopics = {};

getConfigStub.returns(config);
getDefinitionsStub.returns(definitions);
compileAlertTopicsStub.returns(alertTopics);
plugin.getConfig.mockImplementation(() => config);
plugin.getDefinitions.mockImplementation(() => definitions);
plugin.compileAlertTopics.mockImplementation(() => alertTopics);

plugin.compileCloudWatchAlarms();

Expand All @@ -551,41 +542,41 @@ describe('#index', function () {
const definitions = {};
const alertTopics = {};

getConfigStub.returns(config);
getDefinitionsStub.returns(definitions);
compileAlertTopicsStub.returns(alertTopics);
plugin.getConfig.mockImplementation(() => config);
plugin.getDefinitions.mockImplementation(() => definitions);
plugin.compileAlertTopics.mockImplementation(() => alertTopics);

plugin.compileCloudWatchAlarms();

expectCompiled(config, definitions, alertTopics);
});

it('should not compile alarms without config', () => {
getConfigStub.returns(null);
plugin.getConfig.mockImplementation(() => null);

plugin.compileCloudWatchAlarms();

expect(getConfigStub.calledOnce).to.equal(true);
expect(plugin.getConfig.mock.calls.length).toEqual(1);

expect(getDefinitionsStub.calledOnce).to.equal(false);
expect(compileAlertTopicsStub.calledOnce).to.equal(false);
expect(compileGlobalAlarmsStub.calledOnce).to.equal(false);
expect(compileFunctionAlarmsStub.calledOnce).to.equal(false);
expect(plugin.getDefinitions.mock.calls.length).toEqual(0);
expect(plugin.compileAlertTopics.mock.calls.length).toEqual(0);
expect(plugin.compileGlobalAlarms.mock.calls.length).toEqual(0);
expect(plugin.compileFunctionAlarms.mock.calls.length).toEqual(0);
});

it('should not compile alarms on invalid stage', () => {
getConfigStub.returns({
plugin.getConfig.mockImplementation(() => ({
stages: ['blah']
});
}));

plugin.compileCloudWatchAlarms();

expect(getConfigStub.calledOnce).to.equal(true);
expect(plugin.getConfig.mock.calls.length).toEqual(1);

expect(getDefinitionsStub.calledOnce).to.equal(false);
expect(compileAlertTopicsStub.calledOnce).to.equal(false);
expect(compileGlobalAlarmsStub.calledOnce).to.equal(false);
expect(compileFunctionAlarmsStub.calledOnce).to.equal(false);
expect(plugin.getDefinitions.mock.calls.length).toEqual(0);
expect(plugin.compileAlertTopics.mock.calls.length).toEqual(0);
expect(plugin.compileGlobalAlarms.mock.calls.length).toEqual(0);
expect(plugin.compileFunctionAlarms.mock.calls.length).toEqual(0);
});
});
});
16 changes: 7 additions & 9 deletions test/naming.tests.js → src/naming.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict';

const expect = require('chai').expect;

const Naming = require('../src/naming');
const Naming = require('./naming');

describe('#naming', function () {
describe('#getNormalisedName', () => {
Expand All @@ -12,19 +10,19 @@ describe('#naming', function () {
it('should normalise name', () => {
const expected = 'FuncName';
const actual = naming.getNormalisedName('funcName');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});

it('should normalise name with dash', () => {
const expected = 'FuncDashname';
const actual = naming.getNormalisedName('func-name');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});

it('should normalise name with underscore', () => {
const expected = 'FuncUnderscorename';
const actual = naming.getNormalisedName('func_name');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});
});

Expand All @@ -35,7 +33,7 @@ describe('#naming', function () {
it('should get lambda function name', () => {
const expected = 'FuncNameLambdaFunction';
const actual = naming.getLambdaFunctionCFRef('FuncName');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});
});

Expand All @@ -46,7 +44,7 @@ describe('#naming', function () {
it('should get alarm name', () => {
const expected = 'GlobalFunctionErrorsAlarm';
const actual = naming.getAlarmCFRef('functionErrors', 'Global');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});
});

Expand All @@ -57,7 +55,7 @@ describe('#naming', function () {
it('should get the pattern metric name', () => {
const expected = 'MetricNamefoo';
const actual = naming.getPatternMetricName('MetricName', 'foo');
expect(actual).to.equal(expected);
expect(actual).toBe(expected);
});
});
});
2 changes: 0 additions & 2 deletions test/.eslintrc.yml

This file was deleted.

3 changes: 0 additions & 3 deletions test/index.js

This file was deleted.

Loading

0 comments on commit 379ca6d

Please sign in to comment.