test-common.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. let describe;
  2. let test;
  3. let expect;
  4. // Stores the results of each test and suite. Has a terrible
  5. // name to avoid name collision.
  6. let __TestResults__ = {};
  7. // So test names like "toString" don't automatically produce an error
  8. Object.setPrototypeOf(__TestResults__, null);
  9. // This array is used to communicate with the C++ program. It treats
  10. // each message in this array as a separate message. Has a terrible
  11. // name to avoid name collision.
  12. let __UserOutput__ = [];
  13. // We also rebind console.log here to use the array above
  14. console.log = (...args) => {
  15. __UserOutput__.push(args.join(" "));
  16. };
  17. class ExpectationError extends Error {
  18. constructor(message, fileName, lineNumber) {
  19. super(message, fileName, lineNumber);
  20. this.name = "ExpectationError";
  21. }
  22. }
  23. // Use an IIFE to avoid polluting the global namespace as much as possible
  24. (() => {
  25. // FIXME: This is a very naive deepEquals algorithm
  26. const deepEquals = (a, b) => {
  27. if (Array.isArray(a)) return Array.isArray(b) && deepArrayEquals(a, b);
  28. if (typeof a === "object") return typeof b === "object" && deepObjectEquals(a, b);
  29. return Object.is(a, b);
  30. };
  31. const deepArrayEquals = (a, b) => {
  32. if (a.length !== b.length) return false;
  33. for (let i = 0; i < a.length; ++i) {
  34. if (!deepEquals(a[i], b[i])) return false;
  35. }
  36. return true;
  37. };
  38. const deepObjectEquals = (a, b) => {
  39. if (a === null) return b === null;
  40. for (let key of Reflect.ownKeys(a)) {
  41. if (!deepEquals(a[key], b[key])) return false;
  42. }
  43. return true;
  44. };
  45. class Expector {
  46. constructor(target, inverted) {
  47. this.target = target;
  48. this.inverted = !!inverted;
  49. }
  50. get not() {
  51. return new Expector(this.target, !this.inverted);
  52. }
  53. toBe(value) {
  54. this.__doMatcher(() => {
  55. this.__expect(Object.is(this.target, value),
  56. () => ("toBe: expected _" + String(value) + "_, got _" + String(this.target) + "_"));
  57. });
  58. }
  59. // FIXME: Take a precision argument like jest's toBeCloseTo matcher
  60. toBeCloseTo(value) {
  61. this.__expect(typeof this.target === "number", () => "toBeCloseTo: target not of type number");
  62. this.__expect(typeof value === "number", () => "toBeCloseTo: argument not of type number");
  63. this.__doMatcher(() => {
  64. this.__expect(Math.abs(this.target - value) < 0.000001);
  65. });
  66. }
  67. toHaveLength(length) {
  68. this.__expect(typeof this.target.length === "number", () => "toHaveLength: target.length not of type number");
  69. this.__doMatcher(() => {
  70. this.__expect(Object.is(this.target.length, length));
  71. });
  72. }
  73. toHaveProperty(property, value) {
  74. this.__doMatcher(() => {
  75. let object = this.target;
  76. if (typeof property === "string" && property.includes(".")) {
  77. let propertyArray = [];
  78. while (property.includes(".")) {
  79. let index = property.indexOf(".");
  80. propertyArray.push(property.substring(0, index));
  81. if (index + 1 >= property.length) break;
  82. property = property.substring(index + 1, property.length);
  83. }
  84. propertyArray.push(property);
  85. property = propertyArray;
  86. }
  87. if (Array.isArray(property)) {
  88. for (let key of property) {
  89. this.__expect(object !== undefined && object !== null);
  90. object = object[key];
  91. }
  92. } else {
  93. object = object[property];
  94. }
  95. this.__expect(object !== undefined);
  96. if (value !== undefined) this.__expect(deepEquals(object, value));
  97. });
  98. }
  99. toBeDefined() {
  100. this.__doMatcher(() => {
  101. this.__expect(this.target !== undefined, () => "toBeDefined: target was undefined");
  102. });
  103. }
  104. toBeInstanceOf(class_) {
  105. this.__doMatcher(() => {
  106. this.__expect(this.target instanceof class_);
  107. });
  108. }
  109. toBeNull() {
  110. this.__doMatcher(() => {
  111. this.__expect(this.target === null);
  112. });
  113. }
  114. toBeUndefined() {
  115. this.__doMatcher(() => {
  116. this.__expect(this.target === undefined, () => "toBeUndefined: target was not undefined");
  117. });
  118. }
  119. toBeNaN() {
  120. this.__doMatcher(() => {
  121. this.__expect(isNaN(this.target), () => ("toBeNaN: target was _" + String(this.target) + "_, not NaN"));
  122. });
  123. }
  124. toBeTrue() {
  125. this.__doMatcher(() => {
  126. this.__expect(this.target === true);
  127. });
  128. }
  129. toBeFalse() {
  130. this.__doMatcher(() => {
  131. this.__expect(this.target === false);
  132. });
  133. }
  134. __validateNumericComparisonTypes(value) {
  135. this.__expect(typeof this.target === "number" || typeof this.target === "bigint");
  136. this.__expect(typeof value === "number" || typeof value === "bigint");
  137. this.__expect(typeof this.target === typeof value);
  138. }
  139. toBeLessThan(value) {
  140. this.__validateNumericComparisonTypes(value);
  141. this.__doMatcher(() => {
  142. this.__expect(this.target < value);
  143. });
  144. }
  145. toBeLessThanOrEqual(value) {
  146. this.__validateNumericComparisonTypes(value);
  147. this.__doMatcher(() => {
  148. this.__expect(this.target <= value);
  149. });
  150. }
  151. toBeGreaterThan(value) {
  152. this.__validateNumericComparisonTypes(value);
  153. this.__doMatcher(() => {
  154. this.__expect(this.target > value);
  155. });
  156. }
  157. toBeGreaterThanOrEqual(value) {
  158. this.__validateNumericComparisonTypes(value);
  159. this.__doMatcher(() => {
  160. this.__expect(this.target >= value);
  161. });
  162. }
  163. toContain(item) {
  164. this.__doMatcher(() => {
  165. for (let element of this.target) {
  166. if (item === element) return;
  167. }
  168. throw new ExpectationError();
  169. });
  170. }
  171. toContainEqual(item) {
  172. this.__doMatcher(() => {
  173. for (let element of this.target) {
  174. if (deepEquals(item, element)) return;
  175. }
  176. throw new ExpectationError();
  177. });
  178. }
  179. toEqual(value) {
  180. this.__doMatcher(() => {
  181. this.__expect(deepEquals(this.target, value));
  182. });
  183. }
  184. toThrow(value) {
  185. this.__expect(typeof this.target === "function");
  186. this.__expect(
  187. typeof value === "string" ||
  188. typeof value === "function" ||
  189. typeof value === "object" ||
  190. value === undefined
  191. );
  192. this.__doMatcher(() => {
  193. let threw = true;
  194. try {
  195. this.target();
  196. threw = false;
  197. } catch (e) {
  198. if (typeof value === "string") {
  199. this.__expect(e.message.includes(value));
  200. } else if (typeof value === "function") {
  201. this.__expect(e instanceof value);
  202. } else if (typeof value === "object") {
  203. this.__expect(e.message === value.message);
  204. }
  205. }
  206. this.__expect(threw);
  207. });
  208. }
  209. pass(message) {
  210. // FIXME: This does nothing. If we want to implement things
  211. // like assertion count, this will have to do something
  212. }
  213. // jest-extended
  214. fail(message) {
  215. this.__doMatcher(() => {
  216. this.__expect(false, message);
  217. });
  218. }
  219. // jest-extended
  220. toThrowWithMessage(class_, message) {
  221. this.__expect(typeof this.target === "function");
  222. this.__expect(class_ !== undefined);
  223. this.__expect(message !== undefined);
  224. this.__doMatcher(() => {
  225. try {
  226. this.target();
  227. this.__expect(false);
  228. } catch (e) {
  229. this.__expect(e instanceof class_);
  230. this.__expect(e.message.includes(message));
  231. }
  232. });
  233. }
  234. // Test for syntax errors; target must be a string
  235. toEval() {
  236. this.__expect(typeof this.target === "string");
  237. let threw = false;
  238. try {
  239. new Function(this.target);
  240. } catch (e) {
  241. threw = true;
  242. }
  243. this.__expect(this.inverted ? threw : !threw);
  244. }
  245. // Must compile regardless of inverted-ness
  246. toEvalTo(value) {
  247. this.__expect(typeof this.target === "string");
  248. let result;
  249. try {
  250. result = new Function(this.target)();
  251. } catch (e) {
  252. throw new ExpectationError();
  253. }
  254. this.__doMatcher(() => {
  255. this.__expect(deepEquals(value, result));
  256. });
  257. }
  258. toHaveConfigurableProperty(property) {
  259. this.__expect(this.target !== undefined && this.target !== null);
  260. let d = Object.getOwnPropertyDescriptor(this.target, property);
  261. this.__expect(d !== undefined);
  262. this.__doMatcher(() => {
  263. this.__expect(d.configurable);
  264. });
  265. }
  266. toHaveEnumerableProperty(property) {
  267. this.__expect(this.target !== undefined && this.target !== null);
  268. let d = Object.getOwnPropertyDescriptor(this.target, property);
  269. this.__expect(d !== undefined);
  270. this.__doMatcher(() => {
  271. this.__expect(d.enumerable);
  272. });
  273. }
  274. toHaveWritableProperty(property) {
  275. this.__expect(this.target !== undefined && this.target !== null);
  276. let d = Object.getOwnPropertyDescriptor(this.target, property);
  277. this.__expect(d !== undefined);
  278. this.__doMatcher(() => {
  279. this.__expect(d.writable);
  280. });
  281. }
  282. toHaveValueProperty(property, value) {
  283. this.__expect(this.target !== undefined && this.target !== null);
  284. let d = Object.getOwnPropertyDescriptor(this.target, property);
  285. this.__expect(d !== undefined);
  286. this.__doMatcher(() => {
  287. this.__expect(d.value !== undefined);
  288. if (value !== undefined) this.__expect(deepEquals(value, d.value));
  289. });
  290. }
  291. toHaveGetterProperty(property) {
  292. this.__expect(this.target !== undefined && this.target !== null);
  293. let d = Object.getOwnPropertyDescriptor(this.target, property);
  294. this.__expect(d !== undefined);
  295. this.__doMatcher(() => {
  296. this.__expect(d.get !== undefined);
  297. });
  298. }
  299. toHaveSetterProperty(property) {
  300. this.__expect(this.target !== undefined && this.target !== null);
  301. let d = Object.getOwnPropertyDescriptor(this.target, property);
  302. this.__expect(d !== undefined);
  303. this.__doMatcher(() => {
  304. this.__expect(d.set !== undefined);
  305. });
  306. }
  307. __doMatcher(matcher) {
  308. if (!this.inverted) {
  309. matcher();
  310. } else {
  311. let threw = false;
  312. try {
  313. matcher();
  314. } catch (e) {
  315. if (e.name === "ExpectationError") threw = true;
  316. }
  317. if (!threw) throw new ExpectationError("not: test didn't fail");
  318. }
  319. }
  320. __expect(value, details) {
  321. if (value !== true) {
  322. if (details !== undefined)
  323. throw new ExpectationError(details());
  324. else
  325. throw new ExpectationError();
  326. }
  327. }
  328. }
  329. expect = value => new Expector(value);
  330. // describe is able to lump test results inside of it by using this context
  331. // variable. Top level tests have the default suite message
  332. const defaultSuiteMessage = "__$$TOP_LEVEL$$__";
  333. let suiteMessage = defaultSuiteMessage;
  334. describe = (message, callback) => {
  335. suiteMessage = message;
  336. callback();
  337. suiteMessage = defaultSuiteMessage;
  338. };
  339. test = (message, callback) => {
  340. if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
  341. const suite = __TestResults__[suiteMessage];
  342. if (suite[message]) {
  343. suite[message] = {
  344. result: "fail",
  345. };
  346. return;
  347. }
  348. try {
  349. callback();
  350. suite[message] = {
  351. result: "pass",
  352. };
  353. } catch (e) {
  354. suite[message] = {
  355. result: "fail",
  356. details: String(e),
  357. };
  358. }
  359. };
  360. test.skip = (message, callback) => {
  361. if (typeof callback !== "function")
  362. throw new Error("test.skip has invalid second argument (must be a function)");
  363. if (!__TestResults__[suiteMessage]) __TestResults__[suiteMessage] = {};
  364. const suite = __TestResults__[suiteMessage];
  365. if (suite[message]) throw new Error("Duplicate test name: " + message);
  366. suite[message] = {
  367. result: "skip",
  368. };
  369. };
  370. })();