【问题标题】:What is Expect function to validate schema in JEST and Supertest?在 JEST 和 Supertest 中验证模式的 Expect 函数是什么?
【发布时间】:2017-11-21 06:35:14
【问题描述】:

就像在脉轮测试中一样 expect(WallObject).to.have.schema(expectedSchema)。同样,Jest 中有哪些功能?我正在使用超级测试的玩笑。

【问题讨论】:

    标签: supertest jestjs babel-jest supertest-as-promised jest-fetch-mock


    【解决方案1】:

    在 JEST 中没有可用于直接测试模式的内容。我在 AJV 的帮助下实现了这一目标。使用 AJV,我将模式与响应进行比较,然后使用 Jest 期望检查值是否为真。喜欢

    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();
    

    【讨论】:

    • 如果有人有更好的选择来实现这一点,请发布您的答案
    猜你喜欢
    • 2022-11-25
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 2021-05-02
    • 2020-09-14
    • 2020-12-11
    • 2020-10-19
    相关资源
    最近更新 更多