Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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