Skip to content

Commit be53602

Browse files
author
John Doe
committed
refactor: fix unit test
1 parent 38c07da commit be53602

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ExecutorContext } from '@nx/devkit';
1+
import type { ExecutorContext } from '@nx/devkit';
22
import { executeProcess } from '../../internal/execute-process.js';
33
import { normalizeContext } from '../internal/context.js';
44
import type { AutorunCommandExecutorOptions } from './schema.js';
@@ -36,11 +36,11 @@ export default async function runAutorunExecutor(
3636
];
3737
const args = [...positionals, ...objectToCliArgs(restArgs)];
3838
const executorEnvVariables = {
39-
...process.env,
4039
...(verbose && { CP_VERBOSE: 'true' }),
4140
};
4241
const commandString = formatCommandStatus([command, ...args].join(' '), {
4342
cwd: context.cwd,
43+
env: executorEnvVariables,
4444
});
4545

4646
if (dryRun) {
@@ -51,7 +51,14 @@ export default async function runAutorunExecutor(
5151
command,
5252
args,
5353
...(context.cwd ? { cwd: context.cwd } : {}),
54-
...(verbose ? { env: executorEnvVariables } : {}),
54+
...(verbose
55+
? {
56+
env: {
57+
...process.env,
58+
...executorEnvVariables,
59+
},
60+
}
61+
: {}),
5562
});
5663
} catch (error) {
5764
logger.error(stringifyError(error));

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

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

88
describe('runAutorunExecutor', () => {
99
const processEnvCP = Object.fromEntries(
1010
Object.entries(process.env).filter(([k]) => k.startsWith('CP_')),
1111
);
12-
const loggerInfoSpy = vi.spyOn(logger, 'info');
13-
const loggerWarnSpy = vi.spyOn(logger, 'warn');
1412
const executeProcessSpy = vi.spyOn(executeProcessModule, 'executeProcess');
1513

1614
beforeAll(() => {
@@ -37,8 +35,6 @@ describe('runAutorunExecutor', () => {
3735
});
3836

3937
afterEach(() => {
40-
loggerWarnSpy.mockReset();
41-
loggerInfoSpy.mockReset();
4238
executeProcessSpy.mockReset();
4339
});
4440

@@ -110,24 +106,28 @@ describe('runAutorunExecutor', () => {
110106

111107
it('should set env var information if verbose is set', async () => {
112108
const output = await runAutorunExecutor(
113-
{ verbose: true },
109+
{
110+
dryRun: true, // here to produce log
111+
verbose: true,
112+
},
114113
{ ...executorContext('github-action'), cwd: '<CWD>' },
115114
);
116-
expect(executeProcessSpy).toHaveBeenCalledTimes(1);
115+
116+
expect(executeProcessSpy).toHaveBeenCalledTimes(0);
117117

118118
expect(output.command).not.toContain('--verbose');
119-
expect(loggerWarnSpy).toHaveBeenCalledTimes(0);
120-
expect(loggerInfoSpy).toHaveBeenCalledWith(
121-
expect.stringContaining('CP_VERBOSE=true'),
119+
expect(logger.warn).toHaveBeenCalledTimes(1);
120+
expect(logger.warn).toHaveBeenCalledWith(
121+
expect.stringContaining('CP_VERBOSE=\"true\"'),
122122
);
123123
});
124124

125125
it('should log command if dryRun is set', async () => {
126126
await runAutorunExecutor({ dryRun: true }, executorContext('utils'));
127127

128-
expect(loggerInfoSpy).toHaveBeenCalledTimes(0);
129-
expect(loggerWarnSpy).toHaveBeenCalledTimes(1);
130-
expect(loggerWarnSpy).toHaveBeenCalledWith(
128+
expect(logger.command).toHaveBeenCalledTimes(0);
129+
expect(logger.warn).toHaveBeenCalledTimes(1);
130+
expect(logger.warn).toHaveBeenCalledWith(
131131
expect.stringContaining('DryRun execution of'),
132132
);
133133
});

0 commit comments

Comments
 (0)