Skip to content

mockRoll

Jason Godesky edited this page Jul 17, 2026 · 3 revisions

mockRoll mocks Foundry’s Roll class (and in particular its evaluate and evaluateSync methods) so that you can test code that uses it.

Parameters

total: number

The number that you would like the mocked Roll.total to return.

Default: 1

Examples

import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'
import { mockRoll } from '@revolutionarygamesco/common-foundryvtt/mocks'
import { roll } from '@revolutionarygamesco/common-foundryvtt'

describe('mockRoll', () => {
  afterEach(() => {
    vi.unstubAllGlobals()
  })

  it('mocks the Roll class', async () => {
    mockRoll(1)
    const r = new foundry.dice.Roll('1d6')
    await r.evaluate()
    expect(r.total).toBe(1)
  })

  it('can be changed from test to test', () => {
    mockRoll(10)
    expect(roll('1d6')).toBe(10)
  })
})

Clone this wiki locally