|
| 1 | +import child_process from 'node:child_process'; |
| 2 | + |
| 3 | +import { |
| 4 | + dependencies as licenseKitDependenciesObj, |
| 5 | + devDependencies as licenseKitDevDependenciesObj, |
| 6 | +} from '../../../packages/license-kit/package.json'; |
| 7 | +import { |
| 8 | + dependencies as sharedDependenciesObj, |
| 9 | + devDependencies as sharedDevDependenciesObj, |
| 10 | +} from '../../../packages/shared/package.json'; |
| 11 | +import { dependencies as dependenciesObj, devDependencies as devDependenciesObj } from '../package.json'; |
| 12 | + |
| 13 | +const dependencies = Object.keys(dependenciesObj).sort(); |
| 14 | +const devDependencies = Object.keys(devDependenciesObj).sort(); |
| 15 | +const licenseKitDependencies = Object.keys(licenseKitDependenciesObj).sort(); |
| 16 | +const licenseKitDevDependencies = Object.keys(licenseKitDevDependenciesObj).sort(); |
| 17 | +const sharedDependencies = Object.keys(sharedDependenciesObj).sort(); |
| 18 | +const sharedDevDependencies = Object.keys(sharedDevDependenciesObj).sort(); |
| 19 | + |
| 20 | +async function runReportCommandForJsonOutput(args: string[] = []) { |
| 21 | + const command = `yarn workspace license-kit-node-example report${args ? ` ${args.join(' ')}` : ''}`; |
| 22 | + |
| 23 | + const output = await new Promise<string>((resolve) => { |
| 24 | + child_process.exec(command, (_, stdout) => { |
| 25 | + resolve(stdout); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + return JSON.parse(output); |
| 30 | +} |
| 31 | + |
| 32 | +describe('license-kit report', () => { |
| 33 | + it('including transitive deps, with dev deps with default settings', async () => { |
| 34 | + const json = await runReportCommandForJsonOutput(); |
| 35 | + |
| 36 | + expect(json['dhtmlx-gantt'].type).toMatch('GPL-2.0'); |
| 37 | + expect(json['is-even'].content).toMatch('MIT License'); |
| 38 | + expect(json['is-even'].type).toMatch('MIT'); |
| 39 | + expect(json.mariadb.content).toMatch('GNU LESSER GENERAL PUBLIC LICENSE'); |
| 40 | + expect(json.mariadb.type).toMatch('LGPL-2.1-or-later'); |
| 41 | + expect(json.zustand.content).toMatch('MIT License'); |
| 42 | + expect(json.zustand.type).toMatch('MIT'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('without transitive deps and without dev deps', async () => { |
| 46 | + const json = await runReportCommandForJsonOutput(['--dev-deps-mode', 'none', '--transitive-deps-mode', 'none']); |
| 47 | + |
| 48 | + expect(Object.keys(json).toSorted()).toEqual(dependencies.toSorted()); |
| 49 | + }); |
| 50 | + |
| 51 | + it('without transitive deps and with root-only dev deps', async () => { |
| 52 | + const json = await runReportCommandForJsonOutput([ |
| 53 | + '--dev-deps-mode', |
| 54 | + 'root-only', |
| 55 | + '--transitive-deps-mode', |
| 56 | + 'none', |
| 57 | + ]); |
| 58 | + |
| 59 | + expect(Object.keys(json).toSorted()).toEqual(Array.from(new Set([...dependencies, ...devDependencies])).toSorted()); |
| 60 | + }); |
| 61 | + |
| 62 | + it('with root-only dev deps and with transitive dependencies of workspace-specifier-only dependencies', async () => { |
| 63 | + const json = await runReportCommandForJsonOutput([ |
| 64 | + '--dev-deps-mode', |
| 65 | + 'root-only', |
| 66 | + '--transitive-deps-mode', |
| 67 | + 'from-workspace-only', |
| 68 | + ]); |
| 69 | + |
| 70 | + expect(Object.keys(json).toSorted()).toEqual( |
| 71 | + Array.from(new Set([...dependencies, ...devDependencies, ...licenseKitDependencies])).toSorted(), |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it('without dev deps and with transitive dependencies of external dependencies only', async () => { |
| 76 | + const json = await runReportCommandForJsonOutput([ |
| 77 | + '--dev-deps-mode', |
| 78 | + 'none', |
| 79 | + '--transitive-deps-mode', |
| 80 | + 'from-external-only', |
| 81 | + ]); |
| 82 | + |
| 83 | + const resultKeys = Object.keys(json); |
| 84 | + |
| 85 | + expect(resultKeys.toSorted()).toEqual([ |
| 86 | + '@types/geojson', |
| 87 | + '@types/node', |
| 88 | + 'denque', |
| 89 | + 'dhtmlx-gantt', |
| 90 | + 'iconv-lite', |
| 91 | + 'is-even', |
| 92 | + 'is-number', |
| 93 | + 'is-odd', |
| 94 | + 'lru-cache', |
| 95 | + 'mariadb', |
| 96 | + 'safer-buffer', |
| 97 | + 'undici-types', |
| 98 | + 'yallist', |
| 99 | + 'zustand', |
| 100 | + ]); |
| 101 | + |
| 102 | + expect(resultKeys).not.toIncludeAllMembers(licenseKitDependencies); |
| 103 | + expect(resultKeys).not.toIncludeAllMembers(licenseKitDevDependencies); |
| 104 | + }); |
| 105 | + |
| 106 | + it('with root-only dev deps and with transitive dependencies of external dependencies only', async () => { |
| 107 | + const json = await runReportCommandForJsonOutput([ |
| 108 | + '--dev-deps-mode', |
| 109 | + 'root-only', |
| 110 | + '--transitive-deps-mode', |
| 111 | + 'from-external-only', |
| 112 | + ]); |
| 113 | + |
| 114 | + const resultKeys = Object.keys(json); |
| 115 | + |
| 116 | + expect(resultKeys).toContain('license-kit'); |
| 117 | + |
| 118 | + // this time, the result should also include all dependencies of the direct devDependencies of the root package.json |
| 119 | + expect(resultKeys.length).toBeGreaterThan(200); |
| 120 | + expect(resultKeys).toIncludeAllMembers([ |
| 121 | + '@babel/plugin-transform-async-to-generator', |
| 122 | + '@babel/plugin-transform-block-scoped-functions', |
| 123 | + '@babel/plugin-transform-block-scoping', |
| 124 | + '@babel/plugin-transform-class-properties', |
| 125 | + '@babel/helper-create-class-features-plugin', |
| 126 | + '@babel/helper-member-expression-to-functions', |
| 127 | + ]); |
| 128 | + |
| 129 | + expect(resultKeys).not.toIncludeAllMembers(licenseKitDependencies); |
| 130 | + expect(resultKeys).not.toIncludeAllMembers(licenseKitDevDependencies); |
| 131 | + |
| 132 | + // note: sharedDependencies should not be included, yet they contain glob, which is a transitive dependency of something else |
| 133 | + expect(resultKeys).not.toIncludeAllMembers(sharedDevDependencies); |
| 134 | + }); |
| 135 | + |
| 136 | + it('with root-only dev deps and with all transitive dependencies', async () => { |
| 137 | + const json = await runReportCommandForJsonOutput(['--dev-deps-mode', 'root-only', '--transitive-deps-mode', 'all']); |
| 138 | + |
| 139 | + const resultKeys = Object.keys(json); |
| 140 | + |
| 141 | + // this time, the result should also include all transitive dependencies of direct devDependencies of the root package.json, |
| 142 | + // such as that of license-kit itself (which is a direct devDependency of the root package.json) |
| 143 | + expect(resultKeys.length).toBeGreaterThan(300); |
| 144 | + expect(resultKeys).toContain('license-kit'); |
| 145 | + expect(resultKeys).toContain('@callstack/react-native-legal-shared'); |
| 146 | + |
| 147 | + expect(resultKeys).toIncludeAllMembers(licenseKitDependencies); |
| 148 | + expect(resultKeys).not.toIncludeAllMembers(licenseKitDevDependencies); |
| 149 | + |
| 150 | + expect(resultKeys).toIncludeAllMembers(sharedDependencies); |
| 151 | + expect(resultKeys).not.toIncludeAllMembers(sharedDevDependencies); |
| 152 | + }); |
| 153 | +}); |
0 commit comments