Skip to content

Commit fb28d0a

Browse files
author
John Doe
committed
refactor: fix unit tests
1 parent 4ed9e62 commit fb28d0a

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

packages/nx-plugin/src/executors/cli/executor.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default async function runAutorunExecutor(
1919
dryRun,
2020
verbose,
2121
command: cliCommand,
22-
env,
22+
env: targetEnv,
2323
bin,
2424
...argsObj
2525
} = terminalAndExecutorOptions;
@@ -29,14 +29,13 @@ export default async function runAutorunExecutor(
2929
...(cliCommand ? [cliCommand] : []),
3030
];
3131
const args = objectToCliArgs(argsObj);
32-
const envVariables = {
33-
...process.env,
34-
...env,
32+
const executorEnvVariables = {
33+
...targetEnv,
3534
...(verbose && { CP_VERBOSE: 'true' }),
3635
};
3736
const binString = `${command} ${positionals.join(' ')} ${args.join(' ')}`;
3837
const formattedBinString = formatCommand(binString, {
39-
env: envVariables,
38+
env: executorEnvVariables,
4039
cwd,
4140
});
4241

@@ -47,7 +46,10 @@ export default async function runAutorunExecutor(
4746
await executeProcess({
4847
command,
4948
args: [...positionals, ...args],
50-
env: envVariables,
49+
env: {
50+
...process.env,
51+
...executorEnvVariables,
52+
},
5153
...(cwd ? { cwd } : {}),
5254
});
5355
} catch (error) {

packages/nx-plugin/src/executors/cli/executor.unit.test.ts

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { logger } from '@nx/devkit';
22
import { afterAll, afterEach, beforeEach, expect, vi } from 'vitest';
33
import { executorContext } from '@code-pushup/test-nx-utils';
4-
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
4+
import { MEMFS_VOLUME, removeColorCodes } from '@code-pushup/test-utils';
55
import * as executeProcessModule from '../../internal/execute-process.js';
66
import runAutorunExecutor from './executor.js';
77

@@ -44,13 +44,17 @@ describe('runAutorunExecutor', () => {
4444

4545
it('should call executeProcess with return result', async () => {
4646
const output = await runAutorunExecutor({}, executorContext('utils'));
47-
expect(output.success).toBe(true);
48-
expect(output.command).toMatch('npx @code-pushup/cli');
49-
expect(executeProcessSpy).toHaveBeenCalledWith({
50-
command: 'npx',
51-
args: expect.arrayContaining(['@code-pushup/cli']),
52-
cwd: MEMFS_VOLUME,
47+
expect(output).toStrictEqual({
48+
success: true,
49+
command: expect.stringContaining('npx @code-pushup/cli'),
5350
});
51+
expect(executeProcessSpy).toHaveBeenCalledWith(
52+
expect.objectContaining({
53+
command: 'npx',
54+
args: expect.arrayContaining(['@code-pushup/cli']),
55+
cwd: MEMFS_VOLUME,
56+
}),
57+
);
5458
});
5559

5660
it('should normalize context', async () => {
@@ -62,12 +66,15 @@ describe('runAutorunExecutor', () => {
6266
},
6367
);
6468
expect(output.success).toBe(true);
65-
expect(output.command).toMatch('utils');
66-
expect(executeProcessSpy).toHaveBeenCalledWith({
67-
command: 'npx',
68-
args: expect.arrayContaining(['@code-pushup/cli']),
69-
cwd: 'cwd-form-context',
70-
});
69+
expect(output.command).toMatch('npx @code-pushup/cli');
70+
expect(output.command).toContain('cwd-form-context');
71+
expect(executeProcessSpy).toHaveBeenCalledWith(
72+
expect.objectContaining({
73+
command: 'npx',
74+
args: expect.arrayContaining(['@code-pushup/cli']),
75+
cwd: 'cwd-form-context',
76+
}),
77+
);
7178
});
7279

7380
it('should process executorOptions', async () => {
@@ -104,7 +111,6 @@ describe('runAutorunExecutor', () => {
104111
expect(output.command).toMatch(
105112
'--persist.format="md" --persist.format="json"',
106113
);
107-
expect(output.command).toMatch('--upload.apiKey="cp_1234567"');
108114
expect(output.command).toMatch('--upload.project="CLI"');
109115
});
110116

@@ -115,26 +121,23 @@ describe('runAutorunExecutor', () => {
115121
);
116122
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
117123

118-
expect(output.command).toMatch('--verbose');
124+
expect(output.command).toMatch('CP_VERBOSE="true"');
125+
expect(output.command).not.toMatch('--verbose');
119126
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
120-
expect(loggerInfoSpy).toHaveBeenCalledTimes(2);
121-
expect(loggerInfoSpy).toHaveBeenCalledWith(
122-
expect.stringContaining(`Run CLI executor`),
123-
);
124-
expect(loggerInfoSpy).toHaveBeenCalledWith(
125-
expect.stringContaining('Command: npx @code-pushup/cli'),
126-
);
127+
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
127128
});
128129

129130
it('should log command if dryRun is set', async () => {
130-
await runAutorunExecutor({ dryRun: true }, executorContext('utils'));
131+
const output = await runAutorunExecutor(
132+
{ dryRun: true },
133+
executorContext('utils'),
134+
);
131135

136+
expect(output).toStrictEqual({
137+
success: true,
138+
command: expect.stringContaining('npx @code-pushup/cli'),
139+
});
132140
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
133-
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
134-
expect(loggerWarnSpy).toHaveBeenCalledWith(
135-
expect.stringContaining(
136-
'DryRun execution of: npx @code-pushup/cli --dryRun',
137-
),
138-
);
141+
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
139142
});
140143
});

packages/nx-plugin/src/executors/internal/config.unit.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ describe('persistConfig', () => {
143143
},
144144
},
145145
);
146-
expect(osAgnosticPath(String(outputDir))).toBe(
147-
osAgnosticPath(`/test/root/workspace-root/.code-pushup/${projectName}`),
148-
);
146+
expect(String(outputDir)).toBe('{projectRoot}/.code-pushup');
149147
});
150148

151149
it('should parse given outputDir options', () => {

0 commit comments

Comments
 (0)