Skip to content

Commit 768ca49

Browse files
author
John Doe
committed
refactor: add int-test target to test-nx-utils
1 parent 7df6101 commit 768ca49

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

testing/test-nx-utils/src/lib/utils/nx.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import {
99
} from '@nx/devkit';
1010
import { libraryGenerator } from '@nx/js';
1111
import type { LibraryGeneratorSchema } from '@nx/js/src/generators/library/schema';
12+
import { execFile } from 'node:child_process';
1213
import path from 'node:path';
14+
import { promisify } from 'node:util';
1315
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
14-
import { executeProcess } from '@code-pushup/utils';
16+
17+
const execFileAsync = promisify(execFile);
1518

1619
export function executorContext<
1720
T extends { projectName: string; cwd?: string },
@@ -82,11 +85,20 @@ export async function nxShowProjectJson<T extends ProjectConfiguration>(
8285
cwd: string,
8386
project: string,
8487
) {
85-
const { code, stderr, stdout } = await executeProcess({
86-
command: 'npx',
87-
args: ['nx', 'show', `project --json ${project}`],
88-
cwd,
89-
});
88+
try {
89+
const { stdout, stderr } = await execFileAsync(
90+
'npx',
91+
['nx', 'show', 'project', project, '--json'],
92+
{ cwd },
93+
);
9094

91-
return { code, stderr, projectJson: JSON.parse(stdout) as T };
95+
return { code: 0, stderr, projectJson: JSON.parse(stdout) as T };
96+
} catch (error) {
97+
const execError = error as { code?: number; stderr?: string };
98+
return {
99+
code: execError.code ?? 1,
100+
stderr: execError.stderr ?? String(error),
101+
projectJson: {} as T,
102+
};
103+
}
92104
}

testing/test-nx-utils/src/lib/utils/tree.int.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { readFile } from 'node:fs/promises';
12
import path from 'node:path';
23
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
34
import { describe, expect, it } from 'vitest';
4-
import { readJsonFile } from '@code-pushup/utils';
55
import { materializeTree } from './tree.js';
66

77
describe('materializeTree', () => {
@@ -14,8 +14,10 @@ describe('materializeTree', () => {
1414
expect(tree.exists('nx.json')).toBe(true);
1515

1616
await materializeTree(tree, root);
17-
18-
await expect(readJsonFile(path.join(root, 'nx.json'))).resolves.toEqual({
17+
const nxJson = JSON.parse(
18+
await readFile(path.join(root, 'nx.json'), 'utf-8'),
19+
);
20+
expect(nxJson).toStrictEqual({
1921
affected: {
2022
defaultBase: 'main',
2123
},

0 commit comments

Comments
 (0)