|
| 1 | +import { execSync } from 'child_process'; |
| 2 | +import { join, dirname } from 'path'; |
| 3 | +import { mkdirSync, rmSync } from 'fs'; |
| 4 | + |
| 5 | +describe('plugin-openapi', () => { |
| 6 | + let projectDirectory: string; |
| 7 | + |
| 8 | + beforeAll(() => { |
| 9 | + projectDirectory = createTestProject(); |
| 10 | + |
| 11 | + // The plugin has been built and published to a local registry in the jest globalSetup |
| 12 | + // Install the plugin built with the latest source code into the test repo |
| 13 | + execSync(`npm install @nx-plugin-openapi/plugin-openapi@e2e`, { |
| 14 | + cwd: projectDirectory, |
| 15 | + stdio: 'inherit', |
| 16 | + env: process.env, |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + afterAll(() => { |
| 21 | + // Cleanup the test project |
| 22 | + rmSync(projectDirectory, { |
| 23 | + recursive: true, |
| 24 | + force: true, |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should be installed', () => { |
| 29 | + // npm ls will fail if the package is not installed properly |
| 30 | + execSync('npm ls @nx-plugin-openapi/plugin-openapi', { |
| 31 | + cwd: projectDirectory, |
| 32 | + stdio: 'inherit', |
| 33 | + }); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +/** |
| 38 | + * Creates a test project with create-nx-workspace and installs the plugin |
| 39 | + * @returns The directory where the test project was created |
| 40 | + */ |
| 41 | +function createTestProject() { |
| 42 | + const projectName = 'test-project'; |
| 43 | + const projectDirectory = join(process.cwd(), 'tmp', projectName); |
| 44 | + |
| 45 | + // Ensure projectDirectory is empty |
| 46 | + rmSync(projectDirectory, { |
| 47 | + recursive: true, |
| 48 | + force: true, |
| 49 | + }); |
| 50 | + mkdirSync(dirname(projectDirectory), { |
| 51 | + recursive: true, |
| 52 | + }); |
| 53 | + |
| 54 | + execSync( |
| 55 | + `npx --yes create-nx-workspace@latest ${projectName} --preset apps --nxCloud=skip --no-interactive`, |
| 56 | + { |
| 57 | + cwd: dirname(projectDirectory), |
| 58 | + stdio: 'inherit', |
| 59 | + env: process.env, |
| 60 | + } |
| 61 | + ); |
| 62 | + console.log(`Created test project in "${projectDirectory}"`); |
| 63 | + |
| 64 | + return projectDirectory; |
| 65 | +} |
0 commit comments