Skip to content

Commit 9b7d4ec

Browse files
committed
fix(startup): dont start if NODE_ENV=test
1 parent c2cb9e7 commit 9b7d4ec

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

lib/index.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,30 @@ var debug = require('debug')('risingstack/trace')
66

77
var ConfigReader = require('./utils/configReader')
88

9+
var traceNoop = {
10+
report: function () {},
11+
reportError: function () {},
12+
getTransactionId: function () {}
13+
}
14+
915
function Trace () {
16+
if (process.env.NODE_ENV === 'test') {
17+
return traceNoop
18+
}
19+
1020
this.configReader = ConfigReader.create()
21+
1122
try {
1223
this.config = this.configReader.getConfig()
1324
} catch (ex) {
1425
console.error(format('%s trace: error: %s', new Date(), ex.message))
15-
return
26+
return traceNoop
1627
}
1728

1829
// warn the user if the newrelic agent is present as well
1930
if (require.cache.__NR_cache) {
2031
console.error('Make sure to require Trace before New Relic, otherwise Trace won\'t start')
21-
return
32+
return traceNoop
2233
}
2334

2435
this.agent = Agent.create({

lib/index.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
var expect = require('chai').expect
2+
var freshy = require('freshy')
23

34
var Agent = require('./agent')
45
var Instrumentation = require('./instrumentations')
56
var ConfigReader = require('./utils/configReader')
67

78
describe('The trace module', function () {
9+
beforeEach(function () {
10+
freshy.unload('./')
11+
})
12+
13+
it('doesn\'t start in NODE_ENV=test', function () {
14+
this.sandbox.stub(process, 'env', {
15+
NODE_ENV: 'test'
16+
})
17+
18+
var configReaderStub = this.sandbox.stub(ConfigReader, 'create')
19+
20+
var trace = require('./')
21+
expect(trace.report).to.be.a('function')
22+
expect(trace.reportError).to.be.a('function')
23+
expect(trace.getTransactionId).to.be.a('function')
24+
expect(configReaderStub).to.have.not.been.called
25+
})
26+
827
it('initializes', function () {
928
var fakeAgent = {
1029
name: 'agent'

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"scripts": {
2828
"preinstall": "npm install node-gyp-install && node-gyp-install",
2929
"lint": "eslint lib example e2e",
30-
"test": "NODE_ENV=test npm run test-unit && npm run test-e2e",
31-
"test-unit": "NODE_ENV=test mocha $(find ./lib -name \"*.spec.js\" -not -path \"./node_modules/*\")",
32-
"test-cls": "NODE_ENV=test mocha $(find ./lib -name \"*.spec.e2e.js\" -not -path \"./node_modules/*\")",
33-
"test-e2e": "NODE_ENV=test node ./e2e | tap-spec",
30+
"test": "npm run test-unit && npm run test-e2e",
31+
"test-unit": "mocha $(find ./lib -name \"*.spec.js\" -not -path \"./node_modules/*\")",
32+
"test-cls": "mocha $(find ./lib -name \"*.spec.e2e.js\" -not -path \"./node_modules/*\")",
33+
"test-e2e": "node ./e2e | tap-spec",
3434
"cover": "istanbul cover _mocha $(find ./lib -name \"*.spec.js\" -not -path \"./node_modules/*\"); open coverage/lcov-report/index.html",
3535
"changelog": "conventional-changelog -p angular -o CHANGELOG.md -r 500",
3636
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
@@ -70,6 +70,7 @@
7070
"eslint-config-standard": "5.1.0",
7171
"eslint-plugin-standard": "1.3.2",
7272
"express": "4.13.4",
73+
"fresh": "0.3.0",
7374
"freshy": "1.0.2",
7475
"ioredis": "1.15.1",
7576
"istanbul": "0.4.2",

0 commit comments

Comments
 (0)