mocha 单元测试,验证 属性 是否为 joi 模式

mocha unit testing, validate if property is a joi schema

我正在尝试为我的应用程序编写单元测试。 在我的应用程序中,我有一个 joi 架构为 属性.

的组件

为了确保模式已设置,我需要一种方法来检查 属性 是否是一个 joi 模式,但我无法让它工作。

const assert = require("assert");
const joi = require("joi");
const { describe, it } = require("mocha");


describe("should validate joi schema", () => {
    it("test for joi instance", () => {

        let schema = joi.object({
            name: joi.string()
        });

        assert(schema instanceof joi.object);

    });
});

检查对象是否为 joi 的正确方法是什么 schema/instance?

错误信息:

> ./node_modules/.bin/mocha ./tests/index.js

  should validate joi schema
    1) test for joi instance


  1 failing

  1) should validate joi schema
       test for joi instance:

      AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

  assert(schema instanceof joi.object)

      + expected - actual

      -false
      +true
      
      at Context.<anonymous> (tests/joi.js:13:9)
      at processImmediate (node:internal/timers:466:21)

尝试过各种方法:

为什么不用isSchema(schema, [options]):

Checks whether or not the provided argument is a joi schema

const assert = require('assert');
const joi = require('joi');
const { describe, it } = require('mocha');

describe('should validate joi schema', () => {
  it('test for joi instance', () => {
    let schema = joi.object({
      name: joi.string(),
    });

    assert(joi.isSchema(schema), 'schema is joi schema');
  });
});

测试结果:

  should validate joi schema
    ✓ test for joi instance


  1 passing (5ms)