|
| 1 | +import { processNpmWorkspaces } from '../../lib'; |
| 2 | +import * as path from 'path'; |
| 3 | + |
| 4 | +describe('process npm workspaces', () => { |
| 5 | + const originalCurrentWorkingDirectory = process.cwd(); |
| 6 | + |
| 7 | + afterEach(() => { |
| 8 | + process.chdir(originalCurrentWorkingDirectory); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('showNpmScope feature flag forwarding', () => { |
| 12 | + it('should forward showNpmScope flag when set to true', async () => { |
| 13 | + const fixturePath = path.resolve( |
| 14 | + __dirname, |
| 15 | + '..', |
| 16 | + 'fixtures', |
| 17 | + 'workspace-multi-type', |
| 18 | + ); |
| 19 | + process.chdir(fixturePath); |
| 20 | + const currentDir = process.cwd(); |
| 21 | + |
| 22 | + const result = await processNpmWorkspaces( |
| 23 | + currentDir, |
| 24 | + { showNpmScope: true }, |
| 25 | + [ |
| 26 | + `${currentDir}/npm-workspace/package-lock.json`, |
| 27 | + `${currentDir}/npm-workspace/package.json`, |
| 28 | + `${currentDir}/npm-workspace/packages/a/package.json`, |
| 29 | + `${currentDir}/npm-workspace/packages/b/package.json`, |
| 30 | + ], |
| 31 | + ); |
| 32 | + |
| 33 | + expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces'); |
| 34 | + expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1); |
| 35 | + expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({}); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should forward showNpmScope flag when set to false', async () => { |
| 39 | + const fixturePath = path.resolve( |
| 40 | + __dirname, |
| 41 | + '..', |
| 42 | + 'fixtures', |
| 43 | + 'workspace-multi-type', |
| 44 | + ); |
| 45 | + process.chdir(fixturePath); |
| 46 | + const currentDir = process.cwd(); |
| 47 | + |
| 48 | + const result = await processNpmWorkspaces( |
| 49 | + currentDir, |
| 50 | + { showNpmScope: false }, |
| 51 | + [ |
| 52 | + `${currentDir}/npm-workspace/package-lock.json`, |
| 53 | + `${currentDir}/npm-workspace/package.json`, |
| 54 | + `${currentDir}/npm-workspace/packages/a/package.json`, |
| 55 | + `${currentDir}/npm-workspace/packages/b/package.json`, |
| 56 | + ], |
| 57 | + ); |
| 58 | + |
| 59 | + expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces'); |
| 60 | + expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1); |
| 61 | + expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({}); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should work correctly when showNpmScope is undefined (backward compatibility)', async () => { |
| 65 | + const fixturePath = path.resolve( |
| 66 | + __dirname, |
| 67 | + '..', |
| 68 | + 'fixtures', |
| 69 | + 'workspace-multi-type', |
| 70 | + ); |
| 71 | + process.chdir(fixturePath); |
| 72 | + const currentDir = process.cwd(); |
| 73 | + |
| 74 | + const result = await processNpmWorkspaces(currentDir, {}, [ |
| 75 | + `${currentDir}/npm-workspace/package-lock.json`, |
| 76 | + `${currentDir}/npm-workspace/package.json`, |
| 77 | + `${currentDir}/npm-workspace/packages/a/package.json`, |
| 78 | + `${currentDir}/npm-workspace/packages/b/package.json`, |
| 79 | + ]); |
| 80 | + |
| 81 | + expect(result.plugin.name).toEqual('snyk-nodejs-npm-workspaces'); |
| 82 | + expect(result.scannedProjects.length).toBeGreaterThanOrEqual(1); |
| 83 | + expect(result.scannedProjects[0].depGraph?.toJSON()).not.toEqual({}); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments