|
| 1 | +import 'nx/src/internal-testing-utils/mock-project-graph'; |
| 2 | + |
| 3 | +import { Tree } from '@nx/devkit'; |
| 4 | +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; |
| 5 | +import { pluginGenerator } from '../plugin/plugin'; |
| 6 | +import { createNodesGenerator } from './create-nodes'; |
| 7 | +import { setCwd } from '@nx/devkit/internal-testing-utils'; |
| 8 | + |
| 9 | +describe('create-nodes generator', () => { |
| 10 | + let tree: Tree; |
| 11 | + let projectName: string; |
| 12 | + |
| 13 | + beforeEach(async () => { |
| 14 | + projectName = 'my-plugin'; |
| 15 | + tree = createTreeWithEmptyWorkspace(); |
| 16 | + setCwd(''); |
| 17 | + await pluginGenerator(tree, { |
| 18 | + directory: projectName, |
| 19 | + unitTestRunner: 'jest', |
| 20 | + linter: 'eslint', |
| 21 | + compiler: 'tsc', |
| 22 | + includeCreateNodes: false, |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should generate files', async () => { |
| 27 | + await createNodesGenerator(tree, { |
| 28 | + name: 'my-plugin', |
| 29 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 30 | + unitTestRunner: 'jest', |
| 31 | + }); |
| 32 | + |
| 33 | + expect( |
| 34 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.ts') |
| 35 | + ).toBeTruthy(); |
| 36 | + expect( |
| 37 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.spec.ts') |
| 38 | + ).toBeTruthy(); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should create README when it does not exist', async () => { |
| 42 | + await createNodesGenerator(tree, { |
| 43 | + name: 'my-plugin', |
| 44 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 45 | + unitTestRunner: 'jest', |
| 46 | + }); |
| 47 | + |
| 48 | + const readmePath = 'my-plugin/README.md'; |
| 49 | + expect(tree.exists(readmePath)).toBeTruthy(); |
| 50 | + |
| 51 | + const readmeContent = tree.read(readmePath, 'utf-8'); |
| 52 | + expect(readmeContent).toContain('## What is an Inference Plugin?'); |
| 53 | + expect(readmeContent).toContain('## How This Plugin Works'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should append to existing README without duplicating content', async () => { |
| 57 | + const readmePath = 'my-plugin/README.md'; |
| 58 | + tree.write(readmePath, '# My Plugin\n\nExisting content'); |
| 59 | + |
| 60 | + await createNodesGenerator(tree, { |
| 61 | + name: 'my-plugin', |
| 62 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 63 | + unitTestRunner: 'jest', |
| 64 | + }); |
| 65 | + |
| 66 | + const readmeContent = tree.read(readmePath, 'utf-8'); |
| 67 | + expect(readmeContent).toContain('# My Plugin'); |
| 68 | + expect(readmeContent).toContain('Existing content'); |
| 69 | + expect(readmeContent).toContain('## What is an Inference Plugin?'); |
| 70 | + |
| 71 | + // Run generator again to ensure no duplication |
| 72 | + await createNodesGenerator(tree, { |
| 73 | + name: 'another-plugin', |
| 74 | + path: 'my-plugin/src/plugins/another-plugin/plugin', |
| 75 | + unitTestRunner: 'jest', |
| 76 | + }); |
| 77 | + |
| 78 | + const updatedContent = tree.read(readmePath, 'utf-8'); |
| 79 | + const matches = updatedContent.match(/## What is an Inference Plugin\?/g); |
| 80 | + expect(matches?.length).toBe(1); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should skip README when skipReadme is true', async () => { |
| 84 | + await createNodesGenerator(tree, { |
| 85 | + name: 'my-plugin', |
| 86 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 87 | + unitTestRunner: 'jest', |
| 88 | + skipReadme: true, |
| 89 | + }); |
| 90 | + |
| 91 | + // README exists from plugin generator, but should not contain our inference plugin content |
| 92 | + const readmePath = 'my-plugin/README.md'; |
| 93 | + expect(tree.exists(readmePath)).toBeTruthy(); |
| 94 | + const readmeContent = tree.read(readmePath, 'utf-8'); |
| 95 | + expect(readmeContent).not.toContain('## What is an Inference Plugin?'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should skip test file when unitTestRunner is none', async () => { |
| 99 | + await createNodesGenerator(tree, { |
| 100 | + name: 'my-plugin', |
| 101 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 102 | + unitTestRunner: 'none', |
| 103 | + }); |
| 104 | + |
| 105 | + expect( |
| 106 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.ts') |
| 107 | + ).toBeTruthy(); |
| 108 | + expect( |
| 109 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.spec.ts') |
| 110 | + ).toBeFalsy(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should use custom targetName', async () => { |
| 114 | + await createNodesGenerator(tree, { |
| 115 | + name: 'my-plugin', |
| 116 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 117 | + unitTestRunner: 'jest', |
| 118 | + targetName: 'custom-target', |
| 119 | + }); |
| 120 | + |
| 121 | + const pluginContent = tree.read( |
| 122 | + 'my-plugin/src/plugins/my-plugin/plugin.ts', |
| 123 | + 'utf-8' |
| 124 | + ); |
| 125 | + expect(pluginContent).toContain( |
| 126 | + "targetName: options.targetName ?? 'custom-target'" |
| 127 | + ); |
| 128 | + expect(pluginContent).toContain("@default 'custom-target'"); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should use custom configFile pattern', async () => { |
| 132 | + await createNodesGenerator(tree, { |
| 133 | + name: 'my-plugin', |
| 134 | + path: 'my-plugin/src/plugins/my-plugin/plugin', |
| 135 | + unitTestRunner: 'jest', |
| 136 | + configFile: '**/custom.config.json', |
| 137 | + }); |
| 138 | + |
| 139 | + const pluginContent = tree.read( |
| 140 | + 'my-plugin/src/plugins/my-plugin/plugin.ts', |
| 141 | + 'utf-8' |
| 142 | + ); |
| 143 | + expect(pluginContent).toContain("'**/custom.config.json'"); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should handle path with file extension', async () => { |
| 147 | + await createNodesGenerator(tree, { |
| 148 | + name: 'my-plugin', |
| 149 | + path: 'my-plugin/src/plugins/my-plugin/plugin.ts', |
| 150 | + unitTestRunner: 'jest', |
| 151 | + }); |
| 152 | + |
| 153 | + expect( |
| 154 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.ts') |
| 155 | + ).toBeTruthy(); |
| 156 | + expect( |
| 157 | + tree.exists('my-plugin/src/plugins/my-plugin/plugin.spec.ts') |
| 158 | + ).toBeTruthy(); |
| 159 | + }); |
| 160 | +}); |
0 commit comments