test-common-tests.js 12 KB

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