package-manager.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. const path = require("path");
  3. jest.mock("cross-spawn");
  4. jest.mock("fs");
  5. describe("package-manager", () => {
  6. const packageManager = require("./package-manager");
  7. const spawn = require("cross-spawn");
  8. const fs = require("fs");
  9. const defaultSyncResult = {
  10. pid: 1234,
  11. output: [null, null, null],
  12. stdout: null,
  13. stderr: null,
  14. signal: null,
  15. status: 1,
  16. error: null
  17. };
  18. function mockSpawnErrorOnce() {
  19. spawn.sync.mockReturnValueOnce(
  20. Object.assign({}, defaultSyncResult, {
  21. status: null,
  22. error: new Error()
  23. })
  24. );
  25. }
  26. function mockSpawnErrorTwice() {
  27. mockSpawnErrorOnce();
  28. mockSpawnErrorOnce();
  29. }
  30. function mockUpdateYarnOnce() {
  31. fs.existsSync.mockReturnValueOnce(false);
  32. fs.existsSync.mockReturnValueOnce(true);
  33. fs.existsSync.mockReturnValueOnce(false);
  34. fs.existsSync.mockReturnValueOnce(true);
  35. fs.existsSync.mockReturnValueOnce(true);
  36. }
  37. function mockUpdateNPMOnce() {
  38. fs.existsSync.mockReturnValueOnce(true);
  39. fs.existsSync.mockReturnValueOnce(false);
  40. fs.existsSync.mockReturnValueOnce(true);
  41. fs.existsSync.mockReturnValueOnce(true);
  42. fs.existsSync.mockReturnValueOnce(true);
  43. }
  44. spawn.sync.mockReturnValue(defaultSyncResult);
  45. it("should return 'yarn' from getPackageManager if it's installed", () => {
  46. expect(packageManager.getPackageManager()).toEqual("yarn");
  47. });
  48. it("should return 'npm' from getPackageManager if yarn is not installed", () => {
  49. mockSpawnErrorOnce();
  50. expect(packageManager.getPackageManager()).toEqual("npm");
  51. });
  52. it("should spawn yarn add from spawnChild", () => {
  53. const packageName = "some-pkg";
  54. packageManager.spawnChild(packageName);
  55. expect(spawn.sync).toHaveBeenLastCalledWith(
  56. "yarn",
  57. ["global", "add", packageName],
  58. { stdio: "inherit" }
  59. );
  60. });
  61. it("should spawn yarn upgrade from spawnChild", () => {
  62. const packageName = "some-pkg";
  63. mockUpdateYarnOnce();
  64. packageManager.spawnChild(packageName);
  65. expect(spawn.sync).toHaveBeenLastCalledWith(
  66. "yarn",
  67. ["global", "upgrade", packageName],
  68. { stdio: "inherit" }
  69. );
  70. });
  71. it("should spawn npm install from spawnChild", () => {
  72. const packageName = "some-pkg";
  73. mockSpawnErrorTwice();
  74. packageManager.spawnChild(packageName);
  75. expect(spawn.sync).toHaveBeenLastCalledWith(
  76. "npm",
  77. ["install", "-g", packageName],
  78. { stdio: "inherit" }
  79. );
  80. });
  81. it("should spawn npm update from spawnChild", () => {
  82. const packageName = "some-pkg";
  83. mockUpdateNPMOnce();
  84. packageManager.spawnChild(packageName);
  85. expect(spawn.sync).toHaveBeenLastCalledWith(
  86. "npm",
  87. ["update", "-g", packageName],
  88. { stdio: "inherit" }
  89. );
  90. });
  91. it("should return the yarn global dir from getPathToGlobalPackages if yarn is installed", () => {
  92. const yarnDir = "/Users/test/.config/yarn/global";
  93. // Mock confirmation that yarn is installed
  94. spawn.sync.mockReturnValueOnce(defaultSyncResult);
  95. // Mock stdout of `yarn global dir`
  96. spawn.sync.mockReturnValueOnce({
  97. stdout: {
  98. toString: () => `${yarnDir}\n`
  99. }
  100. });
  101. const globalPath = packageManager.getPathToGlobalPackages();
  102. const expected = path.join(yarnDir, "node_modules");
  103. expect(globalPath).toBe(expected);
  104. });
  105. it("should return the npm global dir from getPathToGlobalPackages if yarn is not installed", () => {
  106. mockSpawnErrorOnce();
  107. const globalPath = packageManager.getPathToGlobalPackages();
  108. expect(globalPath).toBe(require("global-modules"));
  109. });
  110. });