Skip to content

Commit

Permalink
tests: added assertion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NatalieWolfe committed Jul 25, 2016
1 parent 28e0672 commit 9af5df2
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**/node_modules/**
**/node_modules/**
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
docs
tests
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- "0.10"
- "0.12"
- "4"
- "5"
- "6"
env:
- SUITE=unit
- SUITE=lint
install:
- npm install
script:
- npm run $SUITE
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# node-newrelic-tester
# New Relic Tester
Library full of test helpers for instrumentation modules.
5 changes: 5 additions & 0 deletions bin/hooks/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#! /bin/bash

# Copy this file into your .git/hooks directory to enable it.

npm run lint
20 changes: 10 additions & 10 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

var _ = require('lodash')
var Metrics = require('newrelic/lib/metrics')
var TestAgent = require('./agent')
var Transaction = require('newrelic/lib/transaction')
var util = require('util')
Expand Down Expand Up @@ -178,7 +177,7 @@ function _transactionTest(tx) {
return null
}

function _metricsTest(expected, exact){
function _metricsTest(expected, exact) {
var metrics = TestAgent.instance.agent.metrics

for (var i = 0; i < expected.length; ++i) {
Expand Down Expand Up @@ -211,7 +210,7 @@ function _metricsTest(expected, exact){
}
}

if (exact && metrics.toJSON().length != expected.length) {
if (exact && metrics.toJSON().length !== expected.length) {
return util.format(
'Metric has %d elements, expected %d.',
metrics.toJSON().length, expected.length
Expand All @@ -230,13 +229,14 @@ function _segmentsTest(parent, _expected) {
// [{name: "name", children: [{name: "child"}]}]
var stack = [[parent.children, _expected]]

var findChild = function(children, name) {
return children.find(function(child) {
var findChild = function findChild(children, name) {
// FIXME: Change to `.find` after deprecating 0.10 and 0.12.
return children.filter(function findChild_childrenFind(child) {
if (name instanceof RegExp) {
return name.test(child.name)
}
return child.name.indexOf(name) >= 0
})
})[0] // <-- FIXME remove this too.
}

while (stack.length) {
Expand All @@ -246,10 +246,10 @@ function _segmentsTest(parent, _expected) {

for (var i = 0; i < expected.length; ++i) {
var expectedChild = expected[i]
var child = findChild(children, expected.name)
var child = findChild(children, expectedChild.name)

if (!child) {
return util.format('Missing child segment "%s"', expected.name)
return util.format('Missing child segment "%s"', expectedChild.name)
}

if (expectedChild.children) {
Expand All @@ -259,7 +259,7 @@ function _segmentsTest(parent, _expected) {
return err
}
} else {
stack.push([child, expectedChild.children])
stack.push([child.children, expectedChild.children])
}
}
}
Expand Down Expand Up @@ -299,7 +299,7 @@ function _exactSegmentsTest(parent, _expected) {
return err
}
} else {
stack.push([child, expectedChild.children])
stack.push([child.children, expectedChild.children])
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"docs": "rm -r docs; jsdoc -c ./jsdoc-conf.json -d docs -r *.js lib/",
"lint": "eslint *.js lib/*",
"test": "tap tests/unit/*.tap.js tests/unit/*/*.tap.js"
"test": "npm run unit",
"unit": "tap tests/unit/*.tap.js tests/unit/*/*.tap.js"
},
"repository": {
"type": "git",
Expand All @@ -19,9 +20,13 @@
"url": "https://raw.github.com/newrelic/node-newrelic/master/LICENSE"
}
],
"engines": {
"node": ">=0.10.0"
},
"devDependencies": {
"eslint": "^2.13.1",
"jsdoc": "^3.4.0",
"newrelic": "^1.28.1",
"tap": "^5.7.3"
},
"dependencies": {
Expand Down
230 changes: 230 additions & 0 deletions tests/unit/assert.tap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
'use strict'

var _ = require('lodash')
var assert = require('../../lib/assert')
var Metrics = require('newrelic/lib/metrics')
var tap = require('tap')
var TestAgent = require('../../lib/agent')


var helper = null
tap.beforeEach(function(done) {
helper = new TestAgent()
done()
})
tap.afterEach(function(done) {
helper.unload()
helper = null
done()
})

tap.notOk(tap.transaction, 'should not extend tap on require')
tap.notOk(tap.metrics, 'should not extend tap on require')
tap.notOk(tap.exactMetrics, 'should not extend tap on require')
tap.notOk(tap.segments, 'should not extend tap on require')
tap.notOk(tap.exactSegments, 'should not extend tap on require')

tap.test('assert.extendTap', function(t) {
assert.extendTap(tap)

tap.type(tap.transaction, 'function', 'should extend tap with more assertions')
tap.type(tap.metrics, 'function', 'should extend tap with more assertions')
tap.type(tap.exactMetrics, 'function', 'should extend tap with more assertions')
tap.type(tap.segments, 'function', 'should extend tap with more assertions')
tap.type(tap.exactSegments, 'function', 'should extend tap with more assertions')

t.end()
})

tap.test('assert.transaction', function(t) {
t.throws(function() {
assert.transaction(null)
}, {message: /is null/}, 'should throw if no transaction given')
t.throws(function() {
assert.transaction({})
}, {message: /is not a.*?Transaction/}, 'should throw if not a Transaction')

var tx1 = null
helper.runInTransaction(function(tx) { tx1 = tx })
helper.runInTransaction(function(tx2) {
t.throws(function() {
assert.transaction(tx1)
}, {
message: /Transaction.*?is not the current transaction/
}, 'should throw if transaction is not the current one')

t.doesNotThrow(function() {
assert.transaction(tx2)
}, 'should not throw for current transaction')

t.transaction(tx2, 'should work on tap assertion too')
})

t.throws(function() {
assert.transaction(null, 'foobar')
}, {message: 'foobar'}, 'should use message if provided')

t.end()
})

tap.test('assert.metrics', function(t) {
var metrics = helper.agent.metrics
metrics.getOrCreateMetric('foobar').incrementCallCount();
metrics.getOrCreateMetric('fizbang').incrementCallCount();

t.doesNotThrow(function() {
assert.metrics(['foobar'])
}, 'should not throw checking just a string')

t.doesNotThrow(function() {
assert.metrics(['foobar', 'fizbang'])
}, 'should not throw checking multiple in array')

t.doesNotThrow(function() {
assert.metrics([{name: 'foobar'}])
}, 'should not throw checking objects with just a name')

t.doesNotThrow(function() {
assert.metrics([{name: 'foobar', callCount: 1}])
}, 'should not throw checking object with name and metric value')

t.throws(function() {
assert.metrics(['doesNotExist'])
}, {message: /Missing metric.*?doesNotExist/}, 'should throw for missing metrics')

t.throws(function() {
assert.metrics([{name: 'doesNotExist'}])
}, {message: /Missing metric.*?doesNotExist/}, 'should throw for missing metrics')

t.throws(function() {
assert.metrics([{name: 'foobar', callCount: 5}])
}, {message: /does not match expected/}, 'should throw for wrong metric values')

t.throws(function() {
assert.metrics(['foobar', 'doesNotExist'])
}, {message: /Missing metric.*?doesNotExist/}, 'should work with multiple values')

t.metrics(['foobar'], 'names should work with tap')
t.metrics([{name: 'foobar', callCount: 1}], 'objects should work with tap too')

t.end()
})

tap.test('assert.exactMetrics', function(t) {
var metrics = helper.agent.metrics
metrics.getOrCreateMetric('foobar').incrementCallCount();
metrics.getOrCreateMetric('fizbang').incrementCallCount();

t.throws(function() {
assert.exactMetrics(['foobar'])
}, {message: /has 2 elements.*?expected 1/}, 'should throw if missing metrics')

t.doesNotThrow(function() {
assert.exactMetrics(['foobar', 'fizbang'])
}, 'should not throw if all metrics specified')

t.end()
})

tap.test('assert.segments', function(t) {
var tx = helper.runInTransaction(function(tx) { return tx })
var root = tx.trace.root
var child = root.add('child')
root.add('sibling')
child.add('grandchild')

t.doesNotThrow(function() {
assert.segments(root, [{name: 'child'}])
}, 'should match a single child')

t.doesNotThrow(function() {
assert.segments(root, [{name: 'child'}, {name: 'sibling'}])
}, 'should match both children')

t.doesNotThrow(function() {
assert.segments(root, [{name: 'sibling'}, {name: 'child'}])
}, 'should not care about ordering')

t.doesNotThrow(function() {
assert.segments(root, [{name: 'child', children: [{name: 'grandchild'}]}])
}, 'should work with nested segments')

t.throws(function() {
assert.segments(root, [{name: 'doesNotExist'}])
}, {
message: /Missing child segment "doesNotExist"/
}, 'should throw for missing segments')

t.throws(function() {
assert.segments(root, [{name: 'sibling', children: [{name: 'foobar'}]}])
}, {
message: /Missing child segment "foobar"/
}, 'should throw for missing child segments')

var spec = [{name: 'child'}]
var curr = spec[0]
var seg = child
for (var i = 0; i < 25000; ++i) {
curr.children = [{name: 'segment ' + i}]
curr = curr.children[0]
seg = seg.add('segment ' + i)
}

t.doesNotThrow(function() {
assert.segments(root, spec)
}, 'should not die on deep segment trees')

t.end()
})

tap.test('assert.exactSegments', function(t) {
var tx = helper.runInTransaction(function(tx) { return tx })
var root = tx.trace.root
var child = root.add('child')
root.add('sibling')
child.add('grandchild')

t.doesNotThrow(function() {
assert.exactSegments(root, [{
name: 'child',
children: [{name: 'grandchild'}]
}, {
name: 'sibling'
}])
}, 'should not throw for exact match')

t.throws(function() {
assert.exactSegments(root, [{
name: 'sibling'
}, {
name: 'child',
children: [{name: 'grandchild'}]
}])
}, {
message: /Expected segment "sibling" as child \d of "\w+", found "child" instead/
}, 'should care about ordering of children')

t.throws(function() {
assert.exactSegments(root, [{
name: 'child',
children: [{name: 'grandchild'}]
}, {
name: 'sibling'
}, {
name: 'foobar'
}])
}, {
message: /Expected segment "foobar" as child \d of "\w+", found no child/
}, 'should care about missing children')

t.throws(function() {
assert.exactSegments(root, [{
name: 'child',
children: [{name: 'grandchild'}]
}])
}, {
message: /Expected 1 children for segment "\w+", found 2 instead/
}, 'should care extra children')

t.end()
})

0 comments on commit 9af5df2

Please sign in to comment.