Add overrideTargetPaths test utility and fix rebase issues

Adds `overrideTargetPaths` to `@backstage/cli-common/testUtils` for
cleanly mocking `targetPaths` in tests without `jest.mock` or
`jest.spyOn`. Migrates all existing test mocks to use the new utility.

Also fixes translations module imports broken by the rebase.

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Patrik Oldsberg
2026-02-23 14:20:59 +01:00
parent 553e727d5f
commit ebc01ef04d
18 changed files with 210 additions and 121 deletions
@@ -16,7 +16,7 @@
import { createMockDirectory } from '@backstage/backend-test-utils';
import { normalize } from 'node:path';
import { targetPaths } from '@backstage/cli-common';
import { overrideTargetPaths } from '@backstage/cli-common/testUtils';
import { categorizePackageDirs } from './categorizePackageDirs';
@@ -52,11 +52,8 @@ jest.mock('./categorizePackageDirs', () => ({
}));
const mockDir = createMockDirectory();
overrideTargetPaths(mockDir.path);
jest.spyOn(targetPaths, 'rootDir', 'get').mockReturnValue(mockDir.path);
jest
.spyOn(targetPaths, 'resolveRoot')
.mockImplementation((...path: string[]) => mockDir.resolve(...path));
jest.spyOn(PackageGraph, 'listTargetPackages').mockResolvedValue([
{
dir: normalize(mockDir.resolve('packages/package-a')),
@@ -83,30 +80,28 @@ jest.spyOn(PackageGraph, 'listTargetPackages').mockResolvedValue([
describe('buildApiReports', () => {
beforeEach(() => {
mockDir.setContent({
[targetPaths.rootDir]: {
'package.json': JSON.stringify({
workspaces: { packages: ['packages/*', 'plugins/*'] },
}),
packages: {
'package-a': {
'package.json': '{}',
},
'package-b': {
'package.json': '{}',
},
'package-c': {},
'README.md': 'Hello World',
'package.json': JSON.stringify({
workspaces: { packages: ['packages/*', 'plugins/*'] },
}),
packages: {
'package-a': {
'package.json': '{}',
},
plugins: {
'plugin-a': {
'package.json': '{}',
},
'plugin-b': {
'package.json': '{}',
},
'plugin-c': {
'package.json': '{}',
},
'package-b': {
'package.json': '{}',
},
'package-c': {},
'README.md': 'Hello World',
},
plugins: {
'plugin-a': {
'package.json': '{}',
},
'plugin-b': {
'package.json': '{}',
},
'plugin-c': {
'package.json': '{}',
},
},
});