test-common-tests.js 12 KB

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