test-common-tests.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. test("toBe", () => {
  2. expect(null).toBe(null);
  3. expect(undefined).toBe(undefined);
  4. expect(null).not.toBe(undefined);
  5. expect(1).toBe(1);
  6. expect(1).not.toBe(2);
  7. expect("1").toBe("1");
  8. expect("1").not.toBe("2");
  9. expect(true).toBeTrue();
  10. expect(true).not.toBeFalse();
  11. expect({}).not.toBe({});
  12. expect([]).not.toBe([]);
  13. function foo() {}
  14. expect(foo).toBe(foo);
  15. expect(function () {}).not.toBe(function () {});
  16. let s = Symbol("foo");
  17. expect(s).toBe(s);
  18. expect(Symbol("foo")).not.toBe(Symbol("foo"));
  19. expect(1n).toBe(1n);
  20. expect(1n).not.toBe(1);
  21. });
  22. test("toBeCloseTo", () => {
  23. expect(1).toBeCloseTo(1);
  24. expect(1).not.toBeCloseTo(1.1);
  25. expect(1).not.toBeCloseTo(1.01);
  26. expect(1).not.toBeCloseTo(1.001);
  27. expect(1).not.toBeCloseTo(1.0001);
  28. expect(1).not.toBeCloseTo(1.00001);
  29. expect(1).toBeCloseTo(1.000001);
  30. [
  31. ["foo", 1],
  32. [1, "foo"],
  33. [1n, 1],
  34. ].forEach(arr => {
  35. expect(() => {
  36. expect(arr[0]).toBeCloseTo(arr[1]);
  37. }).toThrow(ExpectationError);
  38. });
  39. });
  40. test("toHaveLength", () => {
  41. expect([]).toHaveLength(0);
  42. expect([]).not.toHaveLength(1);
  43. expect([1]).toHaveLength(1);
  44. expect({ length: 1 }).toHaveLength(1);
  45. expect(() => {
  46. expect(1).toHaveLength();
  47. }).toThrow(ExpectationError);
  48. });
  49. test("toHaveProperty", () => {
  50. expect([]).toHaveProperty("length");
  51. expect([]).toHaveProperty("length", 0);
  52. expect([1]).not.toHaveProperty("length", 0);
  53. expect({ foo: "bar" }).toHaveProperty("foo");
  54. expect({ foo: "bar" }).toHaveProperty("foo", "bar");
  55. expect({ foo: { bar: "baz" } }).toHaveProperty(["foo", "bar"]);
  56. expect({ foo: { bar: "baz" } }).toHaveProperty(["foo", "bar"], "baz");
  57. expect({ foo: { bar: "baz" } }).toHaveProperty("foo.bar");
  58. expect({ foo: { bar: "baz" } }).toHaveProperty("foo.bar", "baz");
  59. expect({ foo: { bar: "baz" } }).toHaveProperty(["foo", "bar"]);
  60. expect({ foo: { bar: "baz" } }).toHaveProperty(["foo", "bar"], "baz");
  61. expect({ foo: { bar: "baz" } }).not.toHaveProperty(["foo", "baz"]);
  62. expect({ foo: { bar: "baz" } }).not.toHaveProperty(["foo", "baz"], "qux");
  63. expect({ foo: { bar: "baz" } }).not.toHaveProperty("foo.baz");
  64. expect({ foo: { bar: "baz" } }).not.toHaveProperty("foo.baz", "qux");
  65. });
  66. test("toBeDefined", () => {
  67. expect(1).toBeDefined();
  68. expect(true).toBeDefined();
  69. expect(false).toBeDefined();
  70. expect({}).toBeDefined();
  71. expect([]).toBeDefined();
  72. expect("a").toBeDefined();
  73. expect(null).toBeDefined();
  74. expect(undefined).not.toBeDefined();
  75. });
  76. test("toBeInstanceOf", () => {
  77. expect(new Error()).toBeInstanceOf(Error);
  78. expect(Error).not.toBeInstanceOf(Error);
  79. class Parent {}
  80. class Child extends Parent {}
  81. expect(new Child()).toBeInstanceOf(Child);
  82. expect(new Child()).toBeInstanceOf(Parent);
  83. expect(new Parent()).toBeInstanceOf(Parent);
  84. expect(new Parent()).not.toBeInstanceOf(Child);
  85. });
  86. test("toBeNull", () => {
  87. expect(null).toBeNull();
  88. expect(undefined).not.toBeNull();
  89. expect(5).not.toBeNull();
  90. });
  91. test("toBeUndefined", () => {
  92. expect(undefined).toBeUndefined();
  93. expect(null).not.toBeUndefined();
  94. expect().toBeUndefined();
  95. expect(5).not.toBeUndefined();
  96. });
  97. test("toBeNaN", () => {
  98. expect(NaN).toBeNaN();
  99. expect(5).not.toBeNaN();
  100. });
  101. test("toBeTrue", () => {
  102. expect(true).toBeTrue();
  103. expect(false).not.toBeTrue();
  104. expect(null).not.toBeTrue();
  105. expect(undefined).not.toBeTrue();
  106. expect(0).not.toBeTrue();
  107. });
  108. test("toBeFalse", () => {
  109. expect(true).not.toBeFalse();
  110. expect(false).toBeFalse();
  111. expect(null).not.toBeFalse();
  112. expect(undefined).not.toBeFalse();
  113. expect(0).not.toBeFalse();
  114. });
  115. test("toBeLessThan", () => {
  116. expect(0).toBeLessThan(1);
  117. expect(0).toBeLessThan(0.1);
  118. expect(1).not.toBeLessThan(1);
  119. expect(1).not.toBeLessThan(0);
  120. expect(0n).toBeLessThan(1n);
  121. expect(1n).not.toBeLessThan(1n);
  122. expect(1n).not.toBeLessThan(0n);
  123. [
  124. ["foo", 0],
  125. [0, "foo"],
  126. [0, 0n],
  127. [0n, 0],
  128. ].forEach(arr => {
  129. expect(() => {
  130. expect(arr[0]).toBeLessThan(arr[1]);
  131. }).toThrow(ExpectationError);
  132. });
  133. });
  134. test("toBeLessThanOrEqual", () => {
  135. expect(0).toBeLessThanOrEqual(1);
  136. expect(0).toBeLessThanOrEqual(0.1);
  137. expect(1).toBeLessThanOrEqual(1);
  138. expect(1).not.toBeLessThanOrEqual(0);
  139. expect(0n).toBeLessThanOrEqual(1n);
  140. expect(1n).toBeLessThanOrEqual(1n);
  141. expect(1n).not.toBeLessThanOrEqual(0n);
  142. [
  143. ["foo", 0],
  144. [0, "foo"],
  145. [0, 0n],
  146. [0n, 0],
  147. ].forEach(arr => {
  148. expect(() => {
  149. expect(arr[0]).toBeLessThanOrEqual(arr[1]);
  150. }).toThrow(ExpectationError);
  151. });
  152. });
  153. test("toBeGreaterThan", () => {
  154. expect(1).toBeGreaterThan(0);
  155. expect(0.1).toBeGreaterThan(0);
  156. expect(1).not.toBeGreaterThan(1);
  157. expect(0).not.toBeGreaterThan(1);
  158. expect(1n).toBeGreaterThan(0n);
  159. expect(1n).not.toBeGreaterThan(1n);
  160. expect(0n).not.toBeGreaterThan(1n);
  161. [
  162. ["foo", 0],
  163. [0, "foo"],
  164. [0, 0n],
  165. [0n, 0],
  166. ].forEach(arr => {
  167. expect(() => {
  168. expect(arr[0]).toBeGreaterThan(arr[1]);
  169. }).toThrow(ExpectationError);
  170. });
  171. });
  172. test("toBeGreaterThanOrEqual", () => {
  173. expect(1).toBeGreaterThanOrEqual(0);
  174. expect(0.1).toBeGreaterThanOrEqual(0);
  175. expect(1).toBeGreaterThanOrEqual(1);
  176. expect(0).not.toBeGreaterThanOrEqual(1);
  177. expect(1n).toBeGreaterThanOrEqual(0n);
  178. expect(1n).toBeGreaterThanOrEqual(1n);
  179. expect(0n).not.toBeGreaterThanOrEqual(1n);
  180. [
  181. ["foo", 0],
  182. [0, "foo"],
  183. [0, 0n],
  184. [0n, 0],
  185. ].forEach(arr => {
  186. expect(() => {
  187. expect(arr[0]).toBeGreaterThanOrEqual(arr[1]);
  188. }).toThrow(ExpectationError);
  189. });
  190. });
  191. test("toContain", () => {
  192. expect([1, 2, 3]).toContain(1);
  193. expect([1, 2, 3]).toContain(2);
  194. expect([1, 2, 3]).toContain(3);
  195. expect([{ foo: 1 }]).not.toContain({ foo: 1 });
  196. });
  197. test("toContainEqual", () => {
  198. expect([1, 2, 3]).toContainEqual(1);
  199. expect([1, 2, 3]).toContainEqual(2);
  200. expect([1, 2, 3]).toContainEqual(3);
  201. expect([{ foo: 1 }]).toContainEqual({ foo: 1 });
  202. });
  203. test("toEqual", () => {
  204. expect(undefined).toEqual(undefined);
  205. expect(null).toEqual(null);
  206. expect(undefined).not.toEqual(null);
  207. expect(null).not.toEqual(undefined);
  208. expect(NaN).toEqual(NaN);
  209. expect(1).toEqual(1);
  210. expect("abcd").toEqual("abcd");
  211. let s = Symbol();
  212. expect(s).toEqual(s);
  213. expect(Symbol()).not.toEqual(Symbol());
  214. expect(Symbol.for("foo")).toEqual(Symbol.for("foo"));
  215. expect({ foo: 1, bar: { baz: [1, 2, 3] } }).toEqual({ foo: 1, bar: { baz: [1, 2, 3] } });
  216. expect([1, 2, { foo: 1 }, [3, [4, 5]]]).toEqual([1, 2, { foo: 1 }, [3, [4, 5]]]);
  217. function foo() {}
  218. expect(foo).toEqual(foo);
  219. expect(function () {}).not.toEqual(function () {});
  220. });
  221. test("toThrow", () => {
  222. expect(() => {}).not.toThrow();
  223. expect(() => {}).not.toThrow("foo");
  224. expect(() => {}).not.toThrow(TypeError);
  225. expect(() => {}).not.toThrow(new TypeError("foo"));
  226. let thrower = () => {
  227. throw new TypeError("foo bar");
  228. };
  229. expect(thrower).toThrow();
  230. expect(thrower).toThrow(TypeError);
  231. expect(thrower).toThrow("o ba");
  232. expect(thrower).toThrow("foo bar");
  233. expect(thrower).not.toThrow("baz");
  234. expect(thrower).not.toThrow(ReferenceError);
  235. expect(thrower).toThrow(new TypeError("foo bar"));
  236. expect(thrower).not.toThrow(new TypeError("o ba"));
  237. expect(thrower).toThrow(new ReferenceError("foo bar"));
  238. expect(thrower).toThrow({ message: "foo bar" });
  239. });
  240. test("pass", () => {
  241. expect().pass();
  242. expect({}).pass();
  243. });
  244. test("fail", () => {
  245. // FIXME: Doesn't really make sense; this is a great candidate
  246. // for expect.assertions()
  247. try {
  248. expect().fail();
  249. } catch (e) {
  250. expect(e.name).toBe("ExpectationError");
  251. }
  252. });
  253. test("toThrowWithMessage", () => {
  254. let incorrectUsages = [
  255. [1, undefined, undefined],
  256. [() => {}, undefined, undefined],
  257. [() => {}, function () {}, undefined],
  258. [() => {}, undefined, "test"],
  259. ];
  260. incorrectUsages.forEach(arr => {
  261. expect(() => {
  262. expect(arr[0]).toThrowWithMessage(arr[1], arr[2]);
  263. }).toThrow();
  264. });
  265. let thrower = () => {
  266. throw new TypeError("foo bar");
  267. };
  268. expect(thrower).toThrowWithMessage(TypeError, "foo bar");
  269. expect(thrower).toThrowWithMessage(TypeError, "foo");
  270. expect(thrower).toThrowWithMessage(TypeError, "o ba");
  271. expect(thrower).not.toThrowWithMessage(ReferenceError, "foo bar");
  272. expect(thrower).not.toThrowWithMessage(TypeError, "foo baz");
  273. });
  274. // FIXME: Will have to change when this matcher changes to use the
  275. // "eval" function
  276. test("toEval", () => {
  277. expect("let a = 1").toEval();
  278. expect("a < 1").not.toEval();
  279. expect("&&*^%#%@").not.toEval();
  280. expect("function foo() { return 1; }; return foo();").toEval();
  281. });
  282. // FIXME: Will have to change when this matcher changes to use the
  283. // "eval" function
  284. test("toEvalTo", () => {
  285. expect("let a = 1").toEvalTo();
  286. expect("let a = 1").toEvalTo(undefined);
  287. expect("return 10").toEvalTo(10);
  288. expect("return 10").not.toEvalTo(5);
  289. expect(() => {
  290. expect("*^&%%").not.toEvalTo();
  291. }).toThrow();
  292. });
  293. test("toHaveConfigurableProperty", () => {
  294. expect({ foo: 1 }).toHaveConfigurableProperty("foo");
  295. expect(() => {
  296. expect({ foo: 1 }).not.toHaveConfigurableProperty("bar");
  297. }).toThrow();
  298. let o = {};
  299. Object.defineProperty(o, "foo", { configurable: true, value: 1 });
  300. Object.defineProperty(o, "bar", { configurable: false, value: 1 });
  301. expect(o).toHaveConfigurableProperty("foo");
  302. expect(o).not.toHaveConfigurableProperty("bar");
  303. });
  304. test("toHaveEnumerableProperty", () => {
  305. expect({ foo: 1 }).toHaveEnumerableProperty("foo");
  306. expect(() => {
  307. expect({ foo: 1 }).not.toHaveEnumerableProperty("bar");
  308. }).toThrow();
  309. let o = {};
  310. Object.defineProperty(o, "foo", { enumerable: true, value: 1 });
  311. Object.defineProperty(o, "bar", { enumerable: false, value: 1 });
  312. expect(o).toHaveEnumerableProperty("foo");
  313. expect(o).not.toHaveEnumerableProperty("bar");
  314. });
  315. test("toHaveWritableProperty", () => {
  316. expect({ foo: 1 }).toHaveWritableProperty("foo");
  317. expect(() => {
  318. expect({ foo: 1 }).not.toHaveWritableProperty("bar");
  319. }).toThrow();
  320. let o = {};
  321. Object.defineProperty(o, "foo", { writable: true, value: 1 });
  322. Object.defineProperty(o, "bar", { writable: false, value: 1 });
  323. expect(o).toHaveWritableProperty("foo");
  324. expect(o).not.toHaveWritableProperty("bar");
  325. });
  326. test("toHaveGetterProperty", () => {
  327. expect(() => {
  328. expect({ foo: 1 }).not.toHaveGetterProperty("bar");
  329. }).toThrow();
  330. let o = {};
  331. Object.defineProperty(o, "foo", {
  332. get() {
  333. return 1;
  334. },
  335. });
  336. Object.defineProperty(o, "bar", { value: 1 });
  337. expect(o).toHaveGetterProperty("foo");
  338. expect(o).not.toHaveGetterProperty("bar");
  339. });
  340. test("toHaveSetterProperty", () => {
  341. expect(() => {
  342. expect({ foo: 1 }).not.toHaveSetterProperty("bar");
  343. }).toThrow();
  344. let o = {};
  345. Object.defineProperty(o, "foo", { set(_) {} });
  346. Object.defineProperty(o, "bar", { value: 1 });
  347. expect(o).toHaveSetterProperty("foo");
  348. expect(o).not.toHaveSetterProperty("bar");
  349. });