Skip to content

Commit

Permalink
chore(functions/log/helloworld): use declarative signature
Browse files Browse the repository at this point in the history
  • Loading branch information
kenneth-rosario committed Mar 14, 2023
1 parent 9d7a533 commit c7c3e85
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
8 changes: 5 additions & 3 deletions functions/log/helloWorld/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -15,9 +15,11 @@
'use strict';

// [START functions_log_helloworld]
exports.helloWorld = (req, res) => {
const functions = require('@google-cloud/functions-framework');

functions.http('helloWorld', (req, res) => {
console.log('I am a log entry!');
console.error('I am an error!');
res.end();
};
});
// [END functions_log_helloworld]
3 changes: 3 additions & 0 deletions functions/log/helloWorld/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"mocha": "^10.0.0",
"proxyquire": "^2.1.0",
"sinon": "^15.0.0"
},
"dependencies": {
"@google-cloud/functions-framework": "^3.1.3"
}
}
28 changes: 4 additions & 24 deletions functions/log/helloWorld/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 Google LLC
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,31 +14,11 @@

'use strict';

const proxyquire = require('proxyquire').noCallThru();
const sinon = require('sinon');
const assert = require('assert');
const {getFunction} = require('@google-cloud/functions-framework/testing');

const getSample = () => {
const results = [[{}], {}];
const stream = {
on: sinon.stub().returnsThis(),
};
stream.on.withArgs('end').yields();

const logging = {
getEntries: sinon.stub().returns(Promise.resolve(results)),
};

return {
program: proxyquire('../', {
'@google-cloud/logging': sinon.stub().returns(logging),
}),
mocks: {
logging: logging,
results: results,
},
};
};
require('..');

const stubConsole = function () {
sinon.stub(console, 'error');
Expand All @@ -58,7 +38,7 @@ describe('functions_log_helloworld', () => {
const expectedMsg = 'I am a log entry!';
const res = {end: sinon.stub()};

getSample().program.helloWorld({}, res);
getFunction('helloWorld')(null, res);

assert.strictEqual(console.log.callCount, 1);
assert.deepStrictEqual(console.log.firstCall.args, [expectedMsg]);
Expand Down

0 comments on commit c7c3e85

Please sign in to comment.