A simple testing framework for Google Apps Script that supports both Node.js and GAS environments. Inspired by UnitTestingApp.
- Create test files in the
test/helpersdirectory - Create
test-runner.jsin thetestdirectory - Import your test files in
test-runner.js - Queue your tests in the TestRunner's start method
Example test file:
const MyClassTest = (() => {
function queue() {
// Specify if tests should run in GAS environment or Node.js
runInGas(true);
// Add a header for test organization
printHeader('path/to/tested/file.js');
describe('MyFunction', () => {
it('does something specific', () => {
const result = MyClass.doSomething();
expect(result).toBe(true);
});
});
}
return { queue };
})();
// Handle Node.js environment
if (typeof module !== 'undefined') {
MyClass = require('./path/to/tested/file.js');
module.exports = MyClassTest;
}Example test runner:
// Import dependencies if running in Node.js
if (typeof require !== "undefined") {
Tester = require('../lib/gaspec/tester.js');
MyClassTest = require('./path/to/myclass.test.js');
MyFunctionTest = require('./path/to/myfunction.test.js');
}
const TestRunner = (() => {
return {
start() {
// Create tester instance and setup environment
const tester = Tester.setup();
// Queue tests
MyClassTest.queue();
MyFunctionTest.queue();
// Run tests
tester.run();
}
}
})();
// Export for Node.js
if (typeof module !== "undefined") TestRunner.start();The framework provides the following matchers:
toBe(value)- Strict equality (===)toEqual(value)- Loose equality (==)toEqualObject(obj)- Deep object comparisontoContain(value)- Check if array/string contains valuetoBeTruthy()- Check if value is truthytoBeFalsy()- Check if value is falsytoBeNull()- Check if value is nulltoBeUndefined()- Check if value is undefinedtoBeGreaterThan(value)- Numeric comparisontoBeLessThan(value)- Numeric comparisontoThrowError([message])- Check if function throws errortoRespondTo(methodName)- Check if object has methodtoBeInstanceOf(className)- Check instance type
All matchers can be negated using .not:
expect(value).not.toBe(false);Use these functions to structure your tests:
describe(description, fn)- Group related testscontext(description, fn)- Alias for describe, for readabilityit(description, fn)- Individual test caseprintHeader(message)- Log section headersrunInGas(boolean)- Specify test environment
Control which environment your tests run in:
// Run the following tests if the environment is Google Apps Script
runInGas(true);
// Run the following tests if the environment is Node.js
runInGas(false);describe('Feature Group', () => {
context('Specific Feature', () => {
it('handles normal case', () => {
// test code
});
it('handles edge case', () => {
// test code
});
});
});Tests will display with checkmarks (✔️) for passing tests and crosses (❌) for failing tests, including error messages for failures.
- Add a new script file in the Google Apps Script editor
- Add a function that calls
TestRunner.start() - Run the function in the Apps Script web editor
- Run
node path/to/test-runner.jsin the terminal