How to populate multiple rows of .expectJsonSchema in PactumJS?

Viewed 17

How to populate the .expectJsonSchema in PactumJS with multiple values from a object? Just one row is fine, but how to deal with multiple rows?

const schemaJson = {
  "data.token": { type: "string" },
  "data.user_id": { type: "integer" },
};

schemaTest(schemaJson);

function schemaTest(schemaJson) {
  const url = "xxx";

  // set schema object
  let schema_path = [];
  let schema_value = [];
  Object.entries(schemaJson).forEach(function ([key, value], i) {
    schema_path[i] = key;
    schema_value[i] = value;
  });

  // create test
  describe("test", () => {
    let testCreate = e2e("test");
    it("test", async () => {
      await testCreate
        .step("test")
        .spec()
        .post(url)
        .expectJsonSchema(schema_path[0], schema_value[0])
        .expectStatus(200);
    });
  });
}

I know im doing a for loop on on let, this does work on one row.

Ive tried to wrap it into a object/array but i keep getting "Error: Schema is not an object at #", probably not the right object? Or "data.user_id" gives a error "Error: Keyword not supported: "path" at #"

Does not work:

       // set schema object
      let schema = {};
      let i = 0;
      Object.entries(schemaJson).forEach(function ([key, value], i) {
        let schema_path = key;
        let schema_value = value;
        schema[i] = { schema_path: schema_value } 
      }
 .expectJsonSchema(schema[0])


// Or with a empty schemaJson{} 
  .expectJsonSchema(
              schemaJson ? (schema_path[0], schema_value[0]) :{})

// ERROR 
// AssertionError [ERR_ASSERTION]: Response doesn't match with JSON schema - [
//  {
 //   "keywordLocation": "#/type",
//    "instanceLocation": "#"
//  }

IF i could populate the rows like schema[0], schema[1], enz. I cloud illiterate also in the .expectJsonSchema like .expectJsonSchema(schema[1]? schema[1]: {}), because it supports multiple ".expectJsonSchema(xxx)".

1 Answers

The solution was to breakup the query and join them tougher in the loop. So you can add multiple rows:

Object.entries(schemaJson).forEach(function ([
        schema_path,
        schema_value,
      ]) {
        testQuery = testQuery.expectJsonSchema(schema_path, schema_value);
      });
Related