Skip to content
This repository has been archived by the owner on Apr 24, 2022. It is now read-only.

gasunit/GASUnit

Repository files navigation

clasp tested by ts-mocha JavaScript Style Guide

English - 日本語

GASUnit

banner

Testing library for Google Apps Script.
Result will be logged to Logger, or posted to Slack.

Usage

Add library

project key: MSnMmw8hLWgjUG6uKSTQBEzVZgzu5bsVr

Write tests

You can use Exports style to write tests (for now).

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)
        }
      }
    }
  })
}

Run test

Run test function on browser.

.doc/run_test.png

See result

Logger

Success:

.doc/logger_success.png

Fail:

.doc/logger_fail.png

Slack

Success:

.doc/slack_success.png

Fail:

.doc/slack_fail.png

Assertion

GASUnit provides minimum assert function which verify whether value is truthy.
You can use any assertion library (for Google Apps Script).
GASUnit also provides AssertGAS as official assertion library.

Badge

You can use the badge to show that your project is using GASUnit.

tested by GASUnit

Markdown:

[![tested by GASUnit](https://img.shields.io/badge/tested%20by-GASUnit-%234285F1)](https://github.com/gasunit/GASUnit)

HTML:

<a href="https://github.com/gasunit/GASUnit"><img src="https://img.shields.io/badge/tested%20by-GASUnit-%234285F1" alt="tested by GASUnit"></a>

Development

See package.json

Example

See gasunit/example

Article

Japanese