Skip to main content
Version: Next

mockApis

Home > @backstage/test-utils > mockApis

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

Signature:

namespace mockApis 

Remarks

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

1: Creating an actual fake API instance, often with a simplified version of functionality, by calling the mock API itself as a function.

// The function often accepts parameters that control its behavior
const foo = mockApis.foo();

2: Creating a mock API, where all methods are replaced with jest mocks, by calling the API's mock function.

// You can optionally supply a subset of its methods to implement
const foo = mockApis.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);

3: Creating an API factory that behaves similarly to the mock as per above.

const factory = mockApis.foo.factory({
someMethod: () => 'mocked result',
});

Functions

Function

Description

analytics()

Mock implementation of AnalyticsApi.

config(options)

Fake implementation of with optional data supplied.

discovery(options)

Fake implementation of . By default returns URLs on the form http://example.com/api/<pluginIs>.

identity(options)

Fake implementation of . By default returns no token or profile info, and the user user:default/test.

permission(options)

Fake implementation of PermissionApi. By default allows all actions.

storage(options)

Fake implementation of . Stores data temporarily in memory.

translation()

Fake implementation of . By default returns the default translation.

Namespaces

Namespace

Description

analytics

Mock implementations of AnalyticsApi.

config

Mock helpers for .

discovery

Mock implementations of .

identity

Mock implementations of .

permission

Mock implementation of PermissionApi.

storage

Mock implementations of .

translation

Mock implementations of .