Skip to content

Commit ed2dae9

Browse files
committed
chore: create v1-v2 function helpers and tests
1 parent db535af commit ed2dae9

File tree

6 files changed

+313
-165
lines changed

6 files changed

+313
-165
lines changed

packages/nx-plugin/src/plugin/plugin.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import {
55
type CreateNodesV2,
66
createNodesFromFiles,
77
} from '@nx/devkit';
8-
import { PROJECT_JSON_FILE_NAME } from '../internal/constants';
9-
import { createTargets } from './target/targets';
10-
import type { CreateNodesOptions } from './types';
11-
import { normalizedCreateNodesContext } from './utils';
12-
13-
// name has to be "createNodes" to get picked up by Nx
8+
import { PROJECT_JSON_FILE_NAME } from '../internal/constants.js';
9+
import { createTargets } from './target/targets.js';
10+
import type { CreateNodesOptions } from './types.js';
11+
import { normalizedCreateNodesContext } from './utils.js';
1412

13+
/** Create the nodes for a V1 Plugin. The name `createNodes` is required by Nx in order to be picked up as a plugin. */
1514
export const createNodes: CreateNodes = [
1615
`**/${PROJECT_JSON_FILE_NAME}`,
1716
async (
@@ -36,6 +35,7 @@ export const createNodes: CreateNodes = [
3635
},
3736
];
3837

38+
/** Create the nodes for a V2 Plugin. The name `createNodesV2` is required by Nx in order to be picked up as a plugin. */
3939
export const createNodesV2: CreateNodesV2 = [
4040
`**/${PROJECT_JSON_FILE_NAME}`,
4141
async (configFiles, options, context) =>
@@ -48,6 +48,7 @@ export const createNodesV2: CreateNodesV2 = [
4848
globMatchingFile,
4949
parsedCreateNodesOptions,
5050
);
51+
5152
return {
5253
projects: {
5354
[normalizedContext.projectRoot]: {
Lines changed: 127 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,160 @@
1-
import type { CreateNodesContext } from '@nx/devkit';
1+
import type { CreateNodesContext, CreateNodesContextV2 } from '@nx/devkit';
22
import { vol } from 'memfs';
33
import { describe, expect } from 'vitest';
4-
import { invokeCreateNodesOnVirtualFiles } from '@code-pushup/test-nx-utils';
4+
import {
5+
createNodesContextV1,
6+
createNodesContextV2,
7+
invokeCreateNodesOnVirtualFilesV1,
8+
invokeCreateNodesOnVirtualFilesV2,
9+
} from '@code-pushup/test-nx-utils';
510
import { PACKAGE_NAME, PROJECT_JSON_FILE_NAME } from '../internal/constants.js';
611
import { CP_TARGET_NAME } from './constants.js';
7-
import { createNodes } from './plugin.js';
12+
import { createNodes, createNodesV2 } from './plugin.js';
813

914
describe('@code-pushup/nx-plugin/plugin', () => {
10-
let context: CreateNodesContext;
15+
describe('V1', () => {
16+
let context: CreateNodesContext;
1117

12-
beforeEach(() => {
13-
context = {
14-
nxJsonConfiguration: {},
15-
workspaceRoot: '',
16-
};
17-
});
18+
beforeEach(() => {
19+
context = createNodesContextV1({
20+
nxJsonConfiguration: {},
21+
workspaceRoot: '',
22+
});
23+
});
1824

19-
afterEach(() => {
20-
vol.reset();
21-
});
25+
afterEach(() => {
26+
vol.reset();
27+
});
2228

23-
it('should normalize context and use it to create the configuration target on ROOT project', async () => {
24-
const projectRoot = '.';
25-
const matchingFilesData = {
26-
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
27-
name: '@org/empty-root',
28-
})}`,
29-
};
29+
it('should normalize context and use it to create the configuration target on ROOT project', async () => {
30+
const projectRoot = '.';
31+
const matchingFilesData = {
32+
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
33+
name: '@org/empty-root',
34+
})}`,
35+
};
3036

31-
await expect(
32-
invokeCreateNodesOnVirtualFiles(
33-
createNodes,
34-
context,
35-
{},
36-
{ matchingFilesData },
37-
),
38-
).resolves.toStrictEqual({
39-
[projectRoot]: {
40-
targets: {
41-
[`${CP_TARGET_NAME}--configuration`]: {
42-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
37+
await expect(
38+
invokeCreateNodesOnVirtualFilesV1(
39+
createNodes,
40+
context,
41+
{},
42+
{ matchingFilesData },
43+
),
44+
).resolves.toStrictEqual({
45+
[projectRoot]: {
46+
targets: {
47+
[`${CP_TARGET_NAME}--configuration`]: {
48+
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
49+
},
4350
},
4451
},
45-
},
52+
});
4653
});
47-
});
4854

49-
it('should normalize context and use it to create the configuration target on PACKAGE project', async () => {
50-
const projectRoot = 'apps/my-app';
51-
const matchingFilesData = {
52-
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
53-
name: '@org/empty-root',
54-
})}`,
55-
};
55+
it('should create the executor target on PACKAGE project if configured', async () => {
56+
const projectRoot = 'apps/my-app';
57+
const matchingFilesData = {
58+
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
59+
name: '@org/empty-root',
60+
})}`,
61+
[`${projectRoot}/code-pushup.config.ts`]: '{}',
62+
};
5663

57-
await expect(
58-
invokeCreateNodesOnVirtualFiles(
59-
createNodes,
60-
context,
61-
{},
62-
{ matchingFilesData },
63-
),
64-
).resolves.toStrictEqual({
65-
[projectRoot]: {
66-
targets: {
67-
[`${CP_TARGET_NAME}--configuration`]: {
68-
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
64+
await expect(
65+
invokeCreateNodesOnVirtualFilesV1(
66+
createNodes,
67+
context,
68+
{
69+
projectPrefix: 'cli',
70+
},
71+
{ matchingFilesData },
72+
),
73+
).resolves.toStrictEqual({
74+
[projectRoot]: {
75+
targets: {
76+
[CP_TARGET_NAME]: {
77+
executor: `${PACKAGE_NAME}:cli`,
78+
options: {
79+
projectPrefix: 'cli',
80+
},
81+
},
6982
},
7083
},
71-
},
84+
});
7285
});
7386
});
7487

75-
it('should create the executor target on ROOT project if configured', async () => {
76-
const projectRoot = '.';
77-
const matchingFilesData = {
78-
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
79-
name: '@org/empty-root',
80-
})}`,
81-
[`${projectRoot}/code-pushup.config.ts`]: '{}',
82-
};
88+
describe('V2', () => {
89+
let context: CreateNodesContextV2;
8390

84-
await expect(
85-
invokeCreateNodesOnVirtualFiles(
86-
createNodes,
87-
context,
88-
{
89-
projectPrefix: 'cli',
90-
},
91-
{ matchingFilesData },
92-
),
93-
).resolves.toStrictEqual({
94-
[projectRoot]: {
95-
targets: {
96-
[CP_TARGET_NAME]: {
97-
executor: `${PACKAGE_NAME}:cli`,
98-
options: {
99-
projectPrefix: 'cli',
91+
beforeEach(() => {
92+
context = createNodesContextV2({
93+
nxJsonConfiguration: {},
94+
workspaceRoot: '',
95+
});
96+
});
97+
98+
afterEach(() => {
99+
vol.reset();
100+
});
101+
102+
it('should normalize context and use it to create the configuration target on ROOT project', async () => {
103+
const projectRoot = '.';
104+
const matchingFilesData = {
105+
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
106+
name: '@org/empty-root',
107+
})}`,
108+
};
109+
110+
await expect(
111+
invokeCreateNodesOnVirtualFilesV2(
112+
createNodesV2,
113+
context,
114+
{},
115+
{ matchingFilesData },
116+
),
117+
).resolves.toStrictEqual({
118+
[projectRoot]: {
119+
targets: {
120+
[`${CP_TARGET_NAME}--configuration`]: {
121+
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
100122
},
101123
},
102124
},
103-
},
125+
});
104126
});
105-
});
106127

107-
it('should create the executor target on PACKAGE project if configured', async () => {
108-
const projectRoot = 'apps/my-app';
109-
const matchingFilesData = {
110-
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
111-
name: '@org/empty-root',
112-
})}`,
113-
[`${projectRoot}/code-pushup.config.ts`]: '{}',
114-
};
128+
it('should create the executor target on PACKAGE project if configured', async () => {
129+
const projectRoot = 'apps/my-app';
130+
const matchingFilesData = {
131+
[`${projectRoot}/${PROJECT_JSON_FILE_NAME}`]: `${JSON.stringify({
132+
name: '@org/empty-root',
133+
})}`,
134+
[`${projectRoot}/code-pushup.config.ts`]: '{}',
135+
};
115136

116-
await expect(
117-
invokeCreateNodesOnVirtualFiles(
118-
createNodes,
119-
context,
120-
{
121-
projectPrefix: 'cli',
122-
},
123-
{ matchingFilesData },
124-
),
125-
).resolves.toStrictEqual({
126-
[projectRoot]: {
127-
targets: {
128-
[CP_TARGET_NAME]: {
129-
executor: `${PACKAGE_NAME}:cli`,
130-
options: {
131-
projectPrefix: 'cli',
137+
await expect(
138+
invokeCreateNodesOnVirtualFilesV2(
139+
createNodesV2,
140+
context,
141+
{
142+
projectPrefix: 'cli',
143+
},
144+
{ matchingFilesData },
145+
),
146+
).resolves.toStrictEqual({
147+
[projectRoot]: {
148+
targets: {
149+
[CP_TARGET_NAME]: {
150+
executor: `${PACKAGE_NAME}:cli`,
151+
options: {
152+
projectPrefix: 'cli',
153+
},
132154
},
133155
},
134156
},
135-
},
157+
});
136158
});
137159
});
138160
});

packages/nx-plugin/src/plugin/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import type {
88
ProjectConfigurationWithName,
99
} from './types.js';
1010

11+
/**
12+
* Normalize the context for a V1 or V2 Plugin.
13+
* @param context - The context for a V1 or V2 Plugin.
14+
* @param projectConfigurationFile - The project configuration file.
15+
* @param createOptions - The create options.
16+
* @returns The normalized context.
17+
*/
1118
export async function normalizedCreateNodesContext(
1219
context: CreateNodesContext | CreateNodesContextV2,
1320
projectConfigurationFile: string,

packages/nx-plugin/src/plugin/utils.unit.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { vol } from 'memfs';
22
import { describe, expect } from 'vitest';
3-
import { createNodesContext } from '@code-pushup/test-nx-utils';
3+
import { createNodesContextV2 } from '@code-pushup/test-nx-utils';
44
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
55
import { normalizedCreateNodesContext } from './utils.js';
66

@@ -15,7 +15,7 @@ describe('normalizedCreateNodesContext', () => {
1515

1616
await expect(
1717
normalizedCreateNodesContext(
18-
createNodesContext({ workspaceRoot: MEMFS_VOLUME }),
18+
createNodesContextV2({ workspaceRoot: MEMFS_VOLUME }),
1919
'project.json',
2020
),
2121
).resolves.toStrictEqual(
@@ -37,7 +37,7 @@ describe('normalizedCreateNodesContext', () => {
3737

3838
await expect(
3939
normalizedCreateNodesContext(
40-
createNodesContext(),
40+
createNodesContextV2(),
4141
'packages/utils/project.json',
4242
),
4343
).resolves.toStrictEqual(
@@ -59,7 +59,7 @@ describe('normalizedCreateNodesContext', () => {
5959

6060
await expect(
6161
normalizedCreateNodesContext(
62-
createNodesContext({
62+
createNodesContextV2({
6363
nxJsonConfiguration: {
6464
workspaceLayout: {
6565
libsDir: 'libs',
@@ -90,7 +90,7 @@ describe('normalizedCreateNodesContext', () => {
9090
);
9191

9292
await expect(
93-
normalizedCreateNodesContext(createNodesContext(), 'project.json'),
93+
normalizedCreateNodesContext(createNodesContextV2(), 'project.json'),
9494
).resolves.toStrictEqual(
9595
expect.objectContaining({
9696
projectJson: {
@@ -109,7 +109,7 @@ describe('normalizedCreateNodesContext', () => {
109109
);
110110

111111
await expect(
112-
normalizedCreateNodesContext(createNodesContext(), 'project.json'),
112+
normalizedCreateNodesContext(createNodesContextV2(), 'project.json'),
113113
).rejects.toThrow('Error parsing project.json file project.json.');
114114
});
115115

@@ -124,7 +124,7 @@ describe('normalizedCreateNodesContext', () => {
124124
);
125125

126126
await expect(
127-
normalizedCreateNodesContext(createNodesContext(), 'project.json'),
127+
normalizedCreateNodesContext(createNodesContextV2(), 'project.json'),
128128
).resolves.toStrictEqual(
129129
expect.objectContaining({
130130
createOptions: {
@@ -145,7 +145,7 @@ describe('normalizedCreateNodesContext', () => {
145145
);
146146

147147
await expect(
148-
normalizedCreateNodesContext(createNodesContext(), 'project.json', {
148+
normalizedCreateNodesContext(createNodesContextV2(), 'project.json', {
149149
projectPrefix: 'cli',
150150
}),
151151
).resolves.toStrictEqual(

0 commit comments

Comments
 (0)