bigint-number-mix-errors.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. const doTest = (operatorName, executeOperation) => {
  2. [1, null, undefined].forEach(value => {
  3. const messageSuffix = operatorName === "unsigned right-shift" ? "" : " and other type";
  4. expect(() => {
  5. executeOperation(1n, value);
  6. }).toThrowWithMessage(
  7. TypeError,
  8. `Cannot use ${operatorName} operator with BigInt${messageSuffix}`
  9. );
  10. });
  11. };
  12. [
  13. ["addition", (a, b) => a + b],
  14. ["subtraction", (a, b) => a - b],
  15. ["multiplication", (a, b) => a * b],
  16. ["division", (a, b) => a / b],
  17. ["modulo", (a, b) => a % b],
  18. ["exponentiation", (a, b) => a ** b],
  19. ["bitwise OR", (a, b) => a | b],
  20. ["bitwise AND", (a, b) => a & b],
  21. ["bitwise XOR", (a, b) => a ^ b],
  22. ["left-shift", (a, b) => a << b],
  23. ["right-shift", (a, b) => a >> b],
  24. ["unsigned right-shift", (a, b) => a >>> b],
  25. ].forEach(testCase => {
  26. test(`using ${testCase[0]} operator with BigInt and other type`, () => {
  27. doTest(testCase[0], testCase[1]);
  28. });
  29. });