in-operator-basic.js 968 B

1234567891011121314151617181920212223242526272829303132
  1. test("in operator with objects", () => {
  2. const o = { foo: "bar", bar: undefined };
  3. expect("" in o).toBeFalse();
  4. expect("foo" in o).toBeTrue();
  5. expect("bar" in o).toBeTrue();
  6. expect("baz" in o).toBeFalse();
  7. expect("toString" in o).toBeTrue();
  8. });
  9. test("in operator with arrays", () => {
  10. const a = ["hello", "friends"];
  11. expect(0 in a).toBeTrue();
  12. expect(1 in a).toBeTrue();
  13. expect(2 in a).toBeFalse();
  14. expect("0" in a).toBeTrue();
  15. expect("hello" in a).toBeFalse();
  16. expect("friends" in a).toBeFalse();
  17. expect("length" in a).toBeTrue();
  18. });
  19. test("in operator with string object", () => {
  20. const s = new String("foo");
  21. expect("length" in s).toBeTrue();
  22. });
  23. test("error when used with primitives", () => {
  24. ["foo", 123, null, undefined].forEach(value => {
  25. expect(() => {
  26. "prop" in value;
  27. }).toThrowWithMessage(TypeError, "'in' operator must be used on an object");
  28. });
  29. });