123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- import runTestScriptWithVariables from "../postwomanTesting"
- /**
- * @param {string} script
- * @param {number} index
- */
- function getTestResult(script, index) {
- return runTestScriptWithVariables(script).testResults[index].result
- }
- /**
- * @param {string} script
- */
- function getErrors(script) {
- return runTestScriptWithVariables(script).errors
- }
- describe("Error handling", () => {
- test("throws error at unknown test method", () => {
- const testScriptWithUnknownMethod = "pw.expect(1).toBeSomeUnknownMethod()"
- expect(() => {
- runTestScriptWithVariables(testScriptWithUnknownMethod)
- }).toThrow()
- })
- test("errors array is empty on a successful test", () => {
- expect(getErrors("pw.expect(1).toBe(1)")).toStrictEqual([])
- })
- test("throws error at a variable which is not declared", () => {
- expect(() => {
- runTestScriptWithVariables("someVariable")
- }).toThrow()
- })
- })
- describe("toBe", () => {
- test("test for numbers", () => {
- expect(getTestResult("pw.expect(1).toBe(2)", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(1).toBe(1)", 0)).toEqual("PASS")
- })
- test("test for strings", () => {
- expect(getTestResult("pw.expect('hello').toBe('bonjour')", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('hi').toBe('hi')", 0)).toEqual("PASS")
- })
- test("test for negative assertion (.not.toBe)", () => {
- expect(getTestResult("pw.expect(1).not.toBe(1)", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(1).not.toBe(2)", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect('world').not.toBe('planet')", 0)).toEqual(
- "PASS"
- )
- expect(getTestResult("pw.expect('world').not.toBe('world')", 0)).toEqual(
- "FAIL"
- )
- })
- })
- describe("toHaveProperty", () => {
- const dummyResponse = {
- id: 843,
- description: "random",
- }
- test("test for positive assertion (.toHaveProperty)", () => {
- expect(
- getTestResult(
- `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("id")`,
- 0
- )
- ).toEqual("PASS")
- expect(
- getTestResult(`pw.expect(${dummyResponse.id}).toBe(843)`, 0)
- ).toEqual("PASS")
- })
- test("test for negative assertion (.not.toHaveProperty)", () => {
- expect(
- getTestResult(
- `pw.expect(${JSON.stringify(
- dummyResponse
- )}).not.toHaveProperty("type")`,
- 0
- )
- ).toEqual("PASS")
- expect(
- getTestResult(
- `pw.expect(${JSON.stringify(dummyResponse)}).toHaveProperty("type")`,
- 0
- )
- ).toEqual("FAIL")
- })
- })
- describe("toBeLevel2xx", () => {
- test("test for numbers", () => {
- expect(getTestResult("pw.expect(200).toBeLevel2xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect(200).not.toBeLevel2xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(300).toBeLevel2xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(300).not.toBeLevel2xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("test for strings", () => {
- expect(getTestResult("pw.expect('200').toBeLevel2xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect('200').not.toBeLevel2xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('300').toBeLevel2xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect('300').not.toBeLevel2xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("failed to parse to integer", () => {
- expect(getTestResult("pw.expect(undefined).toBeLevel2xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(null).toBeLevel2xx()", 0)).toEqual("FAIL")
- expect(() => {
- runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel2xx()")
- }).toThrow()
- })
- })
- describe("toBeLevel3xx()", () => {
- test("test for numbers", () => {
- expect(getTestResult("pw.expect(300).toBeLevel3xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect(300).not.toBeLevel3xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(400).toBeLevel3xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(400).not.toBeLevel3xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("test for strings", () => {
- expect(getTestResult("pw.expect('300').toBeLevel3xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect('300').not.toBeLevel3xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('400').toBeLevel3xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect('400').not.toBeLevel3xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("failed to parse to integer", () => {
- expect(getTestResult("pw.expect(undefined).toBeLevel3xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(null).toBeLevel3xx()", 0)).toEqual("FAIL")
- expect(() => {
- runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel3xx()")
- }).toThrow()
- })
- })
- describe("toBeLevel4xx()", () => {
- test("test for numbers", () => {
- expect(getTestResult("pw.expect(400).toBeLevel4xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect(400).not.toBeLevel4xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(500).toBeLevel4xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(500).not.toBeLevel4xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("test for strings", () => {
- expect(getTestResult("pw.expect('400').toBeLevel4xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect('400').not.toBeLevel4xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('500').toBeLevel4xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect('500').not.toBeLevel4xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("failed to parse to integer", () => {
- expect(getTestResult("pw.expect(undefined).toBeLevel4xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(null).toBeLevel4xx()", 0)).toEqual("FAIL")
- expect(() => {
- runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel4xx()")
- }).toThrow()
- })
- })
- describe("toBeLevel5xx()", () => {
- test("test for numbers", () => {
- expect(getTestResult("pw.expect(500).toBeLevel5xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect(500).not.toBeLevel5xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(200).toBeLevel5xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect(200).not.toBeLevel5xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("test for strings", () => {
- expect(getTestResult("pw.expect('500').toBeLevel5xx()", 0)).toEqual("PASS")
- expect(getTestResult("pw.expect('500').not.toBeLevel5xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('200').toBeLevel5xx()", 0)).toEqual("FAIL")
- expect(getTestResult("pw.expect('200').not.toBeLevel5xx()", 0)).toEqual(
- "PASS"
- )
- })
- test("failed to parse to integer", () => {
- expect(getTestResult("pw.expect(undefined).toBeLevel5xx()", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(null).toBeLevel5xx()", 0)).toEqual("FAIL")
- expect(() => {
- runTestScriptWithVariables("pw.expect(Symbol('test')).toBeLevel5xx()")
- }).toThrow()
- })
- })
- describe("toHaveLength()", () => {
- test("test for strings", () => {
- expect(getTestResult("pw.expect('word').toHaveLength(4)", 0)).toEqual(
- "PASS"
- )
- expect(getTestResult("pw.expect('word').toHaveLength(5)", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('word').not.toHaveLength(4)", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect('word').not.toHaveLength(5)", 0)).toEqual(
- "PASS"
- )
- })
- test("test for arrays", () => {
- const fruits =
- "['apples', 'bananas', 'oranges', 'grapes', 'strawberries', 'cherries']"
- expect(getTestResult(`pw.expect(${fruits}).toHaveLength(6)`, 0)).toEqual(
- "PASS"
- )
- expect(getTestResult(`pw.expect(${fruits}).toHaveLength(7)`, 0)).toEqual(
- "FAIL"
- )
- expect(
- getTestResult(`pw.expect(${fruits}).not.toHaveLength(6)`, 0)
- ).toEqual("FAIL")
- expect(
- getTestResult(`pw.expect(${fruits}).not.toHaveLength(7)`, 0)
- ).toEqual("PASS")
- })
- })
- describe("toBeType()", () => {
- test("test for positive assertion", () => {
- expect(getTestResult("pw.expect('random').toBeType('string')", 0)).toEqual(
- "PASS"
- )
- expect(getTestResult("pw.expect(true).toBeType('boolean')", 0)).toEqual(
- "PASS"
- )
- expect(getTestResult("pw.expect(5).toBeType('number')", 0)).toEqual("PASS")
- expect(
- getTestResult("pw.expect(new Date()).toBeType('object')", 0)
- ).toEqual("PASS")
- expect(
- getTestResult("pw.expect(undefined).toBeType('undefined')", 0)
- ).toEqual("PASS")
- expect(
- getTestResult("pw.expect(BigInt(123)).toBeType('bigint')", 0)
- ).toEqual("PASS")
- expect(
- getTestResult("pw.expect(Symbol('test')).toBeType('symbol')", 0)
- ).toEqual("PASS")
- expect(
- getTestResult("pw.expect(function() {}).toBeType('function')", 0)
- ).toEqual("PASS")
- })
- test("test for negative assertion", () => {
- expect(
- getTestResult("pw.expect('random').not.toBeType('string')", 0)
- ).toEqual("FAIL")
- expect(getTestResult("pw.expect(true).not.toBeType('boolean')", 0)).toEqual(
- "FAIL"
- )
- expect(getTestResult("pw.expect(5).not.toBeType('number')", 0)).toEqual(
- "FAIL"
- )
- expect(
- getTestResult("pw.expect(new Date()).not.toBeType('object')", 0)
- ).toEqual("FAIL")
- expect(
- getTestResult("pw.expect(undefined).not.toBeType('undefined')", 0)
- ).toEqual("FAIL")
- expect(
- getTestResult("pw.expect(BigInt(123)).not.toBeType('bigint')", 0)
- ).toEqual("FAIL")
- expect(
- getTestResult("pw.expect(Symbol('test')).not.toBeType('symbol')", 0)
- ).toEqual("FAIL")
- expect(
- getTestResult("pw.expect(function() {}).not.toBeType('function')", 0)
- ).toEqual("FAIL")
- })
- test("unexpected type", () => {
- expect(getTestResult("pw.expect('random').toBeType('unknown')", 0)).toEqual(
- "FAIL"
- )
- })
- })
|