0

like in chakram testing expect(WallObject).to.have.schema(expectedSchema). Similarly which function is there in Jest? I am using jest with supertest.

Nikita S
  • 139
  • 2
  • 11

1 Answers1

1

There is nothing available in JEST to test schema directly. I have achieved this with the help of AJV. Using AJV i am comparing schema with response and then using Jest expect checking value is true or not. like

const Ajv = require('ajv');

const ajv = new Ajv({
  allErrors: true,
  format: 'full',
  useDefaults: true,
  coerceTypes: 'array',
  errorDataPath: 'property',
  sourceCode: false,
});

const validateParams = (params, schema) => {
  const validate = ajv.compile(schema);
  const isValidParams = validate(params);
  return isValidParams;
};

 const result = validateParams(res.body, {
        type: 'array',
        items: {
          type: 'object',
          properties: {
            id: {
              type: 'integer',
            },
            email: {
              type: 'string',
            },
         }
      }
    });

 expect(result).toBe(true);
 done();
Nikita S
  • 139
  • 2
  • 11