Skip to main content

mockServices

Home > @backstage/backend-test-utils > mockServices

Mock implementations of the core services, to be used in tests.

Signature:

namespace mockServices 

Remarks

There are some variations among the services depending on what needs tests might have, but overall there are three main usage patterns:

  1. Creating an actual fake service instance, often with a simplified version of functionality, by calling the mock service itself as a function.
// The function often accepts parameters that control its behavior
const foo = mockServices.foo();
  1. Creating a mock service, where all methods are replaced with jest mocks, by calling the service's mock function.
// You can optionally supply a subset of its methods to implement
const foo = mockServices.foo.mock({
someMethod: () => 'mocked result',
});
// After exercising your test, you can make assertions on the mock:
expect(foo.someMethod).toHaveBeenCalledTimes(2);
expect(foo.otherMethod).toHaveBeenCalledWith(testData);
  1. Creating a service factory that behaves similarly to the mock as per above.
await startTestBackend({
features: [
mockServices.foo.factory({
someMethod: () => 'mocked result',
})
],
});

Functions

Function

Description

auth(options)

database(options)

Creates a mock implementation of the coreServices.database. Just returns the given knex instance, which is useful in combination with the TestDatabases facility.

discovery()

httpAuth(options)

Creates a mock implementation of the HttpAuthService.

By default all requests without credentials are treated as requests from the default mock user principal. This behavior can be configured with the defaultCredentials option.

rootConfig(options)

rootLogger(options)

userInfo(customInfo)

Creates a mock implementation of the UserInfoService.

By default it extracts the user's entity ref from a user principal and returns that as the only ownership entity ref, but this can be overridden by passing in a custom set of user info.

Namespaces

Namespace

Description

auth

cache

database

discovery

events

httpAuth

httpRouter

lifecycle

logger

permissions

rootConfig

rootHealth

rootHttpRouter

rootLifecycle

rootLogger

scheduler

urlReader

userInfo