Skip to content

Commit 179bfa8

Browse files
committed
test(instrumentation): add test for koa
1 parent a337cd6 commit 179bfa8

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

scripts/test-instrumentations

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ fi
1010

1111
cd ./test/instrumentations
1212
make test TARGET_VERSIONS=$INSTR_TARGET_VERSIONS \
13+
TARGET_LIBRARIES=$INSTR_TARGET_LIBRARIES \
1314
NODE_INDEX=$NODE_INDEX NODE_TOTAL=$NODE_TOTAL

test/instrumentations/koa/koa.mk

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
target := koa
2+
3+
all = 1.0 1.2 1.4 2.0 2.2
4+
some = 1.4 2.2
5+
one = 1.4
6+
7+
versions := $(call configure_targets,$(one),$(some),$(all))
8+
9+
targets := $(targets) $(target)
10+
11+
# set up as target specific variables to avoid collision in other
12+
# makefiles
13+
test_suite_$(target) : target := $(target)
14+
test_suite_$(target) : versions := $(versions)
15+
test_suite_$(target) : cur_path := $(abspath $(lastword $(MAKEFILE_LIST)))
16+
test_suite_$(target) : cur_dir := $(dir $(cur_path))
17+
test_suite_$(target) : cur_base := $(notdir $(cur_path))
18+
test_suite_$(target) : db_url := pg://localhost/postgres
19+
test_suite_$(target) : errors := 0
20+
21+
# normally all tasks should be phony
22+
.PHONY : before_$(target) \
23+
$(versions:%=before_version_$(target)_%) \
24+
$(versions:%=test_$(target)_%) \
25+
$(versions:%=after_version_$(target)_%) \
26+
after_$(target) \
27+
test_suite_$(target)
28+
29+
# 'this should run before the test
30+
before_$(target) : before
31+
@echo '*---------------*'
32+
@echo '| koa |'
33+
@echo '*---------------*'
34+
35+
# this should run after `before` for each of the version targets
36+
# before the test
37+
$(versions:%=before_version_$(target)_%) : before_$(target)
38+
@npm i koa@$(subst before_version_$(target)_,,$@)
39+
40+
# run the test for each of the versions
41+
$(versions:%=test_$(target)_%) : test_% : before_version_%
42+
$(MOCHA) $(addprefix $(cur_dir), *.spec.js) || exit 0;
43+
44+
# this should run before `after` for each of the version targets
45+
# after the test suite
46+
$(versions:%=after_version_$(target)_%) : $(versions:%=test_$(target)_%)
47+
48+
# this should run after the test
49+
after_$(target) : $(versions:%=after_version_$(target)_%)
50+
51+
## this is the whole test suite
52+
test_suite_$(target) : after_$(target)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict'
2+
3+
require('../test-setup.spec')
4+
5+
var sinon = require('sinon')
6+
var http = require('http')
7+
var expect = require('chai').expect
8+
var Shimmer = require('../../../lib/utils/shimmer')
9+
var Module = require('module')
10+
11+
describe('The koa wrapper module', function () {
12+
var sandbox = sinon.sandbox.create()
13+
var fakeAgent = {
14+
tracer: {
15+
collector: {
16+
userSentError: sandbox.spy()
17+
}
18+
}
19+
}
20+
var koa
21+
beforeEach(function () {
22+
sandbox.reset()
23+
require.cache = {}
24+
Shimmer.unwrap(Module, '_load')
25+
require('../../../lib/instrumentations').create({
26+
instrumentations: { redis: './trace-instrumentation-koa' },
27+
agent: fakeAgent
28+
})
29+
koa = require('koa')
30+
})
31+
it('reports unhandled errors', function () {
32+
var app = koa()
33+
app.use(function * (next) {
34+
throw new Error('But you shouldn\'t worry about it.')
35+
// yield next
36+
})
37+
app.listen(3000)
38+
http.get('http://localhost:3000', function () {
39+
expect(fakeAgent.tracer.collector.userSentError).to.have.been.calledOnce
40+
})
41+
})
42+
})

0 commit comments

Comments
 (0)