1.0.1 • Published 6 years ago
cloud-function-test v1.0.1
Cloud Function Test
Test your functions before deploying to cloud. Write code locally, then test it.
Note that before deploying function to Google Cloud you have to first enable billing for your project, but using Cloud Functions is free up to a free tier quota - Google Cloud Functions Pricing
Installation
Install tester using npm
npm install cloud-function-test
Usage
- Import TestFunction class
var test = require('cloud-function-test').TestFunction;
- Initialize server
test.start(portNumber);
- Create your own function that you want to test locally.
It has to have
req
andres
parameters which will be passed to your function from the Google's server. They stand for Request and Response objects of Express framework.
test.fn = (req, res) => {
res.send({
status: 'OK',
message: 'Hello from function'
});
};
Run your script. Function will be available to you on
http://localhost:3003
or thehttp://localhost:portNumber
you set it to be.Close the server
test.close();
Example test case with mocha, chai an supertest
// recaptcha test
const request = require('supertest');
const chai = require('chai');
const assert = chai.assert;
var test = require('cloud-function-test').TestFunction;
var { reCAPTCHA } = require('../recaptcha-function'); // function to test
describe('reCAPTCHA', () => {
before(done => {
test.start(4000);
app = test.server;
test.fn = reCAPTCHA
done();
});
after(done => {
test.close();
done();
});
it('check if user is not a robot', () => {
request(app)
.get('/')
.send({
env: 'development'
})
.expect(res => {
assert.isTrue(res.body.success);
})
.end((err, res) => {
if (err) return done(err);
done();
});
});
});