1.0.1 • Published 6 years ago

cloud-function-test v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Cloud Function Test Travis CI

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

  1. Import TestFunction class
var test = require('cloud-function-test').TestFunction;
  1. Initialize server
test.start(portNumber);
  1. Create your own function that you want to test locally. It has to have req and res 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'
  });
};
  1. Run your script. Function will be available to you on http://localhost:3003 or the http://localhost:portNumber you set it to be.

  2. 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();
    });
  });
});