@backstage/backend-test-utils 0.4.4-next.1 → 0.4.5-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,42 @@
1
1
  # @backstage/backend-test-utils
2
2
 
3
+ ## 0.4.5-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 4e79d19: The default services for `startTestBackend` and `ServiceFactoryTester` now includes the Root Health Service.
8
+ - Updated dependencies
9
+ - @backstage/backend-defaults@0.4.2-next.0
10
+ - @backstage/backend-app-api@0.8.1-next.0
11
+ - @backstage/backend-plugin-api@0.7.1-next.0
12
+ - @backstage/config@1.2.0
13
+ - @backstage/errors@1.2.4
14
+ - @backstage/types@1.1.1
15
+ - @backstage/plugin-auth-node@0.4.18-next.0
16
+ - @backstage/plugin-events-node@0.3.9-next.0
17
+
18
+ ## 0.4.4
19
+
20
+ ### Patch Changes
21
+
22
+ - 2f99178: The `ServiceFactoryTest.get` method was deprecated and the `ServiceFactoryTest.getSubject` should be used instead. The `getSubject` method has the same behavior, but has a better method name to indicate that the service instance returned is the subject currently being tested.
23
+ - edf5cc3: The function `isDockerDisabledForTests` is deprecated and will no longer be exported in the near future as it should only be used internally.
24
+ - b05e1e1: Service factories exported by this package have been updated to use the new service factory format that doesn't use a callback.
25
+ - fce7887: Added mock for the Root Health Service in `mockServices`.
26
+ - 906c817: Updated `startTestBackend` and `ServiceFactoryTester` to only accept plain service factory or backend feature objects, no longer supporting the callback form. This lines up with the changes to `@backstage/backend-plugin-api` and should not require any code changes.
27
+ - 95a3a0b: Rename frontend and backend `setupRequestMockHandlers` methods to `registerMswTestHooks`.
28
+ - b9ed1bb: bumped better-sqlite3 from ^9.0.0 to ^11.0.0
29
+ - 98ccf00: Internal refactor of `mockServices.httpAuth.factory` to allow it to still be constructed with options, but without declaring options via `createServiceFactory`.
30
+ - Updated dependencies
31
+ - @backstage/backend-plugin-api@0.7.0
32
+ - @backstage/backend-defaults@0.4.0
33
+ - @backstage/backend-app-api@0.8.0
34
+ - @backstage/plugin-events-node@0.3.8
35
+ - @backstage/plugin-auth-node@0.4.17
36
+ - @backstage/config@1.2.0
37
+ - @backstage/errors@1.2.4
38
+ - @backstage/types@1.1.1
39
+
3
40
  ## 0.4.4-next.1
4
41
 
5
42
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -44,10 +44,23 @@ var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
44
44
  var textextensions__default = /*#__PURE__*/_interopDefaultCompat(textextensions);
45
45
  var express__default = /*#__PURE__*/_interopDefaultCompat(express);
46
46
 
47
- function isDockerDisabledForTests() {
47
+ function registerMswTestHooks(worker) {
48
+ beforeAll(() => worker.listen({ onUnhandledRequest: "error" }));
49
+ afterAll(() => worker.close());
50
+ afterEach(() => worker.resetHandlers());
51
+ }
52
+
53
+ function isDockerDisabledForTests$1() {
48
54
  return Boolean(process.env.BACKSTAGE_TEST_DISABLE_DOCKER) || !Boolean(process.env.CI);
49
55
  }
50
56
 
57
+ function setupRequestMockHandlers(worker) {
58
+ registerMswTestHooks(worker);
59
+ }
60
+ function isDockerDisabledForTests() {
61
+ return isDockerDisabledForTests$1();
62
+ }
63
+
51
64
  async function attemptMemcachedConnection(connection) {
52
65
  const startTime = Date.now();
53
66
  for (; ; ) {
@@ -183,7 +196,7 @@ class TestCaches {
183
196
  */
184
197
  static create(options) {
185
198
  const ids = options?.ids;
186
- const disableDocker = options?.disableDocker ?? isDockerDisabledForTests();
199
+ const disableDocker = options?.disableDocker ?? isDockerDisabledForTests$1();
187
200
  let testCacheIds;
188
201
  if (ids) {
189
202
  testCacheIds = ids;
@@ -720,7 +733,7 @@ class TestDatabases {
720
733
  */
721
734
  static create(options) {
722
735
  const ids = options?.ids;
723
- const disableDocker = options?.disableDocker ?? isDockerDisabledForTests();
736
+ const disableDocker = options?.disableDocker ?? isDockerDisabledForTests$1();
724
737
  let testDatabaseIds;
725
738
  if (ids) {
726
739
  testDatabaseIds = ids;
@@ -814,12 +827,6 @@ class TestDatabases {
814
827
  }
815
828
  }
816
829
 
817
- function setupRequestMockHandlers(worker) {
818
- beforeAll(() => worker.listen({ onUnhandledRequest: "error" }));
819
- afterAll(() => worker.close());
820
- afterEach(() => worker.resetHandlers());
821
- }
822
-
823
830
  const tmpdirMarker = Symbol("os-tmpdir-mock");
824
831
  class MockDirectoryImpl {
825
832
  #root;
@@ -1358,14 +1365,18 @@ function createLoggerMock() {
1358
1365
  warn: jest.fn()
1359
1366
  };
1360
1367
  }
1361
- function simpleFactory(ref, factory) {
1362
- return backendPluginApi.createServiceFactory((options) => ({
1368
+ function simpleFactoryWithOptions(ref, factory) {
1369
+ const factoryWithOptions = (...options) => backendPluginApi.createServiceFactory({
1363
1370
  service: ref,
1364
1371
  deps: {},
1365
1372
  async factory() {
1366
- return factory(options);
1373
+ return factory(...options);
1367
1374
  }
1368
- }));
1375
+ })();
1376
+ return Object.assign(
1377
+ factoryWithOptions,
1378
+ factoryWithOptions(...[void 0])
1379
+ );
1369
1380
  }
1370
1381
  function simpleMock(ref, mockFactory) {
1371
1382
  return (partialImpl) => {
@@ -1395,14 +1406,20 @@ exports.mockServices = void 0;
1395
1406
  }
1396
1407
  mockServices2.rootConfig = rootConfig;
1397
1408
  ((rootConfig2) => {
1398
- rootConfig2.factory = simpleFactory(backendPluginApi.coreServices.rootConfig, rootConfig2);
1409
+ rootConfig2.factory = simpleFactoryWithOptions(
1410
+ backendPluginApi.coreServices.rootConfig,
1411
+ rootConfig2
1412
+ );
1399
1413
  })(rootConfig = mockServices2.rootConfig || (mockServices2.rootConfig = {}));
1400
1414
  function rootLogger(options) {
1401
1415
  return MockRootLoggerService.create(options);
1402
1416
  }
1403
1417
  mockServices2.rootLogger = rootLogger;
1404
1418
  ((rootLogger2) => {
1405
- rootLogger2.factory = simpleFactory(backendPluginApi.coreServices.rootLogger, rootLogger2);
1419
+ rootLogger2.factory = simpleFactoryWithOptions(
1420
+ backendPluginApi.coreServices.rootLogger,
1421
+ rootLogger2
1422
+ );
1406
1423
  rootLogger2.mock = simpleMock(backendPluginApi.coreServices.rootLogger, () => ({
1407
1424
  child: jest.fn(),
1408
1425
  debug: jest.fn(),
@@ -1425,10 +1442,11 @@ exports.mockServices = void 0;
1425
1442
  }
1426
1443
  mockServices2.tokenManager = tokenManager;
1427
1444
  ((tokenManager2) => {
1428
- tokenManager2.factory = simpleFactory(
1429
- backendPluginApi.coreServices.tokenManager,
1430
- tokenManager2
1431
- );
1445
+ tokenManager2.factory = backendPluginApi.createServiceFactory({
1446
+ service: backendPluginApi.coreServices.tokenManager,
1447
+ deps: {},
1448
+ factory: () => tokenManager2()
1449
+ });
1432
1450
  tokenManager2.mock = simpleMock(backendPluginApi.coreServices.tokenManager, () => ({
1433
1451
  authenticate: jest.fn(),
1434
1452
  getToken: jest.fn()
@@ -1439,7 +1457,11 @@ exports.mockServices = void 0;
1439
1457
  }
1440
1458
  mockServices2.identity = identity;
1441
1459
  ((identity2) => {
1442
- identity2.factory = simpleFactory(backendPluginApi.coreServices.identity, identity2);
1460
+ identity2.factory = backendPluginApi.createServiceFactory({
1461
+ service: backendPluginApi.coreServices.identity,
1462
+ deps: {},
1463
+ factory: () => identity2()
1464
+ });
1443
1465
  identity2.mock = simpleMock(backendPluginApi.coreServices.identity, () => ({
1444
1466
  getIdentity: jest.fn()
1445
1467
  }));
@@ -1507,15 +1529,17 @@ exports.mockServices = void 0;
1507
1529
  }
1508
1530
  mockServices2.httpAuth = httpAuth;
1509
1531
  ((httpAuth2) => {
1510
- httpAuth2.factory = backendPluginApi.createServiceFactory(
1511
- (options) => ({
1512
- service: backendPluginApi.coreServices.httpAuth,
1513
- deps: { plugin: backendPluginApi.coreServices.pluginMetadata },
1514
- factory: ({ plugin }) => new MockHttpAuthService(
1515
- plugin.getId(),
1516
- options?.defaultCredentials ?? exports.mockCredentials.user()
1517
- )
1518
- })
1532
+ const factoryWithOptions = (options) => backendPluginApi.createServiceFactory({
1533
+ service: backendPluginApi.coreServices.httpAuth,
1534
+ deps: { plugin: backendPluginApi.coreServices.pluginMetadata },
1535
+ factory: ({ plugin }) => new MockHttpAuthService(
1536
+ plugin.getId(),
1537
+ options?.defaultCredentials ?? exports.mockCredentials.user()
1538
+ )
1539
+ })();
1540
+ httpAuth2.factory = Object.assign(
1541
+ factoryWithOptions,
1542
+ factoryWithOptions()
1519
1543
  );
1520
1544
  httpAuth2.mock = simpleMock(backendPluginApi.coreServices.httpAuth, () => ({
1521
1545
  credentials: jest.fn(),
@@ -1638,6 +1662,7 @@ const defaultServiceFactories = [
1638
1662
  exports.mockServices.lifecycle.factory(),
1639
1663
  exports.mockServices.logger.factory(),
1640
1664
  exports.mockServices.permissions.factory(),
1665
+ exports.mockServices.rootHealth.factory(),
1641
1666
  exports.mockServices.rootLifecycle.factory(),
1642
1667
  exports.mockServices.rootLogger.factory(),
1643
1668
  exports.mockServices.scheduler.factory(),
@@ -1766,13 +1791,16 @@ async function startTestBackend(options) {
1766
1791
  deps: {
1767
1792
  config: backendPluginApi.coreServices.rootConfig,
1768
1793
  lifecycle: backendPluginApi.coreServices.rootLifecycle,
1769
- rootLogger: backendPluginApi.coreServices.rootLogger
1794
+ rootLogger: backendPluginApi.coreServices.rootLogger,
1795
+ health: backendPluginApi.coreServices.rootHealth
1770
1796
  },
1771
- async factory({ config, lifecycle, rootLogger }) {
1797
+ async factory({ config, lifecycle, rootLogger, health }) {
1772
1798
  const router = backendAppApi.DefaultRootHttpRouter.create();
1773
1799
  const logger = rootLogger.child({ service: "rootHttpRouter" });
1774
1800
  const app = express__default.default();
1775
1801
  const middleware = backendAppApi.MiddlewareFactory.create({ config, logger });
1802
+ const healthRouter = rootHttpRouter.createHealthRouter({ health });
1803
+ app.use(healthRouter);
1776
1804
  app.use(router.handler());
1777
1805
  app.use(middleware.notFound());
1778
1806
  app.use(middleware.error());
@@ -2047,13 +2075,13 @@ function toInternalServiceFactory(factory) {
2047
2075
  }
2048
2076
  return f;
2049
2077
  }
2050
- const pluginMetadataServiceFactory = backendPluginApi.createServiceFactory(
2051
- (options) => ({
2078
+ function createPluginMetadataServiceFactory(pluginId) {
2079
+ return backendPluginApi.createServiceFactory({
2052
2080
  service: backendPluginApi.coreServices.pluginMetadata,
2053
2081
  deps: {},
2054
- factory: async () => ({ getId: () => options?.pluginId })
2055
- })
2056
- );
2082
+ factory: async () => ({ getId: () => pluginId })
2083
+ });
2084
+ }
2057
2085
  class ServiceRegistry {
2058
2086
  static create(factories) {
2059
2087
  const registry = new ServiceRegistry(factories);
@@ -2076,7 +2104,7 @@ class ServiceRegistry {
2076
2104
  #resolveFactory(ref, pluginId) {
2077
2105
  if (ref.id === backendPluginApi.coreServices.pluginMetadata.id) {
2078
2106
  return Promise.resolve(
2079
- toInternalServiceFactory(pluginMetadataServiceFactory({ pluginId }))
2107
+ toInternalServiceFactory(createPluginMetadataServiceFactory(pluginId))
2080
2108
  );
2081
2109
  }
2082
2110
  let resolvedFactory = this.#providedFactories.get(ref.id);
@@ -2249,20 +2277,25 @@ class ServiceFactoryTester {
2249
2277
  * @returns A new tester instance for the provided subject.
2250
2278
  */
2251
2279
  static from(subject, options) {
2252
- const subjectFactory = typeof subject === "function" ? subject() : subject;
2253
2280
  const registry = ServiceRegistry.create([
2254
2281
  ...defaultServiceFactories,
2255
- ...options?.dependencies?.map(
2256
- (f) => typeof f === "function" ? f() : f
2257
- ) ?? [],
2258
- subjectFactory
2282
+ ...options?.dependencies ?? [],
2283
+ subject
2259
2284
  ]);
2260
- return new ServiceFactoryTester(subjectFactory.service, registry);
2285
+ return new ServiceFactoryTester(subject.service, registry);
2261
2286
  }
2262
2287
  constructor(subject, registry) {
2263
2288
  this.#subject = subject;
2264
2289
  this.#registry = registry;
2265
2290
  }
2291
+ /**
2292
+ * Returns the service instance for the subject.
2293
+ *
2294
+ * @deprecated Use `getSubject` instead.
2295
+ */
2296
+ async get(...args) {
2297
+ return this.getSubject(...args);
2298
+ }
2266
2299
  /**
2267
2300
  * Returns the service instance for the subject.
2268
2301
  *
@@ -2273,7 +2306,7 @@ class ServiceFactoryTester {
2273
2306
  *
2274
2307
  * By default the plugin ID 'test' is used.
2275
2308
  */
2276
- async get(...args) {
2309
+ async getSubject(...args) {
2277
2310
  const [pluginId] = args;
2278
2311
  return this.#registry.get(this.#subject, pluginId ?? "test");
2279
2312
  }
@@ -2299,6 +2332,7 @@ exports.TestCaches = TestCaches;
2299
2332
  exports.TestDatabases = TestDatabases;
2300
2333
  exports.createMockDirectory = createMockDirectory;
2301
2334
  exports.isDockerDisabledForTests = isDockerDisabledForTests;
2335
+ exports.registerMswTestHooks = registerMswTestHooks;
2302
2336
  exports.setupRequestMockHandlers = setupRequestMockHandlers;
2303
2337
  exports.startTestBackend = startTestBackend;
2304
2338
  //# sourceMappingURL=index.cjs.js.map