RAS Playground 日本語 GitHub

RAS standard library

Test API

These can be used without import, only in test files (test/**/*.test.ras) and helper modules for tests. ras test runs them on the Excel stand-in without starting Excel (nothing is emitted to VBA). For how to write tests and the ras test options, see Testing.

// test/api.test.ras
describe("Sheet1", () => {
    beforeEach(() => {
        Sheet1.range("A1").value = 3;
    });

    test("can read a value", () => {
        expect(Sheet1.range("A1").value).toBe(3);
        expect([1, 2]).toEqual([1, 2]);
    });

    test("choose \"No\" in a confirmation", () => {
        const box = mockMsgBox(() => vbNo);
        expect(msgBox("Continue?", vbYesNo)).toBe(vbNo);
        expect(box.calls.length).toBe(1);
    });
});

Reference

Global describe

const describe: RasTest.DescribeFn

Groups tests. A test's name is the describe names and its own joined with >.

test.d.ras

Global test

const test: RasTest.TestFn

Declares a test. body is a lambda without arguments.

test.d.ras

Global it

const it: RasTest.TestFn

Same as test.

test.d.ras

Function beforeAll

function beforeAll(fn: () => void): void

Runs once before the first test of the describe (of the file at the top level).

test.d.ras

Function afterAll

function afterAll(fn: () => void): void

Runs once after the last test of the describe.

test.d.ras

Function beforeEach

function beforeEach(fn: () => void): void

Runs before each test in the describe.

test.d.ras

Function afterEach

function afterEach(fn: () => void): void

Runs after each test in the describe.

test.d.ras

Function expect

function expect<T>(actual: T): RasTest.Matchers<T>

test.d.ras

Function useWorkbook

function useWorkbook(path: string): Excel.Workbook

Replaces the project's workbook with the contents of the workbook at path (relative to the project root). The original file is not changed.

test.d.ras

Function tempDir

function tempDir(): string

An empty folder created for each test and deleted after the test ends.

test.d.ras

Function projectDir

function projectDir(): string

The absolute path of the project root (the folder of ras.toml). Find the files of the tests from it (with ras test --excel, thisWorkbook.path is the folder of the copy of the workbook).

test.d.ras

Function printed

function printed(): string[]

The lines debug.print printed during the test.

test.d.ras

Function simulate

function simulate(...events: string[]): void

Does what the user does and raises its events (the @on handlers run). Written as for ras run --event: open (Workbook.Open), activate:Sheet, select:Sheet!A1, enter:Sheet!A1=value (nothing after = clears), double-click:Sheet!A1, right-click:Sheet!A1, click:Sheet!name of a shape or control. The sheet is its tab name or code name. Several are done in order. An error of a handler fails the test. Not available with --excel.

test.d.ras

Function mockMsgBox

function mockMsgBox(handler?: (prompt: string, buttons: int, title: string) => int): RasTest.Mock

Calls handler(prompt, buttons, title) instead of msgBox and returns its return value. vbOK when omitted.

test.d.ras

Function mockInputBox

function mockInputBox(handler?: (prompt: string, title: string, defaultValue: any) => any): RasTest.Mock

Calls handler(prompt, title, defaultValue) instead of inputBox and application.inputBox.

test.d.ras

Function mockGetOpenFilename

function mockGetOpenFilename(handler?: () => any): RasTest.Mock

Calls handler() instead of getOpenFilename (return false for cancel).

test.d.ras

Function mockGetSaveAsFilename

function mockGetSaveAsFilename(handler?: () => any): RasTest.Mock

Calls handler() instead of getSaveAsFilename (return false for cancel).

test.d.ras

Function mockRun

function mockRun(macroName: string, handler?: any): RasTest.Mock

Calls handler(...args) instead of application.run(macroName, ...) (a call to hand-written VBA).

test.d.ras

Function mockCreateObject

function mockCreateObject(progId: string, factory: any): RasTest.Mock

createObject(progId) / new use the object factory() returns.

test.d.ras

Interface RasTest.Matchers

interface Matchers<T>

The assertions of expect(actual). A failure stops the test (it is not caught by try / catch).

#
readonly not: Matchers<T>

Negates the assertion.

#
toBe(expected: T): void

The same comparison as RAS's == (objects by identity).

#
toEqual(expected: any): void

Compares arrays, structs, tuples and Maps element by element.

#
toBeCloseTo(expected: double, digits?: int): void

The absolute difference is less than 10 ^ -digits / 2 (digits defaults to 2).

#
toBeGreaterThan(expected: any): void
#
toBeGreaterThanOrEqual(expected: any): void
#
toBeLessThan(expected: any): void
#
toBeLessThanOrEqual(expected: any): void
#
toBeTruthy(): void

True by the same rules as RAS conditions.

#
toBeFalsy(): void
#
toBeEmpty(): void

Empty (the value of an empty cell).

#
toBeNothing(): void

Nothing。

#
toContain(item: any): void

An element of the array, or a substring of the string.

#
toHaveLength(n: int): void

The length of the array or string.

#
toMatch(pattern: string): void

The string matches the regular expression (VBScript.RegExp syntax).

#
toThrow(expected?: any): void

actual is a lambda without arguments. Passes if running it raises an error. An integer narrows it to that error number, a string to errors whose description contains it.

#
toMatchSnapshot(hint?: string): void

Compares the value with the one recorded before (__snapshots__/<test file name>.snap), recording it when there is none. ras test -u records it again. hint names the record among those of the same test.

test.d.ras

Interface RasTest.EachFn

The return value of test.each(table) / describe.each(table).

#
(name: string, body: any, options?: any): void

Creates a test for each row of the table. The name can contain %s %i %d %f %% and $fieldName.

test.d.ras

Interface RasTest.TestFn

#
(name: string, body: () => void, options?: any): void

Declares a test. options is { timeout: milliseconds }.

#
skip(name: string, body?: () => void, options?: any): void

Not run (the result is "skipped").

#
only(name: string, body: () => void, options?: any): void

Only the tests marked only in the file are run.

#
todo(name: string): void

A planned test without a body (the result is "todo").

#
each(table: any): EachFn

Creates a test for each row of the table.

test.d.ras

Interface RasTest.DescribeFn

#
(name: string, body: () => void): void

Groups tests. Can be nested.

#
skip(name: string, body: () => void): void
#
only(name: string, body: () => void): void
#
each(table: any): EachFn

test.d.ras

Interface RasTest.Mock

The record of calls to a replaced function.

#
readonly calls: any[]

The arguments of each call (calls[0].prompt and so on; calls[0].args for mockRun).

test.d.ras