From c7c3e85c96c29b97aaab9f8fe70abd5be6d00ec7 Mon Sep 17 00:00:00 2001 From: Kenneth Rosario Acevedo Date: Tue, 14 Mar 2023 16:48:39 +0000 Subject: [PATCH] chore(functions/log/helloworld): use declarative signature --- functions/log/helloWorld/index.js | 8 +++--- functions/log/helloWorld/package.json | 3 +++ functions/log/helloWorld/test/index.test.js | 28 +++------------------ 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/functions/log/helloWorld/index.js b/functions/log/helloWorld/index.js index c8f91d936d..6ebb5c7393 100644 --- a/functions/log/helloWorld/index.js +++ b/functions/log/helloWorld/index.js @@ -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. @@ -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] diff --git a/functions/log/helloWorld/package.json b/functions/log/helloWorld/package.json index ba216ac47b..0d1953663a 100644 --- a/functions/log/helloWorld/package.json +++ b/functions/log/helloWorld/package.json @@ -18,5 +18,8 @@ "mocha": "^10.0.0", "proxyquire": "^2.1.0", "sinon": "^15.0.0" + }, + "dependencies": { + "@google-cloud/functions-framework": "^3.1.3" } } diff --git a/functions/log/helloWorld/test/index.test.js b/functions/log/helloWorld/test/index.test.js index a8a609f408..254a9c66e5 100644 --- a/functions/log/helloWorld/test/index.test.js +++ b/functions/log/helloWorld/test/index.test.js @@ -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. @@ -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'); @@ -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]);