Open-Source Wikis

/

Grafana

/

How to contribute

/

Testing

grafana/grafana

Testing

Grafana has three independent test suites: backend Go tests, frontend Jest tests, and Playwright end-to-end tests. CI runs all three.

Backend tests (Go)

Command What it runs
go test ./pkg/services/myservice/ Single package
go test -run TestName ./pkg/... Single test by name
make test-go-unit All Go unit tests
make test-go-integration Integration tests (some need a database)
make test-go-integration-postgres Integration tests using Postgres (start with make devenv sources=postgres_tests)
make test-go-integration-mysql Same for MySQL

CI shards the backend test run with SHARD/SHARDS environment variables; locally you don't need them.

Patterns

  • Unit tests live next to source files: foo.gofoo_test.go.
  • Mocks are usually generated under mocks/ subdirectories or hand-written *_fake.go files. The repo uses both mockery and wire-style fakes — match what neighbors do.
  • Test helpers for HTTP / web tests live in pkg/api/common_test.go — the setupHTTPServer helpers are the most common starting point for a new API test.
  • Database tests use pkg/services/sqlstore/sqlstore_test.go helpers and the testing matrix from pkg/tests/ for end-to-end DB cases.
  • Slow compile: pkg/api/ has a heavy import graph; first compile of its tests takes ~2 min. Use -run TestName aggressively.

Build tags

Many tests gate on // +build integration or oss/enterprise build tags. The integration-test Make targets pass the right tags; running raw go test ./... will skip them.

Frontend tests (Jest)

Command What it runs
yarn test path/to/file Watch mode (default!)
yarn jest --no-watch path/to/file One-shot
yarn test -t "pattern" Filter by test name regex
yarn test -u Update snapshots
yarn test:ci Full CI run (sharded)
yarn test:coverage Coverage report

⚠️ Plain yarn test defaults to --watch. In CI/AI/automation contexts always use yarn jest --no-watch or yarn test --watchAll=false.

Patterns

  • Co-located tests: Component.tsxComponent.test.tsx.
  • React Testing Library is the default — render with render(...), query with screen.getByRole, prefer accessible queries over test ids.
  • MSW is used for HTTP mocking. Per-test setup typically wires up server.use(...) handlers.
  • Storybook has its own test setup under packages/grafana-ui/. Run yarn storybook for the design-system playground.

End-to-end tests (Playwright)

Command What it runs
yarn e2e:playwright The default e2e suite (excludes cloud-plugin tests)
yarn e2e:pw path/to/test.spec.ts One file
yarn e2e:playwright:debug With Playwright UI / inspector
yarn e2e:playwright:storybook Storybook visual tests
yarn e2e:plugin:build && yarn e2e:plugin:build:dev Build the bundled test plugins

E2E specs live in e2e-playwright/. Earlier Cypress specs have been migrated; a few may still exist but Playwright is the supported framework.

Test plugins

e2e-playwright/test-plugins/ contains Yarn workspace plugins that act as fixtures (e.g. a fake datasource, a fake panel). They are built before e2e runs:

yarn e2e:plugin:build:dev

Linters and type checks (also run in CI)

make lint-go                      # golangci-lint
yarn lint                         # ESLint + stylelint
yarn lint:fix                     # ESLint auto-fix
yarn prettier:write               # Prettier auto-format
yarn typecheck                    # tsc --noEmit

Custom analysis

  • Custom ESLint rules live in packages/grafana-eslint-rules/ (workspace @grafana/eslint-plugin). Examples: theme-token-usage, no-untranslated-strings.
  • Go has a ruleguard.rules.go file at pkg/ruleguard.rules.go used by golangci-lint's ruleguard linter.
  • madge runs yarn lint:circular to detect circular TS imports.

Local QA screenshots

These screenshots came from local QA runs and are kept here as supporting test evidence rather than overview material.

Authentication: anonymous login page renders

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Testing – Grafana wiki | Factory