test-common-tests.js 11 KB

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