postwomanTesting.spec.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import runTestScriptWithVariables from "../postwomanTesting"
  2. /**
  3. * @param {string} script
  4. * @param {number} index
  5. */
  6. function getTestResult(script, index) {
  7. return runTestScriptWithVariables(script).testResults[index].result
  8. }
  9. /**
  10. * @param {string} script
  11. */
  12. function getErrors(script) {
  13. return runTestScriptWithVariables(script).errors
  14. }
  15. describe("Error handling", () => {
  16. test("throws error at unknown test method", () => {
  17. const testScriptWithUnknownMethod = "pw.expect(1).toBeSomeUnknownMethod()"
  18. expect(() => {
  19. runTestScriptWithVariables(testScriptWithUnknownMethod)
  20. }).toThrow()
  21. })
  22. test("errors array is empty on a successful test", () => {
  23. expect(getErrors("pw.expect(1).toBe(1)")).toStrictEqual([])
  24. })
  25. test("throws error at a variable which is not declared", () => {
  26. expect(() => {
  27. runTestScriptWithVariables("someVariable")
  28. }).toThrow()
  29. })
  30. })
  31. describe("toBe", () => {
  32. test("test for numbers", () => {
  33. expect(getTestResult("pw.expect(1).toBe(2)", 0)).toEqual("FAIL")
  34. expect(getTestResult("pw.expect(1).toBe(1)", 0)).toEqual("PASS")
  35. })
  36. test("test for strings", () => {
  37. expect(getTestResult("pw.expect('hello').toBe('bonjour')", 0)).toEqual(
  38. "FAIL"
  39. )
  40. expect(getTestResult("pw.expect('hi').toBe('hi')", 0)).toEqual("PASS")
  41. })
  42. test("test for negative assertion (.not.toBe)", () => {
  43. expect(getTestResult("pw.expect(1).not.toBe(1)", 0)).toEqual("FAIL")
  44. expect(getTestResult("pw.expect(1).not.toBe(2)", 0)).toEqual("PASS")
  45. expect(getTestResult("pw.expect('world').not.toBe('planet')", 0)).toEqual(
  46. "PASS"
  47. )
  48. expect(getTestResult("pw.expect('world').not.toBe('world')", 0)).toEqual(
  49. "FAIL"
  50. )
  51. })
  52. })
  53. describe("toHaveProperty", () => {
  54. const dummyResponse = {
  55. id: 843,
  56. description: "random",
  57. }
  58. test("test for positive assertion (.toHaveProperty)", () => {
  59. expect(
  60. getTestResult(
  61. `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("id")`,
  62. 0
  63. )
  64. ).toEqual("PASS")
  65. expect(
  66. getTestResult(`pw.expect(${dummyResponse.id}).toBe(843)`, 0)
  67. ).toEqual("PASS")
  68. })
  69. test("test for negative assertion (.not.toHaveProperty)", () => {
  70. expect(
  71. getTestResult(
  72. `pw.expect(${JSON.stringify(
  73. dummyResponse
  74. )}).not.toHaveProperty("type")`,
  75. 0
  76. )
  77. ).toEqual("PASS")
  78. expect(
  79. getTestResult(
  80. `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("type")`,
  81. 0
  82. )
  83. ).toEqual("FAIL")
  84. })
  85. })
  86. describe("toBeLevel2xx", () => {
  87. test("test for numbers", () => {
  88. expect(getTestResult("pw.expect(200).toBeLevel2xx()", 0)).toEqual("PASS")
  89. expect(getTestResult("pw.expect(200).not.toBeLevel2xx()", 0)).toEqual(
  90. "FAIL"
  91. )
  92. expect(getTestResult("pw.expect(300).toBeLevel2xx()", 0)).toEqual("FAIL")
  93. expect(getTestResult("pw.expect(300).not.toBeLevel2xx()", 0)).toEqual(
  94. "PASS"
  95. )
  96. })
  97. test("test for strings", () => {
  98. expect(getTestResult("pw.expect('200').toBeLevel2xx()", 0)).toEqual("PASS")
  99. expect(getTestResult("pw.expect('200').not.toBeLevel2xx()", 0)).toEqual(
  100. "FAIL"
  101. )
  102. expect(getTestResult("pw.expect('300').toBeLevel2xx()", 0)).toEqual("FAIL")
  103. expect(getTestResult("pw.expect('300').not.toBeLevel2xx()", 0)).toEqual(
  104. "PASS"
  105. )
  106. })
  107. test("failed to parse to integer", () => {
  108. expect(getTestResult("pw.expect(undefined).toBeLevel2xx()", 0)).toEqual(
  109. "FAIL"
  110. )
  111. expect(getTestResult("pw.expect(null).toBeLevel2xx()", 0)).toEqual("FAIL")
  112. expect(() => {
  113. runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel2xx()")
  114. }).toThrow()
  115. })
  116. })
  117. describe("toBeLevel3xx()", () => {
  118. test("test for numbers", () => {
  119. expect(getTestResult("pw.expect(300).toBeLevel3xx()", 0)).toEqual("PASS")
  120. expect(getTestResult("pw.expect(300).not.toBeLevel3xx()", 0)).toEqual(
  121. "FAIL"
  122. )
  123. expect(getTestResult("pw.expect(400).toBeLevel3xx()", 0)).toEqual("FAIL")
  124. expect(getTestResult("pw.expect(400).not.toBeLevel3xx()", 0)).toEqual(
  125. "PASS"
  126. )
  127. })
  128. test("test for strings", () => {
  129. expect(getTestResult("pw.expect('300').toBeLevel3xx()", 0)).toEqual("PASS")
  130. expect(getTestResult("pw.expect('300').not.toBeLevel3xx()", 0)).toEqual(
  131. "FAIL"
  132. )
  133. expect(getTestResult("pw.expect('400').toBeLevel3xx()", 0)).toEqual("FAIL")
  134. expect(getTestResult("pw.expect('400').not.toBeLevel3xx()", 0)).toEqual(
  135. "PASS"
  136. )
  137. })
  138. test("failed to parse to integer", () => {
  139. expect(getTestResult("pw.expect(undefined).toBeLevel3xx()", 0)).toEqual(
  140. "FAIL"
  141. )
  142. expect(getTestResult("pw.expect(null).toBeLevel3xx()", 0)).toEqual("FAIL")
  143. expect(() => {
  144. runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel3xx()")
  145. }).toThrow()
  146. })
  147. })
  148. describe("toBeLevel4xx()", () => {
  149. test("test for numbers", () => {
  150. expect(getTestResult("pw.expect(400).toBeLevel4xx()", 0)).toEqual("PASS")
  151. expect(getTestResult("pw.expect(400).not.toBeLevel4xx()", 0)).toEqual(
  152. "FAIL"
  153. )
  154. expect(getTestResult("pw.expect(500).toBeLevel4xx()", 0)).toEqual("FAIL")
  155. expect(getTestResult("pw.expect(500).not.toBeLevel4xx()", 0)).toEqual(
  156. "PASS"
  157. )
  158. })
  159. test("test for strings", () => {
  160. expect(getTestResult("pw.expect('400').toBeLevel4xx()", 0)).toEqual("PASS")
  161. expect(getTestResult("pw.expect('400').not.toBeLevel4xx()", 0)).toEqual(
  162. "FAIL"
  163. )
  164. expect(getTestResult("pw.expect('500').toBeLevel4xx()", 0)).toEqual("FAIL")
  165. expect(getTestResult("pw.expect('500').not.toBeLevel4xx()", 0)).toEqual(
  166. "PASS"
  167. )
  168. })
  169. test("failed to parse to integer", () => {
  170. expect(getTestResult("pw.expect(undefined).toBeLevel4xx()", 0)).toEqual(
  171. "FAIL"
  172. )
  173. expect(getTestResult("pw.expect(null).toBeLevel4xx()", 0)).toEqual("FAIL")
  174. expect(() => {
  175. runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel4xx()")
  176. }).toThrow()
  177. })
  178. })
  179. describe("toBeLevel5xx()", () => {
  180. test("test for numbers", () => {
  181. expect(getTestResult("pw.expect(500).toBeLevel5xx()", 0)).toEqual("PASS")
  182. expect(getTestResult("pw.expect(500).not.toBeLevel5xx()", 0)).toEqual(
  183. "FAIL"
  184. )
  185. expect(getTestResult("pw.expect(200).toBeLevel5xx()", 0)).toEqual("FAIL")
  186. expect(getTestResult("pw.expect(200).not.toBeLevel5xx()", 0)).toEqual(
  187. "PASS"
  188. )
  189. })
  190. test("test for strings", () => {
  191. expect(getTestResult("pw.expect('500').toBeLevel5xx()", 0)).toEqual("PASS")
  192. expect(getTestResult("pw.expect('500').not.toBeLevel5xx()", 0)).toEqual(
  193. "FAIL"
  194. )
  195. expect(getTestResult("pw.expect('200').toBeLevel5xx()", 0)).toEqual("FAIL")
  196. expect(getTestResult("pw.expect('200').not.toBeLevel5xx()", 0)).toEqual(
  197. "PASS"
  198. )
  199. })
  200. test("failed to parse to integer", () => {
  201. expect(getTestResult("pw.expect(undefined).toBeLevel5xx()", 0)).toEqual(
  202. "FAIL"
  203. )
  204. expect(getTestResult("pw.expect(null).toBeLevel5xx()", 0)).toEqual("FAIL")
  205. expect(() => {
  206. runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel5xx()")
  207. }).toThrow()
  208. })
  209. })
  210. describe("toHaveLength()", () => {
  211. test("test for strings", () => {
  212. expect(getTestResult("pw.expect('word').toHaveLength(4)", 0)).toEqual(
  213. "PASS"
  214. )
  215. expect(getTestResult("pw.expect('word').toHaveLength(5)", 0)).toEqual(
  216. "FAIL"
  217. )
  218. expect(getTestResult("pw.expect('word').not.toHaveLength(4)", 0)).toEqual(
  219. "FAIL"
  220. )
  221. expect(getTestResult("pw.expect('word').not.toHaveLength(5)", 0)).toEqual(
  222. "PASS"
  223. )
  224. })
  225. test("test for arrays", () => {
  226. const fruits =
  227. "['apples', 'bananas', 'oranges', 'grapes', 'strawberries', 'cherries']"
  228. expect(getTestResult(`pw.expect(${fruits}).toHaveLength(6)`, 0)).toEqual(
  229. "PASS"
  230. )
  231. expect(getTestResult(`pw.expect(${fruits}).toHaveLength(7)`, 0)).toEqual(
  232. "FAIL"
  233. )
  234. expect(
  235. getTestResult(`pw.expect(${fruits}).not.toHaveLength(6)`, 0)
  236. ).toEqual("FAIL")
  237. expect(
  238. getTestResult(`pw.expect(${fruits}).not.toHaveLength(7)`, 0)
  239. ).toEqual("PASS")
  240. })
  241. })
  242. describe("toBeType()", () => {
  243. test("test for positive assertion", () => {
  244. expect(getTestResult("pw.expect('random').toBeType('string')", 0)).toEqual(
  245. "PASS"
  246. )
  247. expect(getTestResult("pw.expect(true).toBeType('boolean')", 0)).toEqual(
  248. "PASS"
  249. )
  250. expect(getTestResult("pw.expect(5).toBeType('number')", 0)).toEqual("PASS")
  251. expect(
  252. getTestResult("pw.expect(new Date()).toBeType('object')", 0)
  253. ).toEqual("PASS")
  254. expect(
  255. getTestResult("pw.expect(undefined).toBeType('undefined')", 0)
  256. ).toEqual("PASS")
  257. expect(
  258. getTestResult("pw.expect(BigInt(123)).toBeType('bigint')", 0)
  259. ).toEqual("PASS")
  260. expect(
  261. getTestResult("pw.expect(Symbol('test')).toBeType('symbol')", 0)
  262. ).toEqual("PASS")
  263. expect(
  264. getTestResult("pw.expect(function() {}).toBeType('function')", 0)
  265. ).toEqual("PASS")
  266. })
  267. test("test for negative assertion", () => {
  268. expect(
  269. getTestResult("pw.expect('random').not.toBeType('string')", 0)
  270. ).toEqual("FAIL")
  271. expect(getTestResult("pw.expect(true).not.toBeType('boolean')", 0)).toEqual(
  272. "FAIL"
  273. )
  274. expect(getTestResult("pw.expect(5).not.toBeType('number')", 0)).toEqual(
  275. "FAIL"
  276. )
  277. expect(
  278. getTestResult("pw.expect(new Date()).not.toBeType('object')", 0)
  279. ).toEqual("FAIL")
  280. expect(
  281. getTestResult("pw.expect(undefined).not.toBeType('undefined')", 0)
  282. ).toEqual("FAIL")
  283. expect(
  284. getTestResult("pw.expect(BigInt(123)).not.toBeType('bigint')", 0)
  285. ).toEqual("FAIL")
  286. expect(
  287. getTestResult("pw.expect(Symbol('test')).not.toBeType('symbol')", 0)
  288. ).toEqual("FAIL")
  289. expect(
  290. getTestResult("pw.expect(function() {}).not.toBeType('function')", 0)
  291. ).toEqual("FAIL")
  292. })
  293. test("unexpected type", () => {
  294. expect(getTestResult("pw.expect('random').toBeType('unknown')", 0)).toEqual(
  295. "FAIL"
  296. )
  297. })
  298. })