Testing library for Google Apps Script
Clone or download
Latest commit 21a2919 Dec 15, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Apply javascript standard style (#15) Sep 19, 2018
.clasp.json Apply clasp (#14) Sep 18, 2018
.eslintrc.json Apply javascript standard style (#15) Sep 19, 2018
.gitignore Apply clasp (#14) Sep 18, 2018
README.md Update README.md Dec 15, 2018
package-lock.json Apply javascript standard style (#15) Sep 19, 2018
package.json Apply javascript standard style (#15) Sep 19, 2018

README.md

JavaScript Style Guide

GASUnit

Testing library for Google Apps Script. Result will be logged to Logger, or posted to Slack. You can use Exports style to write tests (for now).

Usage

Add library

project key: MSnMmw8hLWgjUG6uKSTQBEzVZgzu5bsVr

Write tests

Exports style

Exports style is inspired by Mocha.

Use Logger:

var exports = GASUnit.exports
var assert = GASUnit.assert

function test_array () {
  exports({
    'Array': {
      '#indexOf()': {
        'should return -1 when not present': function () {
          assert([1, 2, 3].indexOf(4) === -1)
        },
        'should return the index when present': function () {
          assert([1, 2, 3].indexOf(3) === 3)
        }
      }
    }
  })
}

Use Slack:

var WEBHOOK_URL = 'https://...'
var exports = GASUnit.slack(WEBHOOK_URL).exports
var assert = GASUnit.assert

function test_array () {
  exports({
    'Array': {
      '#indexOf()': {
        'should return -1 when not present': function () {
          assert([1, 2, 3].indexOf(4) === -1)
        },
        'should return the index when present': function () {
          assert([1, 2, 3].indexOf(3) === 3)
        }
      }
    }
  })
}

If you're publishing source code, should not write webhook url as a literal. You can use properties as environment variables.

var WEBHOOK_URL = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL')
var exports = GASUnit.slack(WEBHOOK_URL).exports
var assert = GASUnit.assert

function test_array () {
  exports({
    'Array': {
      '#indexOf()': {
        'should return -1 when not present': function () {
          assert([1, 2, 3].indexOf(4) === -1)
        },
        'should return the index when present': function () {
          assert([1, 2, 3].indexOf(3) === 3)
        }
      }
    }
  })
}

Or you can...

var WEBHOOK_URL = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL')
var exports = WEBHOOK_URL ? GASUnit.slack(WEBHOOK_URL).exports : GASUnit.exports
var assert = GASUnit.assert

function test_array () {
  exports({
    'Array': {
      '#indexOf()': {
        'should return -1 when not present': function () {
          assert([1, 2, 3].indexOf(4) === -1)
        },
        'should return the index when present': function () {
          assert([1, 2, 3].indexOf(3) === 3)
        }
      }
    }
  })
}

Assertion

GASUnit provides minimum assert function which verify whether value is truthy. You can add any assertion library and use it.

Development

# install dependencies
$ npm install

# lint code by ESLint
$ npm run lint

# login to Google Drive
$ npm run login

# logout from Google Drive
$ npm run logout

# pull code from Google Drive
$ npm run pull

# push code to Google Drive
$ npm run push

# open project page on Google Drive
$ npm run open

Example

see gasunit/example

Article