Skip to content

Commit acbd3ab

Browse files
committed
fix: e2e tests
1 parent 90daca5 commit acbd3ab

File tree

2 files changed

+51
-20
lines changed

2 files changed

+51
-20
lines changed

e2e/nx-plugin-e2e/tests/executor-cli.e2e.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {type Tree, updateProjectConfiguration} from '@nx/devkit';
1+
import { type Tree, updateProjectConfiguration } from '@nx/devkit';
22
import path from 'node:path';
3-
import {readProjectConfiguration} from 'nx/src/generators/utils/project-configuration';
4-
import {afterEach, expect} from 'vitest';
3+
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
4+
import { afterEach, expect } from 'vitest';
55
import {
66
type AutorunCommandExecutorOptions,
77
generateCodePushupConfig,
@@ -17,27 +17,27 @@ import {
1717
removeColorCodes,
1818
teardownTestFolder,
1919
} from '@code-pushup/test-utils';
20-
import {executeProcess, readJsonFile} from '@code-pushup/utils';
21-
import {INLINE_PLUGIN} from './inline-plugin.js';
20+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
21+
import { INLINE_PLUGIN } from './inline-plugin.js';
2222

2323
async function addTargetToWorkspace(
2424
tree: Tree,
2525
options: { cwd: string; project: string },
2626
executorOptions?: AutorunCommandExecutorOptions,
2727
) {
28-
const {cwd, project} = options;
28+
const { cwd, project } = options;
2929
const projectCfg = readProjectConfiguration(tree, project);
3030
updateProjectConfiguration(tree, project, {
3131
...projectCfg,
3232
targets: {
3333
...projectCfg.targets,
3434
'code-pushup': {
3535
executor: '@code-pushup/nx-plugin:cli',
36-
...(executorOptions && {options: executorOptions}),
36+
...(executorOptions && { options: executorOptions }),
3737
},
3838
},
3939
});
40-
const {root} = projectCfg;
40+
const { root } = projectCfg;
4141
generateCodePushupConfig(tree, root, {
4242
plugins: [
4343
{
@@ -73,8 +73,8 @@ describe('executor command', () => {
7373

7474
it('should execute no specific command by default', async () => {
7575
const cwd = path.join(testFileDir, 'execute-default-command');
76-
await addTargetToWorkspace(tree, {cwd, project});
77-
const {stdout, code} = await executeProcess({
76+
await addTargetToWorkspace(tree, { cwd, project });
77+
const { stdout, code } = await executeProcess({
7878
command: 'npx',
7979
args: ['nx', 'run', `${project}:code-pushup`, '--dryRun'],
8080
cwd,
@@ -87,9 +87,9 @@ describe('executor command', () => {
8787

8888
it('should execute print-config executor', async () => {
8989
const cwd = path.join(testFileDir, 'execute-print-config-command');
90-
await addTargetToWorkspace(tree, {cwd, project});
90+
await addTargetToWorkspace(tree, { cwd, project });
9191

92-
const {stdout, code} = await executeProcess({
92+
const { stdout, code } = await executeProcess({
9393
command: 'npx',
9494
args: ['nx', 'run', `${project}:code-pushup`, 'print-config'],
9595
cwd,
@@ -106,9 +106,9 @@ describe('executor command', () => {
106106

107107
it('should execute print-config executor with api key', async () => {
108108
const cwd = path.join(testFileDir, 'execute-print-config-command');
109-
await addTargetToWorkspace(tree, {cwd, project});
109+
await addTargetToWorkspace(tree, { cwd, project });
110110

111-
const {stdout, code} = await executeProcess({
111+
const { stdout, code } = await executeProcess({
112112
command: 'npx',
113113
args: [
114114
'nx',
@@ -134,7 +134,7 @@ describe('executor command', () => {
134134
const cwd = path.join(testFileDir, 'execute-collect-with-merged-options');
135135
await addTargetToWorkspace(
136136
tree,
137-
{cwd, project},
137+
{ cwd, project },
138138
{
139139
persist: {
140140
outputDir: '.reports',
@@ -143,7 +143,7 @@ describe('executor command', () => {
143143
},
144144
);
145145

146-
const {stdout, code} = await executeProcess({
146+
const { stdout, code } = await executeProcess({
147147
command: 'npx',
148148
args: [
149149
'nx',
@@ -168,9 +168,9 @@ describe('executor command', () => {
168168

169169
it('should execute collect executor and add report to sub folder named by project', async () => {
170170
const cwd = path.join(testFileDir, 'execute-collect-command');
171-
await addTargetToWorkspace(tree, {cwd, project});
171+
await addTargetToWorkspace(tree, { cwd, project });
172172

173-
const {stdout, code} = await executeProcess({
173+
const { stdout, code } = await executeProcess({
174174
command: 'npx',
175175
args: ['nx', 'run', `${project}:code-pushup`, 'collect'],
176176
cwd,

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { logger } from '@nx/devkit';
22
import { execSync } from 'node:child_process';
3-
import { afterEach, expect, vi } from 'vitest';
3+
import { afterAll, afterEach, beforeEach, expect, vi } from 'vitest';
44
import { executorContext } from '@code-pushup/test-nx-utils';
55
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
66
import runAutorunExecutor from './executor.js';
@@ -19,14 +19,31 @@ vi.mock('node:child_process', async () => {
1919
});
2020

2121
describe('runAutorunExecutor', () => {
22+
const processEnvCP = Object.fromEntries(
23+
Object.entries(process.env).filter(([k]) => k.startsWith('CP_')),
24+
);
2225
const loggerInfoSpy = vi.spyOn(logger, 'info');
2326
const loggerWarnSpy = vi.spyOn(logger, 'warn');
2427

28+
beforeAll(() => {
29+
Object.entries(process.env)
30+
.filter(([k]) => k.startsWith('CP_'))
31+
.forEach(([k]) => delete process.env[k]);
32+
});
33+
34+
beforeEach(() => {
35+
vi.unstubAllEnvs();
36+
});
37+
2538
afterEach(() => {
2639
loggerWarnSpy.mockReset();
2740
loggerInfoSpy.mockReset();
2841
});
2942

43+
afterAll(() => {
44+
Object.entries(processEnvCP).forEach(([k, v]) => (process.env[k] = v));
45+
});
46+
3047
it('should call execSync with return result', async () => {
3148
const output = await runAutorunExecutor({}, executorContext('utils'));
3249
expect(output.success).toBe(true);
@@ -63,7 +80,20 @@ describe('runAutorunExecutor', () => {
6380
expect(output.command).toMatch('--persist.filename="REPORT"');
6481
});
6582

66-
it('should create command from context, options and arguments', async () => {
83+
it('should create command from context and options if no api key is set', async () => {
84+
vi.stubEnv('CP_PROJECT', 'CLI');
85+
const output = await runAutorunExecutor(
86+
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
87+
executorContext('core'),
88+
);
89+
expect(output.command).toMatch('--persist.filename="REPORT"');
90+
expect(output.command).toMatch(
91+
'--persist.format="md" --persist.format="json"',
92+
);
93+
});
94+
95+
it('should create command from context, options and arguments if api key is set', async () => {
96+
vi.stubEnv('CP_API_KEY', 'cp_1234567');
6797
vi.stubEnv('CP_PROJECT', 'CLI');
6898
const output = await runAutorunExecutor(
6999
{ persist: { filename: 'REPORT', format: ['md', 'json'] } },
@@ -73,6 +103,7 @@ describe('runAutorunExecutor', () => {
73103
expect(output.command).toMatch(
74104
'--persist.format="md" --persist.format="json"',
75105
);
106+
expect(output.command).toMatch('--upload.apiKey="cp_1234567"');
76107
expect(output.command).toMatch('--upload.project="CLI"');
77108
});
78109

0 commit comments

Comments
 (0)