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 >.
Global test
const test: RasTest.TestFn
Declares a test. body is a lambda without arguments.
Global it
const it: RasTest.TestFn
Same as test.
Function beforeAll
function beforeAll(fn: () => void): void
Runs once before the first test of the describe (of the file at the top level).
Function afterAll
function afterAll(fn: () => void): void
Runs once after the last test of the describe.
Function beforeEach
function beforeEach(fn: () => void): void
Runs before each test in the describe.
Function afterEach
function afterEach(fn: () => void): void
Runs after each test in the describe.
Function expect
function expect<T>(actual: T): RasTest.Matchers<T>
Checks a value. expect(actual).toBe(expected).
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.
Function tempDir
function tempDir(): string
An empty folder created for each test and deleted after the test ends.
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).
Function printed
function printed(): string[]
The lines debug.print printed during the test.
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.
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.
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.
Function mockGetOpenFilename
function mockGetOpenFilename(handler?: () => any): RasTest.Mock
Calls handler() instead of getOpenFilename (return false for cancel).
Function mockGetSaveAsFilename
function mockGetSaveAsFilename(handler?: () => any): RasTest.Mock
Calls handler() instead of getSaveAsFilename (return false for cancel).
Function mockRun
function mockRun(macroName: string, handler?: any): RasTest.Mock
Calls handler(...args) instead of application.run(macroName, ...) (a call to hand-written VBA).
Function mockCreateObject
function mockCreateObject(progId: string, factory: any): RasTest.Mock
createObject(progId) / new use the object factory() returns.
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(digitsdefaults 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.RegExpsyntax).- #
toThrow(expected?: any): void
actualis 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 -urecords it again.hintnames the record among those of the same test.
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.
Interface RasTest.TestFn
- #
(name: string, body: () => void, options?: any): void
Declares a test.
optionsis{ 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
onlyin 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.
Interface RasTest.DescribeFn
Interface RasTest.Mock
The record of calls to a replaced function.