RAS Playground 日本語 GitHub

Run, test and debug

Testing

Tests are written in *.test.ras files under test/, in the same style as vitest (spec §14.12). The test files and helpers (the .ras files in test/) are not included in the workbook. ras test does not start Excel; it runs on the Excel stand-in. The functions and matchers you can use are in the test API.

// test/report.test.ras
import { rebuildReport } from "src/report";

describe("rebuildReport", () => {
    beforeEach(() => useWorkbook("test/fixtures/sales.xlsx"));

    test("writes twice the quantity in column D", () => {
        rebuildReport();
        expect(Sheet1.cells(2, 4).value).toBe(6);
        expect(Sheet1.range("D2:D4").value).toEqual([[6], [10], [4]]);
    });

    test("does nothing when you choose No at the confirmation", () => {
        const box = mockMsgBox(() => vbNo);
        expect(msgBox("Continue?", vbYesNo)).toBe(vbNo);
        expect(box.calls.length).toBe(1);
    });
});
// src/report.ras
export function rebuildReport() {
    for (let r = 2; r <= 4; r++) {
        const qty = Sheet1.cells(r, 2).value;
        if (isError(qty)) continue;
        Sheet1.cells(r, 4).value = qty as int * 2;
    }
}

ras test

ras test                          # run all tests
ras test test/report.test.ras     # specify a file (or folder)
ras test -t "rebuildReport"       # filter by a string contained in the name (describe > test)
ras test --list                   # list without running
ras test --reporter json          # print results as JSON Lines (junit for JUnit XML)
ras test --watch                  # watch for changes and re-run the affected test files
ras test --ci                     # make test.only and dialogs that were not replaced into errors
ras test --inspect-brk            # stop each test file at its first line until a debugger attaches (no timeout)
ras test -u                       # record the snapshots (toMatchSnapshot) again
ras test --excel                  # run in the real Excel (Windows; slow)

Running in CI

ras test runs on the Excel stand-in, so it runs as it is in a Linux CI (it needs Node.js). --reporter junit writes the results as JUnit XML.

# .github/workflows/ras.yml
name: RAS
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-node@v4
        with: { node-version: 22 }
      - name: Install ras
        env: { GH_TOKEN: "${{ github.token }}" }
        run: |
          gh release download --repo cool0707/ras --pattern 'ras-*-linux-x64.zip' --dir "$RUNNER_TEMP"
          unzip -q "$RUNNER_TEMP"/ras-*-linux-x64.zip -d "$RUNNER_TEMP/ras"
          echo "$RUNNER_TEMP/ras" >> "$GITHUB_PATH"
      - run: ras check --target stand-in
      - run: ras test --ci --reporter junit > test-results.xml

To check with the real Excel, make a Windows machine with Excel installed a self-hosted runner. Excel's COM cannot be used from a runner running as a service, so run the runner in the interactive session of a logged-on user (with run.cmd), and turn on "Trust access to the VBA project object model" in Excel's Trust Center > Macro Settings.

  excel:
    runs-on: [self-hosted, windows, excel]
    needs: test
    steps:
      - uses: actions/checkout@v5
      - run: ras excel compile          # compile the generated VBA in Excel (compile errors are located in the .ras)
      - run: ras test --excel --ci --reporter junit > excel-results.xml
      - uses: actions/upload-artifact@v4
        if: failure()
        with: { name: workbooks, path: build/test/workbooks }

Running in VS Code

In the VS Code extension, the tests are shown in the Test Explorer, and ▷ runs all the tests, a file, a describe or a single test (▷ is also shown in the gutter next to each describe / test of a test file). It runs ras test --reporter json and shows the result of each test. A failed test shows its message on the line of the expect, and the difference between the expected and the actual value can be opened. The [Show the Failed Test's Workbook in Sheet Preview] button of the message (also in the context menu of the test in the Test Explorer) opens the workbook as it was when the test failed in the sheet preview, with the cells changed since the test started (after beforeAll) marked. "Debug Test" stops at breakpoints in the test files and in src/, where you can look at variables and step (VS Code's debugger attaches to ras test --inspect-brk).

Generating tests

In VS Code and the playground, "Generate test" above an exported function in src/ adds, to test/<module>.test.ras, a skeleton test that calls that function and checks its return value. The arguments and expected value are placeholders that fit the types, so rewrite them to the values you want to check.