|
| 1 | +import type { Tree } from '@nx/devkit'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration'; |
| 4 | +import { afterEach, expect } from 'vitest'; |
| 5 | +import { generateCodePushupConfig } from '@code-pushup/nx-plugin'; |
| 6 | +import { |
| 7 | + generateWorkspaceAndProject, |
| 8 | + materializeTree, |
| 9 | + nxShowProjectJson, |
| 10 | + nxTargetProject, |
| 11 | + registerPluginInWorkspace, |
| 12 | +} from '@code-pushup/test-nx-utils'; |
| 13 | +import { |
| 14 | + E2E_ENVIRONMENTS_DIR, |
| 15 | + TEST_OUTPUT_DIR, |
| 16 | + teardownTestFolder, |
| 17 | +} from '@code-pushup/test-utils'; |
| 18 | +import { INLINE_PLUGIN } from '../mocks/inline-plugin.js'; |
| 19 | + |
| 20 | +describe('nx-plugin pluginsConfig', () => { |
| 21 | + let tree: Tree; |
| 22 | + const project = 'my-lib'; |
| 23 | + const testFileDir = path.join( |
| 24 | + E2E_ENVIRONMENTS_DIR, |
| 25 | + nxTargetProject(), |
| 26 | + TEST_OUTPUT_DIR, |
| 27 | + 'plugin-plugins-config', |
| 28 | + ); |
| 29 | + |
| 30 | + beforeEach(async () => { |
| 31 | + tree = await generateWorkspaceAndProject(project); |
| 32 | + }); |
| 33 | + |
| 34 | + afterEach(async () => { |
| 35 | + await teardownTestFolder(testFileDir); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should apply pluginsConfig options to executor target', async () => { |
| 39 | + const cwd = path.join(testFileDir, 'plugins-config-applied'); |
| 40 | + const binPath = 'packages/cli/src/index.ts'; |
| 41 | + const configPath = '{projectRoot}/code-pushup.config.ts'; |
| 42 | + const projectPrefix = 'cli'; |
| 43 | + |
| 44 | + // Register plugin with options in the plugins array and pluginsConfig |
| 45 | + registerPluginInWorkspace( |
| 46 | + tree, |
| 47 | + { |
| 48 | + plugin: '@code-pushup/nx-plugin', |
| 49 | + options: { |
| 50 | + config: configPath, |
| 51 | + persist: { |
| 52 | + outputDir: '.code-pushup/{projectName}', |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + projectPrefix, |
| 58 | + bin: binPath, |
| 59 | + env: { |
| 60 | + NODE_OPTIONS: '--import tsx', |
| 61 | + TSX_TSCONFIG_PATH: 'tsconfig.base.json', |
| 62 | + }, |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + const { root } = readProjectConfiguration(tree, project); |
| 67 | + generateCodePushupConfig(tree, root, { |
| 68 | + plugins: [ |
| 69 | + { |
| 70 | + fileImports: '', |
| 71 | + codeStrings: INLINE_PLUGIN, |
| 72 | + }, |
| 73 | + ], |
| 74 | + }); |
| 75 | + |
| 76 | + await materializeTree(tree, cwd); |
| 77 | + |
| 78 | + const { code, projectJson } = await nxShowProjectJson(cwd, project); |
| 79 | + expect(code).toBe(0); |
| 80 | + |
| 81 | + expect(projectJson).toStrictEqual( |
| 82 | + expect.objectContaining({ |
| 83 | + targets: expect.objectContaining({ |
| 84 | + 'code-pushup': expect.objectContaining({ |
| 85 | + executor: '@code-pushup/nx-plugin:cli', |
| 86 | + options: expect.objectContaining({ |
| 87 | + projectPrefix, |
| 88 | + bin: binPath, |
| 89 | + env: { |
| 90 | + NODE_OPTIONS: '--import tsx', |
| 91 | + TSX_TSCONFIG_PATH: 'tsconfig.base.json', |
| 92 | + }, |
| 93 | + }), |
| 94 | + }), |
| 95 | + }), |
| 96 | + }), |
| 97 | + ); |
| 98 | + }); |
| 99 | +}); |
0 commit comments