Skip to content

Commit dd857f5

Browse files
committed
test: fix nx-plugin tests and inline custom dummy plugin
1 parent 9972999 commit dd857f5

File tree

6 files changed

+40
-41
lines changed

6 files changed

+40
-41
lines changed

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { type Tree, updateProjectConfiguration } from '@nx/devkit';
2-
import { join, relative } from 'node:path';
2+
import { join } from 'node:path';
33
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
44
import { afterEach, expect } from 'vitest';
55
import { generateCodePushupConfig } from '@code-pushup/nx-plugin';
@@ -12,21 +12,16 @@ import { teardownTestFolder } from '@code-pushup/test-setup';
1212
import {
1313
E2E_ENVIRONMENTS_DIR,
1414
TEST_OUTPUT_DIR,
15-
osAgnosticPath,
1615
removeColorCodes,
1716
} from '@code-pushup/test-utils';
1817
import { executeProcess, readJsonFile } from '@code-pushup/utils';
19-
20-
function relativePathToCwd(testDir: string): string {
21-
return relative(join(process.cwd(), testDir), process.cwd());
22-
}
18+
import { INLINE_PLUGIN } from './inline-plugin.js';
2319

2420
async function addTargetToWorkspace(
2521
tree: Tree,
2622
options: { cwd: string; project: string },
2723
) {
2824
const { cwd, project } = options;
29-
const pathRelativeToPackage = relative(join(cwd, 'libs', project), cwd);
3025
const projectCfg = readProjectConfiguration(tree, project);
3126
updateProjectConfiguration(tree, project, {
3227
...projectCfg,
@@ -39,18 +34,10 @@ async function addTargetToWorkspace(
3934
});
4035
const { root } = projectCfg;
4136
generateCodePushupConfig(tree, root, {
42-
fileImports: `import type {CoreConfig} from "@code-pushup/models";`,
4337
plugins: [
4438
{
45-
// @TODO replace with inline plugin
46-
fileImports: `import {customPlugin} from "${osAgnosticPath(
47-
join(
48-
relativePathToCwd(cwd),
49-
pathRelativeToPackage,
50-
'dist/testing/test-utils',
51-
),
52-
)}";`,
53-
codeStrings: 'customPlugin()',
39+
fileImports: '',
40+
codeStrings: INLINE_PLUGIN,
5441
},
5542
],
5643
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export const INLINE_PLUGIN = `
2+
{
3+
slug: 'good-feels',
4+
title: 'Good feels',
5+
icon: 'javascript',
6+
audits: [
7+
{
8+
slug: 'always-perfect',
9+
title: 'Always perfect',
10+
},
11+
],
12+
runner: () => [
13+
{
14+
slug: 'always-perfect',
15+
score: 1,
16+
value: 100,
17+
displayValue: '✅ Perfect! 👌',
18+
},
19+
],
20+
}
21+
`;

e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Tree } from '@nx/devkit';
2-
import { join, relative } from 'node:path';
2+
import { join } from 'node:path';
33
import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration';
44
import { afterEach, expect } from 'vitest';
55
import { generateCodePushupConfig } from '@code-pushup/nx-plugin';
@@ -17,6 +17,7 @@ import {
1717
removeColorCodes,
1818
} from '@code-pushup/test-utils';
1919
import { executeProcess, readTextFile } from '@code-pushup/utils';
20+
import { INLINE_PLUGIN } from './inline-plugin.js';
2021

2122
describe('nx-plugin', () => {
2223
let tree: Tree;
@@ -45,7 +46,7 @@ describe('nx-plugin', () => {
4546
const { code, projectJson } = await nxShowProjectJson(cwd, project);
4647
expect(code).toBe(0);
4748

48-
expect(projectJson.targets).toStrictEqual({
49+
expect(projectJson.targets).toEqual({
4950
['code-pushup--configuration']: {
5051
configurations: {},
5152
executor: 'nx:run-commands',
@@ -168,28 +169,21 @@ describe('nx-plugin', () => {
168169

169170
it('should execute dynamic executor target', async () => {
170171
const cwd = join(testFileDir, 'execute-dynamic-executor');
171-
const pathRelativeToPackage = relative(join(cwd, 'libs', project), cwd);
172172
registerPluginInWorkspace(tree, {
173173
plugin: '@code-pushup/nx-plugin',
174174
});
175175
const { root } = readProjectConfiguration(tree, project);
176176
generateCodePushupConfig(tree, root, {
177-
fileImports: `import type {CoreConfig} from "@code-pushup/models";`,
178177
plugins: [
179178
{
180-
// @TODO replace with inline plugin
181-
fileImports: `import {customPlugin} from "${join(
182-
relative(join(process.cwd(), cwd), process.cwd()),
183-
pathRelativeToPackage,
184-
'dist/testing/test-utils',
185-
)}";`,
186-
codeStrings: 'customPlugin()',
179+
fileImports: '',
180+
codeStrings: INLINE_PLUGIN,
187181
},
188182
],
189183
upload: {
190184
server: 'https://api.staging.code-pushup.dev/graphql',
191185
organization: 'code-pushup',
192-
apiKey: '12345678',
186+
apiKey: 'cp_12345678',
193187
},
194188
});
195189

packages/nx-plugin/src/generators/configuration/code-pushup-config.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,10 @@ export function generateCodePushupConfig(
5858
fileImports: formatArrayToLinesOfJsString(configFileImports),
5959
persist: formatObjectToFormattedJsString(persist),
6060
upload: formatObjectToFormattedJsString(upload),
61-
plugins: formatArrayToJSArray(
62-
plugins.flatMap(({ codeStrings }) => codeStrings),
63-
),
61+
plugins: `[${plugins.map(({ codeStrings }) => codeStrings).join(',')}]`,
6462
categories:
6563
categories &&
66-
formatArrayToJSArray(
67-
categories.flatMap(({ codeStrings }) => codeStrings),
68-
),
64+
`[${categories.map(({ codeStrings }) => codeStrings).join(',')}]`,
6965
});
7066
}
7167
}

packages/nx-plugin/src/generators/configuration/code-pushup-config.unit.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ describe('generateCodePushupConfig options', () => {
168168
upload: {
169169
organization: 'code-pushup',
170170
project: 'cli',
171-
server: 'https://code-pushup.dev/portal',
172-
apiKey: '12345678',
171+
server: 'https://api.staging.code-pushup.dev/graphql',
172+
apiKey: 'cp_12345678',
173173
},
174174
});
175175
expect(generateFilesSpy).toHaveBeenCalledWith(
@@ -180,8 +180,8 @@ describe('generateCodePushupConfig options', () => {
180180
upload: formatObjectToFormattedJsString({
181181
organization: 'code-pushup',
182182
project: 'cli',
183-
server: 'https://code-pushup.dev/portal',
184-
apiKey: '12345678',
183+
server: 'https://api.staging.code-pushup.dev/graphql',
184+
apiKey: 'cp_12345678',
185185
}),
186186
}),
187187
);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@nx/devkit';
99
import { libraryGenerator } from '@nx/js';
1010
import type { LibraryGeneratorSchema } from '@nx/js/src/utils/schema';
11+
import { join } from 'node:path';
1112
import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/create-tree-with-empty-workspace';
1213
import { executeProcess } from '@code-pushup/utils';
1314

@@ -45,14 +46,14 @@ export async function generateWorkspaceAndProject(
4546
typeof options === 'string' ? { name: options } : options;
4647
await libraryGenerator(tree, {
4748
name,
48-
directory: 'libs',
49+
directory: join('libs', name),
4950
tags: 'scope:plugin',
5051
linter: 'none',
5152
unitTestRunner: 'none',
5253
testEnvironment: 'node',
5354
buildable: false,
5455
publishable: false,
55-
projectNameAndRootFormat: 'derived',
56+
projectNameAndRootFormat: 'as-provided',
5657
...normalizedOptions,
5758
});
5859

0 commit comments

Comments
 (0)