Testing
A policy is a class with a constructor. To test one, construct it with a user and a resource, call an action, and assert on the boolean. There is no server to start, no request to fake and nothing to mock.
A table of cases
Most policies are a grid: a few kinds of user, a few states of the resource, and a handful of actions. Write that grid as a table, one row per case, and generate a test from each row. This is the test for ArticlePolicy from Writing policies:
import { describe, expect, test } from 'bun:test'
import { CurrentUser } from '@blocks/policy'
import { type Article, ArticlePolicy } from './article-policy'
const author = new CurrentUser({ id: 'u1', role: 'writer' })
const otherWriter = new CurrentUser({ id: 'u2', role: 'writer' })
const editor = new CurrentUser({ id: 'u3', role: 'editor' })
const draft: Article = { id: 'a1', authorId: 'u1', status: 'draft', archived: false }
const published: Article = { ...draft, status: 'published' }
const archived: Article = { ...draft, archived: true }
const cases = [
// user, article, action, allowed
[author, draft, 'view', true],
[otherWriter, draft, 'view', false],
[otherWriter, published, 'view', true],
[author, draft, 'update', true],
[otherWriter, draft, 'update', false],
[editor, draft, 'update', true],
[author, archived, 'update', false],
[editor, archived, 'update', false],
[author, draft, 'publish', false],
[editor, draft, 'publish', true],
[editor, published, 'publish', false],
[editor, archived, 'publish', false],
[author, draft, 'archive', false],
[editor, draft, 'archive', true],
[editor, archived, 'archive', false],
] as const
describe('ArticlePolicy', () => {
for (const [user, article, action, allowed] of cases) {
const name = `${user.role} ${user.id} ${allowed ? 'can' : 'cannot'} ${action} a ${article.archived ? 'archived' : article.status} article`
test(name, () => {
expect(new ArticlePolicy(user, article)[action]()).toBe(allowed)
})
}
})Each row reads as a sentence: this user, this article, this action, allowed or not. The test name is built from the row, so a failure names the case. Adding a rule means adding rows, and a reviewer can read the table to see exactly who may do what.
Cover the deny conditions on their own rows. Here the archived article gets a row for each action it blocks, including for an editor, who is otherwise allowed almost everything.
Running it
Run it with bun test:
bun testThe same file runs under Vitest. It uses only describe, test and expect with toBe, which Vitest provides with the same names, so change the import on its first line from bun:test to vitest.
What to test where
- The policy, with a table like this one. The user and the resource are plain values you build in the test.
- The code that resolves facts, when a policy takes facts the server looked up, as in the download example. Test the resolver with fake lookups and check the facts it produces. The policy's own tests stay free of lookups.
- The enforcement, once: that your app's error for a denial, such as
ForbiddenError, becomes a 403 and other errors do not. Enforcing has an example; its test calls the route with an allowed and a denied user.
A test asks the policy the same way the app does, by calling the action, so it checks exactly the boolean the app gets.