resolve-packages.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. const path = require("path");
  3. function mockPromise(value) {
  4. const isValueAPromise = (value || {}).then;
  5. const mockedPromise = {
  6. then: function(callback) {
  7. return mockPromise(callback(value));
  8. }
  9. };
  10. return isValueAPromise ? value : mockedPromise;
  11. }
  12. function spawnChild(pkg) {
  13. return pkg;
  14. }
  15. function getLoc(option) {
  16. let packageModule = [];
  17. option.filter(pkg => {
  18. mockPromise(spawnChild(pkg)).then(_ => {
  19. try {
  20. let loc = path.join("..", "..", "node_modules", pkg);
  21. packageModule.push(loc);
  22. } catch (err) {
  23. throw new Error(
  24. "Package wasn't validated correctly.." + "Submit an issue for",
  25. pkg,
  26. "if this persists"
  27. );
  28. }
  29. });
  30. return packageModule;
  31. });
  32. return packageModule;
  33. }
  34. describe("resolve-packages", () => {
  35. let moduleLoc;
  36. afterEach(() => {
  37. moduleLoc = null;
  38. });
  39. it("should resolve a location of a published module", () => {
  40. moduleLoc = getLoc(["webpack-addons-ylvis"]);
  41. expect(moduleLoc).toEqual([
  42. path.normalize("../../node_modules/webpack-addons-ylvis")
  43. ]);
  44. });
  45. it("should be empty if argument is blank", () => {
  46. // normally caught before getting resolved
  47. moduleLoc = getLoc([" "]);
  48. expect(moduleLoc).toEqual([path.normalize("../../node_modules/ ")]);
  49. });
  50. it("should resolve multiple locations of published modules", () => {
  51. /* we're testing multiple paths here. At Github this up for discussion, because if
  52. * we validate each package on each run, we can catch and build the questions in init gradually
  53. * while we get one filepath at the time. If not, this is a workaround.
  54. */
  55. moduleLoc = getLoc(["webpack-addons-ylvis", "webpack-addons-noop"]);
  56. expect(moduleLoc).toEqual([
  57. path.normalize("../../node_modules/webpack-addons-ylvis"),
  58. path.normalize("../../node_modules/webpack-addons-noop")
  59. ]);
  60. });
  61. });