Testing
You can write unit tests for your JSA extension logic using standard JavaScript testing tools. We recommend QUnit for its simplicity — no build step required, runs in the browser, and the learning curve is gentle.
Why test?
Section titled “Why test?”If you’re coming from Ruby, you know the value of tests. The same applies here: tests catch regressions, document behavior, and give you confidence when refactoring. Even a few tests covering your core logic save time in the long run.
What to test
Section titled “What to test”Focus on testing your extension’s business logic — the functions that process data, make decisions, or transform model information. You typically won’t test the SketchUp API calls themselves (that’s our job), but you can test the logic around them.
For example, if your extension calculates material coverage or generates a report from entity data, those calculations are great candidates for tests.
A minimal QUnit example
Section titled “A minimal QUnit example”Create a test HTML file that loads QUnit and your extension’s logic:
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>My Extension Tests</title> <link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-2.20.1.css"></head><body> <div id="qunit"></div> <script src="https://code.jquery.com/qunit/qunit-2.20.1.js"></script>
<script type="module"> // Import your extension's logic // import { calculateArea, formatReport } from './src/helpers.js';
// Example: testing a helper function function calculateTotalArea(faces) { return faces.reduce((sum, face) => sum + face.area, 0); }
QUnit.module('calculateTotalArea', function() { QUnit.test('sums face areas correctly', function(assert) { const faces = [ { area: 100 }, { area: 250 }, { area: 50 }, ]; assert.equal(calculateTotalArea(faces), 400); });
QUnit.test('returns 0 for empty array', function(assert) { assert.equal(calculateTotalArea([]), 0); }); }); </script></body></html>Open this file in a browser to run the tests. No server needed — just double-click it.
- Separate logic from API calls. Put your calculation and transformation logic in separate functions (or a separate file) that you can test independently of
SketchUpApi. - Mock sparingly. If a function takes entity data as input and returns a result, you can pass in plain objects that look like entities without needing a real SketchUp connection.
- Start small. Even 3-4 tests covering your core helper functions add real value.
Resources
Section titled “Resources”- QUnit documentation
- QUnit cookbook — quick-start guide