Instant.fromEpochMicroseconds.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. describe("correct behavior", () => {
  2. test("length is 1", () => {
  3. expect(Temporal.Instant.fromEpochMicroseconds).toHaveLength(1);
  4. });
  5. test("basic functionality", () => {
  6. expect(Temporal.Instant.fromEpochMicroseconds(0n).epochMicroseconds).toBe(0n);
  7. expect(Temporal.Instant.fromEpochMicroseconds(1n).epochMicroseconds).toBe(1n);
  8. expect(Temporal.Instant.fromEpochMicroseconds(999_999_999n).epochMicroseconds).toBe(
  9. 999_999_999n
  10. );
  11. expect(
  12. Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_000n).epochMicroseconds
  13. ).toBe(8_640_000_000_000_000_000n);
  14. expect(Temporal.Instant.fromEpochMicroseconds(-0n).epochMicroseconds).toBe(0n);
  15. expect(Temporal.Instant.fromEpochMicroseconds(-1n).epochMicroseconds).toBe(-1n);
  16. expect(Temporal.Instant.fromEpochMicroseconds(-999_999_999n).epochMicroseconds).toBe(
  17. -999_999_999n
  18. );
  19. expect(
  20. Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_000n).epochMicroseconds
  21. ).toBe(-8_640_000_000_000_000_000n);
  22. });
  23. });
  24. describe("errors", () => {
  25. test("argument must be coercible to BigInt", () => {
  26. expect(() => {
  27. Temporal.Instant.fromEpochMicroseconds(123);
  28. }).toThrowWithMessage(TypeError, "Cannot convert number to BigInt");
  29. expect(() => {
  30. Temporal.Instant.fromEpochMicroseconds("foo");
  31. }).toThrowWithMessage(SyntaxError, "Invalid value for BigInt: foo");
  32. });
  33. test("out-of-range epoch microseconds value", () => {
  34. expect(() => {
  35. Temporal.Instant.fromEpochMicroseconds(8_640_000_000_000_000_001n);
  36. }).toThrowWithMessage(
  37. RangeError,
  38. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  39. );
  40. expect(() => {
  41. Temporal.Instant.fromEpochMicroseconds(-8_640_000_000_000_000_001n);
  42. }).toThrowWithMessage(
  43. RangeError,
  44. "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
  45. );
  46. });
  47. });