Skip to content

Commit

Permalink
Refactored tests (#159)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jmdobry authored Aug 3, 2016
1 parent 72cb8cf commit 99cf3ef
Show file tree
Hide file tree
Showing 155 changed files with 2,331 additions and 1,655 deletions.
4 changes: 0 additions & 4 deletions .jshintignore

This file was deleted.

15 changes: 0 additions & 15 deletions .jshintrc

This file was deleted.

26 changes: 21 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
sudo: false
language: node_js
node_js:
- "5"
- "6"
- "4"
- "0.12"

cache:
Expand Down Expand Up @@ -54,11 +55,24 @@ cache:
- computeengine/node_modules/
- datastore/node_modules/
- debugger/node_modules/
- functions/background/node_modules/
- functions/datastore/node_modules/
- functions/errorreporting/node_modules/
- functions/gcs/node_modules/
- functions/helloworld/node_modules/
- functions/http/node_modules/
- functions/log/node_modules/
- functions/ocr/node_modules/
- functions/pubsub/node_modules/
- functions/sendgrid/node_modules/
- functions/slack/node_modules/
- functions/uuid/node_modules/
- language/node_modules/
- logging/node_modules/
- monitoring/node_modules/
- prediction/node_modules/
- pubsub/node_modules/
- speech/node_modules/
- storage/node_modules/
- trace/node_modules/
- vision/node_modules/
Expand All @@ -69,13 +83,15 @@ services:

env:
global:
- GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/encrypted/nodejs-docs-samples.json
- GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/nodejs-docs-samples.json
- TEST_BUCKET_NAME=nodejs-docs-samples
- GCLOUD_PROJECT=nodejs-docs-samples

before_install:
- 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
- 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
- npm set progress=false

after_success:
- npm run report
before_script:
- node scripts/install

script: npm run all-cover && bash <(curl -s https://codecov.io/bash)
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,26 @@ Cloud Storage bucket:

memcached

1. In another terminal, run the tests from the root of the project:
1. In another terminal, run the unit tests from the root of the project:

npm test

or with coverage:

npm run coverage

1. Then run the system tests from the root of the project:

npm run system-test

or with coverage:

npm run system-cover

1. Or run all the tests at once with coverage:

npm run all-cover

## Client libraries

### <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
Expand Down
25 changes: 20 additions & 5 deletions appengine/analytics/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
// [START app]
'use strict';

// [START setup]
var express = require('express');
var request = require('request');

var app = express();
app.enable('trust proxy');
// [END setup]

// [START track]
// The following environment variable is set by app.yaml when running on GAE,
// but will need to be manually set when running locally. See README.md.
var GA_TRACKING_ID = process.env.GA_TRACKING_ID;
Expand All @@ -43,15 +47,19 @@ function trackEvent (category, action, label, value, cb) {
form: data
},
function (err, response) {
if (err) { return cb(err); }
if (err) {
return cb(err);
}
if (response.statusCode !== 200) {
return cb(new Error('Tracking failed'));
}
cb();
}
);
}
// [END track]

// [START endpoint]
app.get('/', function (req, res, next) {
trackEvent(
'Example category',
Expand All @@ -62,14 +70,21 @@ app.get('/', function (req, res, next) {
// This sample treats an event tracking error as a fatal error. Depending
// on your application's needs, failing to track an event may not be
// considered an error.
if (err) { return next(err); }
if (err) {
return next(err);
}
res.status(200).send('Event tracked.');
});
});
// [END endpoint]

// Start the server
var server = app.listen(process.env.PORT || 8080, function () {
console.log('App listening on port %s', server.address().port);
// [START listen]
var PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
console.log('App listening on port %s', PORT);
console.log('Press Ctrl+C to quit.');
});
// [END listen]
// [END app]

module.exports = app;
6 changes: 4 additions & 2 deletions appengine/analytics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
},
"scripts": {
"start": "node app.js",
"monitor": "nodemon app.js",
"deploy": "gcloud app deploy"
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
},
"dependencies": {
"express": "^4.13.4",
"request": "^2.69.0"
},
"devDependencies": {
"mocha": "^2.5.3"
}
}
100 changes: 100 additions & 0 deletions appengine/analytics/test/app.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

var express = require('express');
var path = require('path');
var proxyquire = require('proxyquire').noPreserveCache();
var request = require('supertest');

var SAMPLE_PATH = path.join(__dirname, '../app.js');

function getSample () {
var testApp = express();
sinon.stub(testApp, 'listen').callsArg(1);
var expressMock = sinon.stub().returns(testApp);
var resultsMock = {
statusCode: 200,
foo: 'bar'
};

var requestMock = {
post: sinon.stub().callsArgWith(2, null, resultsMock)
};

var app = proxyquire(SAMPLE_PATH, {
request: requestMock,
express: expressMock
});
return {
app: app,
mocks: {
express: expressMock,
results: resultsMock,
request: requestMock
}
};
}

describe('appengine/analytics/app.js', function () {
var sample;

beforeEach(function () {
sample = getSample();

assert(sample.mocks.express.calledOnce);
assert(sample.app.listen.calledOnce);
assert.equal(sample.app.listen.firstCall.args[0], process.env.PORT || 8080);
});

it('should record a visit', function (done) {
var expectedResult = 'Event tracked.';

request(sample.app)
.get('/')
.expect(200)
.expect(function (response) {
assert.equal(response.text, expectedResult);
})
.end(done);
});

it('should handle request error', function (done) {
var expectedResult = 'request_error';

sample.mocks.request.post.onFirstCall().callsArgWith(2, expectedResult);

request(sample.app)
.get('/')
.expect(500)
.expect(function (response) {
assert.equal(response.text, expectedResult + '\n');
})
.end(done);
});

it('should handle track error', function (done) {
sample.mocks.request.post.onFirstCall().callsArgWith(2, null, {
statusCode: 400
});

request(sample.app)
.get('/')
.expect(500)
.expect(function (response) {
assert.notEqual(response.text.indexOf('Error: Tracking failed'), -1);
})
.end(done);
});
});
6 changes: 4 additions & 2 deletions appengine/bower/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"node": "~4.2"
},
"scripts": {
"start": "node server.js",
"postinstall": "bower install --config.interactive=false",
"deploy": "gcloud app deploy"
"test": "mocha -R spec -t 120000 --require intelli-espower-loader ../../test/_setup.js test/*.test.js"
},
"dependencies": {
"bower": "^1.7.7",
"express": "^4.13.4",
"jade": "^1.11.0"
},
"devDependencies": {
"mocha": "^2.5.3"
}
}
27 changes: 16 additions & 11 deletions appengine/bower/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,34 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// [START app]
'use strict';

// [START setup]
var express = require('express');
var path = require('path');

var app = express();
app.enable('trust proxy');
// [END setup]

// Setup view engine
app.set('view engine', 'jade');
app.set('views', path.join(__dirname, 'views'));

app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, 'public')));

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

var server = app.listen(
process.env.PORT || 8080,
'0.0.0.0',
function () {
var address = server.address().address;
var port = server.address().port;
console.log('App listening at http://%s:%s', address, port);
console.log('Press Ctrl+C to quit.');
}
);
// [START listen]
var PORT = process.env.PORT || 8080;
app.listen(PORT, function () {
console.log('App listening on port %s', PORT);
console.log('Press Ctrl+C to quit.');
});
// [END listen]
// [END app]

module.exports = app;
Loading

0 comments on commit 99cf3ef

Please sign in to comment.