how to unit test google apps scripts?
I'm trying to get set up with unit tests for google app scripts, and I found two projects:
https://code.google.com/p/gas-unit/ https://code.google.com/p/gasunit/
So I'm confused which to use :-)
I just had a go with the unhyphenated gasunit, which seems to expect that the script is embedded in a spreadsheet, which I am a little unclear on how to do ... and the scripts I want to test are web based scripts rather than spreadsheet ones
I had more luck testing the hyphenated gas-unit, which managed to send me both an email output of the test and generate a results page in my google site:
https://sites.google.com/site/testappscript2/TestResults
so I'm going to with gas-unit for the moment, but I'd really like to see some official testing framework incorporated by Google. In particular I'd like to find some way to get these scripts to be run with some frequency to send me the results. Also I'd love to get some BDD going; see my other posts:
How to get Cucumber/Capybara/Mechanize to work against external non-rails site how to use capybara has_text
Come on Google, you famously have "Testing Rocks, Debugging Sucks" in all your bathrooms? How about better testing support for Google Apps Scripts?
Solution 1:
You can try out QUnit for Google Apps Script. It is a patch for QUnit turned into a Google Apps Script library with API docs.
All you need is a script project that imports a QUnit library (for example the one with the project key MxL38OxqIK-B73jyDTvCe-OBao7QLBR4j
) and has a doGet function that configures QUnit using URL parameters and optionally also with your own settings, loads a function that runs your tests, and finally returns QUnit.getHtml(). Here is an example:
function doGet( e ) {
QUnit.urlParams( e.parameter );
QUnit.config({ title: "Unit tests for my project" });
QUnit.load( myTests );
return QUnit.getHtml();
};
// Imports the following functions:
// ok, equal, notEqual, deepEqual, notDeepEqual, strictEqual,
// notStrictEqual, throws, module, test, asyncTest, expect
QUnit.helpers(this);
function myTests() {
module("dummy module");
test("dummy test", 1, function() {
ok(true);
});
}
Then authorize the script, save a version of it, publish the script project ("Deploy as web app") and go to the test URL ("latest code") with your browser. Your tests will be run and results will be displayed via HtmlService. You can single-click on them to see their assertions, but as of writing this, you will probably not be able to do so in Firefox 20 and 21 due to Caja issue 1688.
Solution 2:
I just wrote another testing framework named GasT for my google spreadsheet add-on development & testing.
GasT is a TAP-compliant testing framework for Google Apps Script. It provides a simple way to verify that the GAS programs you write behave as expected. https://github.com/huan/gast
My goal is to get a simple tap tool like tape(for javascript) or bats(for bash). the test suite format is quite clear:
var gastLibUrl = 'https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js'
eval(UrlFetchApp.fetch(gastLibUrl).getContentText())
var test = GasTap.setPrintDriver('Logger')
function gast() {
test('do calculation right', function (t) {
var i = 3 + 4
t.equal(i, 7, 'I can calc 3 + 4 = 7')
})
test('Spreadsheet exist', function (t) {
var ss = SpreadsheetApp.openById('1TBJpvlW3WWney4rk1yW5N9bAP8dOMkWxI97dOtco-fc')
t.ok(ss, 'I can open spreadsheet')
})
test.finish()
}
Hope someone will like it. :)
there's a online version, you can go to have a look on it here: https://docs.google.com/spreadsheets/d/19M2DY3hunU6tDQFX5buJmZ_f3E8VFmlqAtodyC-J8Ag/edit#gid=0&vpid=A1
Solution 3:
The clasp
tool provides the ability to develop and deploy Apps Script projects locally from the command-line.
From the clasp
repo:
npm install -g @google/clasp
- enable Apps Script API: https://script.google.com/home/usersettings
- Develop locally and use the
clasp
tool to deploy.
Edit the node-google-apps-script
project has been deprecated in favor of clasp
There is the node-google-apps-script package to allow using standard JavaScript packages and automated testing tooling.
-
npm install -g node-google-apps-script
. - Go through the authorization steps to provide client secrets to allow uploading and importing Apps Script projects.
- Use
gulp
orgrunt
or whatever you use for test running normal JavaScript projects.
There is an official Google sample available that uses this workflow.
See Google Apps Developer Blog post announcement for more details.
Once the files are downloaded, convert them to TypeScript by renaming them to end with .ts
instead of .js
. Once they are TypeScript, ava
can be used to test them. Converting them to TypeScript also lets one use ES6 language features.
Solution 4:
I created gas-unit (https://code.google.com/p/gas-unit/) and have spent a bit of time over the last few days tidying up the examples and adding a HTML test runner.
I have been using it myself for some spreadsheet manipulation I've been doing with reasonable success. I've also been using Jasmine for non-GAS client side js work and have really enjoyed that. I miss the ability in gas-unit to easily create spys and I favour the BDD style of specification writing.
gas-unit has been a great learning exercise for me and it does work although there may be undiscovered issues with scope and closure - this is my first significant js exercise outside of DOM manipulation.
I think the future for testing in GAS has to be with a port of QUnit (as Adam suggests) or Jasmine. I had a quick look at what it would take to port Jasmine but as yet have not been able to find the time to tackle it.