Testing Strategy
Relevant source files
The following files were used as context for generating this wiki page:
- CLAUDE.md
- AGENTS.md
- README.md
- packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte
- packages/string-templates/test/javascript.spec.ts
- packages/backend-core/src/plugin/tests/validation.spec.ts
- packages/server/src/jsRunner/tests/jsRunner.spec.ts
- packages/bbui/README.md
- packages/worker/src/api/routes/global/tests/templates.spec.ts
- packages/backend-core/src/db/Replication.spec.ts
- packages/server/src/middleware/tests/trimViewRowInfo.spec.ts
- packages/server/src/api/controllers/query/import/sources/tests/openapi3/openapi3.spec.js
The Budibase testing strategy employs a multi-layered approach to ensure code quality, stability, and correctness across its monorepo architecture. The primary framework is Jest, used for unit, integration, and API testing. The strategy emphasizes the use of test utilities and helpers to create consistent and maintainable tests, particularly within the packages/server package.
Testing is organized within each package, typically in test or tests subdirectories. The strategy covers everything from individual function validation and component behavior to complex API interactions, plugin validation, and specialized scenarios like database replication and version upgrades. External services are mocked using libraries like nock to ensure tests are isolated and deterministic.
Framework and Tooling
Budibase relies on a standard set of tools for its testing infrastructure, with Jest at its core.
Core Tools
| Tool | Purpose | Example File |
|---|---|---|
| Jest | The primary JavaScript testing framework for running tests, managing assertions, and mocking. | packages/string-templates/test/javascript.spec.ts |
| nock | Used for mocking HTTP requests to external services, ensuring API tests are isolated and predictable. | packages/backend-core/src/plugin/tests/validation.spec.ts |
| timekeeper | Freezes time in tests to make time-dependent logic deterministic. | packages/string-templates/test/javascript.spec.ts:9 |
| @apidevtools/swagger-parser | Used in tests to parse and validate OpenAPI specifications for import functionality. | packages/server/src/api/controllers/query/import/sources/tests/openapi3/openapi3.spec.js:3 |
Test Execution
Tests are executed via the yarn test <filename> command within a specific package directory. This command invokes Jest with the appropriate configuration for that package.
The overall test process can be visualized as follows:
This diagram shows the general flow of test execution using Jest.
Sources: CLAUDE.md, AGENTS.md
Test Types and Examples
Unit & Integration Testing
Unit and integration tests form the foundation of the testing strategy. They are written using Jest's describe and it syntax.
JavaScript Template Testing
The packages/string-templates package contains extensive tests for its JavaScript execution engine. These tests verify simple expressions, context variable access (bindings), error handling, and security against malicious code. The tests also include a suite of "real test cases from Budicloud" to validate complex, multi-step logic extracted from production use cases.
Sources: packages/string-templates/test/javascript.spec.ts
JS Runner (isolated-vm) Testing
The jsRunner feature, which uses isolated-vm for sandboxed JavaScript execution, is tested in packages/server. These tests use a TestConfiguration helper to set up a proper context. They validate basic execution, security against sandbox escapes, and context immutability.
// packages/server/src/jsRunner/tests/jsRunner.spec.ts:28-38
it("should not allow the context to be mutated", async () => {
const context = { array: [1] }
const result = await processJS(
`
const array = $("array");
array.push(2);
return array[1]
`,
context
)
expect(result).toEqual(2)
expect(context.array).toEqual([1])
})
Sources: packages/server/src/jsRunner/tests/jsRunner.spec.ts
Plugin Validation Testing
Plugin schema validation is tested in packages/backend-core. These tests use nock to mock HTTP responses for fetching plugin schemas (e.g., datasource, automation, component) and then run the validate utility against the mocked schema to ensure correctness.
This diagram illustrates the sequence for testing plugin schema validation using nock.
Sources: packages/backend-core/src/plugin/tests/validation.spec.ts
API & Middleware Testing
API-level tests are crucial, especially in packages/server. The project provides a TestConfiguration utility to streamline the setup for these tests.
TestConfiguration Helper
The TestConfiguration class is a key utility for API tests. It initializes a test environment and provides access to a test API client. This is used across many test files, such as in middleware tests.
The following class diagram shows the structure of the TestConfiguration used in middleware tests.
Middleware Testing
Middleware functions are tested individually. For example, the trimViewRowInfoMiddleware test file sets up a TestConfiguration, mocks SDK functions like sdk.views.get and sdk.tables.getTable, and then executes the middleware with a sample context to assert that the request body is correctly trimmed based on the view's schema.
Sources: packages/server/src/middleware/tests/trimViewRowInfo.spec.ts
Specialized Testing
Automation Testing
Automations can be tested directly from the Budibase builder UI. A "Test data" modal allows users to provide input for an automation trigger, either by filling out fields or by providing a raw JSON object.
The process is as follows:
- User opens the "Add test data" modal.
- User chooses to input data as "values" (form fields) or "JSON".
- The component validates the input to ensure required fields for the trigger are present.
- On "Run test", the
automationStore.actions.testfunction is called with the automation definition and the provided test data. - The backend executes the automation, and the result is displayed to the user.
This diagram shows the user flow for testing an automation within the builder.
Sources: packages/builder/src/components/automation/AutomationBuilder/FlowChart/TestDataModal.svelte
Database Replication Testing
The Replication.spec.ts file demonstrates how database replication logic is tested. It uses Jest's mocking capabilities to mock the PouchDB instances (sourceDb and targetDb) and asserts the behavior of the Replication class. Tests cover filtering logic (e.g., skipping migration documents, automation logs) and resolving revision inconsistencies between source and target databases.
Sources: packages/backend-core/src/db/Replication.spec.ts
Template Export/Import Testing
Tests for template functionality, such as exporting email templates, are located in packages/worker. These tests use the TestConfiguration API helper to make requests to the template endpoints. They verify that the exported data (in YAML format) is correct and that the API handles different scenarios, such as exporting default templates versus user-edited ones.
Sources: packages/worker/src/api/routes/global/tests/templates.spec.ts
Test Style and Guidelines
The project maintains a set of guidelines to ensure consistency in testing.
- Structure: Use Jest's
describe/itstructure. - Mocking: Mock external services using
nock. - Assertions: Assert the final outcome rather than intermediate states.
- No
console.log:console.logoutput is not visible in test STDOUT and should be avoided. - Utilities: Use provided test utilities like
TestConfigurationfor API tests,createAutomationBuilderfor automation tests, andbasicTablefor creating test resources. This promotes code reuse and simplifies test setup. - Datasource-specific tests: For tests using
datasourceDescribe, theDATASOURCEenvironment variable can be used to narrow the test run to a single database type.