Instant.fromEpochMilliseconds.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.Instant.fromEpochMilliseconds).toHaveLength(1);
  4. });
  5. test("basic functionality", () => {
  6. expect(Temporal.Instant.fromEpochMilliseconds(0).epochMilliseconds).toBe(0);
  7. expect(Temporal.Instant.fromEpochMilliseconds(1).epochMilliseconds).toBe(1);
  8. expect(Temporal.Instant.fromEpochMilliseconds(999_999_999).epochMilliseconds).toBe(
  9. 999_999_999
  10. );
  11. expect(
  12. Temporal.Instant.fromEpochMilliseconds(8_640_000_000_000_000).epochMilliseconds
  13. ).toBe(8_640_000_000_000_000);
  14. expect(Temporal.Instant.fromEpochMilliseconds(-0).epochMilliseconds).toBe(0);
  15. expect(Temporal.Instant.fromEpochMilliseconds(-1).epochMilliseconds).toBe(-1);
  16. expect(Temporal.Instant.fromEpochMilliseconds(-999_999_999).epochMilliseconds).toBe(
  17. -999_999_999
  18. );
  19. expect(
  20. Temporal.Instant.fromEpochMilliseconds(-8_640_000_000_000_000).epochMilliseconds
  21. ).toBe(-8_640_000_000_000_000);
  22. });
  23. });
  24. test("errors", () => {
  25. test("argument must be coercible to BigInt", () => {
  26. expect(() => {
  27. Temporal.Instant.fromEpochMilliseconds(1.23);
  28. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  29. // NOTE: ToNumber is called on the argument first, so this is effectively NaN.
  30. expect(() => {
  31. Temporal.Instant.fromEpochMilliseconds("foo");
  32. }).toThrowWithMessage(RangeError, "Cannot convert non-integral number to BigInt");
  33. });
  34. test("out-of-range epoch milliseconds value", () => {
  35. expect(() => {
  36. Temporal.Instant.fromEpochMilliseconds(8_640_000_000_000_001);
  37. }).toThrowWithMessage(
  38. RangeError,
  39. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  40. );
  41. expect(() => {
  42. Temporal.Instant.fromEpochMilliseconds(-8_640_000_000_000_001);
  43. }).toThrowWithMessage(
  44. RangeError,
  45. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  46. );
  47. });
  48. });