Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wrapper to call serverless invokes with mock data, with execution wrapped with promises and tested with mocha
// wire up exec for serverless invokes with mock test data event inputs
require('dotenv').load();
const exec = require('child_process').exec
, assert = require('assert')
const stream = require('stream');
const expect = require('chai').expect;
describe('Run serverless invoke tests', function(){
this.timeout(10000);
//pass in opts to view stdout / stderr
it('Test dynamo record update event callback from mock json', function(done) {
execp('sls invoke local --function myFunc --path test/data/dynamo_update_event.json').then( (err, stdout, stderr) => {
expect(stderr).to.not.exist;
done()
})
})
})
/**
* Promisified child_process.exec
*
* @param cmd
* @param opts See child_process.exec node docs
* @param {stream.Writable} opts.stdout If defined, child process stdout will be piped to it.
* @param {stream.Writable} opts.stderr If defined, child process stderr will be piped to it.
*
* @returns {Promise<{ stdout: string, stderr: stderr }>}
*/
function execp(cmd, opts) {
opts || (opts = {});
return new Promise((resolve, reject) => {
const child = exec(cmd, opts,
(err, stdout, stderr) => err ? reject(err) : resolve({
stdout: stdout,
stderr: stderr
}));
if (opts.stdout) {
child.stdout.pipe(opts.stdout);
}
if (opts.stderr) {
child.stderr.pipe(opts.stderr);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment