Promise.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. test("length is 1", () => {
  2. expect(Promise).toHaveLength(1);
  3. });
  4. describe("errors", () => {
  5. test("must be called as constructor", () => {
  6. expect(() => {
  7. Promise();
  8. }).toThrowWithMessage(TypeError, "Promise constructor must be called with 'new'");
  9. });
  10. test("executor must be a function", () => {
  11. expect(() => {
  12. new Promise();
  13. }).toThrowWithMessage(TypeError, "Promise executor must be a function");
  14. });
  15. });
  16. describe("normal behavior", () => {
  17. test("returns a Promise object", () => {
  18. const promise = new Promise(() => {});
  19. expect(promise).toBeInstanceOf(Promise);
  20. expect(typeof promise).toBe("object");
  21. });
  22. test("executor is called with resolve and reject functions", () => {
  23. let resolveFunction = null;
  24. let rejectFunction = null;
  25. new Promise((resolve, reject) => {
  26. resolveFunction = resolve;
  27. rejectFunction = reject;
  28. });
  29. expect(typeof resolveFunction).toBe("function");
  30. expect(typeof rejectFunction).toBe("function");
  31. expect(resolveFunction).not.toBe(rejectFunction);
  32. });
  33. });