Skip to content

Commit 99cf3ef

Browse files
authored
Refactored tests (GoogleCloudPlatform#159)
* Refactor tests. * Tweak build. * Tweak build. * More tests. * Tweak build. * Tweak build. * Fix build. * Fix build. * Speed up build. * Fix build. * Remove extra dep. * Investigate why 0.12 fails. * Scripts. * More tests. * Upgrades * Upgrades * Update readme
1 parent 72cb8cf commit 99cf3ef

File tree

155 files changed

+2331
-1655
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+2331
-1655
lines changed

.jshintignore

-4
This file was deleted.

.jshintrc

-15
This file was deleted.

.travis.yml

+21-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
sudo: false
1515
language: node_js
1616
node_js:
17-
- "5"
17+
- "6"
18+
- "4"
1819
- "0.12"
1920

2021
cache:
@@ -54,11 +55,24 @@ cache:
5455
- computeengine/node_modules/
5556
- datastore/node_modules/
5657
- debugger/node_modules/
58+
- functions/background/node_modules/
59+
- functions/datastore/node_modules/
60+
- functions/errorreporting/node_modules/
61+
- functions/gcs/node_modules/
62+
- functions/helloworld/node_modules/
63+
- functions/http/node_modules/
64+
- functions/log/node_modules/
65+
- functions/ocr/node_modules/
66+
- functions/pubsub/node_modules/
67+
- functions/sendgrid/node_modules/
68+
- functions/slack/node_modules/
5769
- functions/uuid/node_modules/
70+
- language/node_modules/
5871
- logging/node_modules/
5972
- monitoring/node_modules/
6073
- prediction/node_modules/
6174
- pubsub/node_modules/
75+
- speech/node_modules/
6276
- storage/node_modules/
6377
- trace/node_modules/
6478
- vision/node_modules/
@@ -69,13 +83,15 @@ services:
6983

7084
env:
7185
global:
72-
- GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/encrypted/nodejs-docs-samples.json
86+
- GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/nodejs-docs-samples.json
7387
- TEST_BUCKET_NAME=nodejs-docs-samples
7488
- GCLOUD_PROJECT=nodejs-docs-samples
7589

7690
before_install:
77-
- openssl aes-256-cbc -K $encrypted_fda0b707c7d5_key -iv $encrypted_fda0b707c7d5_iv -in test/encrypted/nodejs-docs-samples.json.enc -out test/encrypted/nodejs-docs-samples.json -d
91+
- openssl aes-256-cbc -K $encrypted_fda0b707c7d5_key -iv $encrypted_fda0b707c7d5_iv -in test/nodejs-docs-samples.json.enc -out test/nodejs-docs-samples.json -d
7892
- npm set progress=false
7993

80-
after_success:
81-
- npm run report
94+
before_script:
95+
- node scripts/install
96+
97+
script: npm run all-cover && bash <(curl -s https://codecov.io/bash)

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,26 @@ Cloud Storage bucket:
166166

167167
memcached
168168

169-
1. In another terminal, run the tests from the root of the project:
169+
1. In another terminal, run the unit tests from the root of the project:
170170

171171
npm test
172172

173+
or with coverage:
174+
175+
npm run coverage
176+
177+
1. Then run the system tests from the root of the project:
178+
179+
npm run system-test
180+
181+
or with coverage:
182+
183+
npm run system-cover
184+
185+
1. Or run all the tests at once with coverage:
186+
187+
npm run all-cover
188+
173189
## Client libraries
174190

175191
### <img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=36" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="left" height="36" width="36" style="margin-top: 9px;"/>Google Cloud Node.js client library

appengine/analytics/app.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
// [START app]
1616
'use strict';
1717

18+
// [START setup]
1819
var express = require('express');
1920
var request = require('request');
2021

2122
var app = express();
23+
app.enable('trust proxy');
24+
// [END setup]
2225

26+
// [START track]
2327
// The following environment variable is set by app.yaml when running on GAE,
2428
// but will need to be manually set when running locally. See README.md.
2529
var GA_TRACKING_ID = process.env.GA_TRACKING_ID;
@@ -43,15 +47,19 @@ function trackEvent (category, action, label, value, cb) {
4347
form: data
4448
},
4549
function (err, response) {
46-
if (err) { return cb(err); }
50+
if (err) {
51+
return cb(err);
52+
}
4753
if (response.statusCode !== 200) {
4854
return cb(new Error('Tracking failed'));
4955
}
5056
cb();
5157
}
5258
);
5359
}
60+
// [END track]
5461

62+
// [START endpoint]
5563
app.get('/', function (req, res, next) {
5664
trackEvent(
5765
'Example category',
@@ -62,14 +70,21 @@ app.get('/', function (req, res, next) {
6270
// This sample treats an event tracking error as a fatal error. Depending
6371
// on your application's needs, failing to track an event may not be
6472
// considered an error.
65-
if (err) { return next(err); }
73+
if (err) {
74+
return next(err);
75+
}
6676
res.status(200).send('Event tracked.');
6777
});
6878
});
79+
// [END endpoint]
6980

70-
// Start the server
71-
var server = app.listen(process.env.PORT || 8080, function () {
72-
console.log('App listening on port %s', server.address().port);
81+
// [START listen]
82+
var PORT = process.env.PORT || 8080;
83+
app.listen(PORT, function () {
84+
console.log('App listening on port %s', PORT);
7385
console.log('Press Ctrl+C to quit.');
7486
});
87+
// [END listen]
7588
// [END app]
89+
90+
module.exports = app;

appengine/analytics/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
},
1111
"scripts": {
1212
"start": "node app.js",
13-
"monitor": "nodemon app.js",
14-
"deploy": "gcloud app deploy"
13+
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
1514
},
1615
"dependencies": {
1716
"express": "^4.13.4",
1817
"request": "^2.69.0"
18+
},
19+
"devDependencies": {
20+
"mocha": "^2.5.3"
1921
}
2022
}

appengine/analytics/test/app.test.js

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var path = require('path');
18+
var proxyquire = require('proxyquire').noPreserveCache();
19+
var request = require('supertest');
20+
21+
var SAMPLE_PATH = path.join(__dirname, '../app.js');
22+
23+
function getSample () {
24+
var testApp = express();
25+
sinon.stub(testApp, 'listen').callsArg(1);
26+
var expressMock = sinon.stub().returns(testApp);
27+
var resultsMock = {
28+
statusCode: 200,
29+
foo: 'bar'
30+
};
31+
32+
var requestMock = {
33+
post: sinon.stub().callsArgWith(2, null, resultsMock)
34+
};
35+
36+
var app = proxyquire(SAMPLE_PATH, {
37+
request: requestMock,
38+
express: expressMock
39+
});
40+
return {
41+
app: app,
42+
mocks: {
43+
express: expressMock,
44+
results: resultsMock,
45+
request: requestMock
46+
}
47+
};
48+
}
49+
50+
describe('appengine/analytics/app.js', function () {
51+
var sample;
52+
53+
beforeEach(function () {
54+
sample = getSample();
55+
56+
assert(sample.mocks.express.calledOnce);
57+
assert(sample.app.listen.calledOnce);
58+
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
59+
});
60+
61+
it('should record a visit', function (done) {
62+
var expectedResult = 'Event tracked.';
63+
64+
request(sample.app)
65+
.get('/')
66+
.expect(200)
67+
.expect(function (response) {
68+
assert.equal(response.text, expectedResult);
69+
})
70+
.end(done);
71+
});
72+
73+
it('should handle request error', function (done) {
74+
var expectedResult = 'request_error';
75+
76+
sample.mocks.request.post.onFirstCall().callsArgWith(2, expectedResult);
77+
78+
request(sample.app)
79+
.get('/')
80+
.expect(500)
81+
.expect(function (response) {
82+
assert.equal(response.text, expectedResult + '\n');
83+
})
84+
.end(done);
85+
});
86+
87+
it('should handle track error', function (done) {
88+
sample.mocks.request.post.onFirstCall().callsArgWith(2, null, {
89+
statusCode: 400
90+
});
91+
92+
request(sample.app)
93+
.get('/')
94+
.expect(500)
95+
.expect(function (response) {
96+
assert.notEqual(response.text.indexOf('Error: Tracking failed'), -1);
97+
})
98+
.end(done);
99+
});
100+
});

appengine/bower/package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
"node": "~4.2"
1010
},
1111
"scripts": {
12-
"start": "node server.js",
1312
"postinstall": "bower install --config.interactive=false",
14-
"deploy": "gcloud app deploy"
13+
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
1514
},
1615
"dependencies": {
1716
"bower": "^1.7.7",
1817
"express": "^4.13.4",
1918
"jade": "^1.11.0"
19+
},
20+
"devDependencies": {
21+
"mocha": "^2.5.3"
2022
}
2123
}

appengine/bower/server.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,34 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14+
// [START app]
1415
'use strict';
1516

17+
// [START setup]
1618
var express = require('express');
1719
var path = require('path');
1820

1921
var app = express();
22+
app.enable('trust proxy');
23+
// [END setup]
2024

2125
// Setup view engine
2226
app.set('view engine', 'jade');
27+
app.set('views', path.join(__dirname, 'views'));
2328

24-
app.use(express.static(path.join(__dirname, '/public')));
29+
app.use(express.static(path.join(__dirname, 'public')));
2530

2631
app.get('/', function (req, res) {
2732
res.render('index');
2833
});
2934

30-
var server = app.listen(
31-
process.env.PORT || 8080,
32-
'0.0.0.0',
33-
function () {
34-
var address = server.address().address;
35-
var port = server.address().port;
36-
console.log('App listening at http://%s:%s', address, port);
37-
console.log('Press Ctrl+C to quit.');
38-
}
39-
);
35+
// [START listen]
36+
var PORT = process.env.PORT || 8080;
37+
app.listen(PORT, function () {
38+
console.log('App listening on port %s', PORT);
39+
console.log('Press Ctrl+C to quit.');
40+
});
41+
// [END listen]
42+
// [END app]
43+
44+
module.exports = app;

0 commit comments

Comments
 (0)