studio paquetes org language for javascript chai

javascript - paquetes - r repository



¿Cómo funciona el expect(). To.be.true en Chai? (2)

Esta pregunta ya tiene una respuesta aquí:

expect(true).to.be.true;

En este código, todo el ''to'', ''be'', ''true'' parece ser un atributo de la respuesta del objeto de ''expect (true)''.

¿Cómo pueden funcionar estos atributos para que puedan generar una excepción?


Eche un vistazo a la fuente de la afirmación chai, pero el tl; dr es que Chai implementa sus propios métodos chainable en su biblioteca de afirmaciones. Sin embargo, las palabras clave especiales son simplemente azúcar sintáctica, como se muestra en el siguiente código. Literalmente, solo son propiedades que se agregan y se pueden encadenar, pero en realidad no se define nada:

/** * ### Language Chains * * The following are provided as chainable getters to improve the readability * of your assertions. * * **Chains** * * - to * - be * - been * - is * - that * - which * - and * - has * - have * - with * - at * - of * - same * - but * - does * * @name language chains * @namespace BDD * @api public */ [ ''to'', ''be'', ''been'' , ''is'', ''and'', ''has'', ''have'' , ''with'', ''that'', ''which'', ''at'' , ''of'', ''same'', ''but'', ''does'' ].forEach(function (chain) { Assertion.addProperty(chain); });

A partir de ahí, lo que realmente busca son palabras clave que define específicamente. Entonces, un ejemplo es la palabra clave .to.be.true , se verá como true como se define en el fragmento de código a continuación.

/** * ### .true * * Asserts that the target is strictly (`===`) equal to `true`. * * expect(true).to.be.true; * * Add `.not` earlier in the chain to negate `.true`. However, it''s often best * to assert that the target is equal to its expected value, rather than not * equal to `true`. * * expect(false).to.be.false; // Recommended * expect(false).to.not.be.true; // Not recommended * * expect(1).to.equal(1); // Recommended * expect(1).to.not.be.true; // Not recommended * * A custom error message can be given as the second argument to `expect`. * * expect(false, ''nooo why fail??'').to.be.true; * * @name true * @namespace BDD * @api public */ Assertion.addProperty(''true'', function () { this.assert( true === flag(this, ''object'') , ''expected #{this} to be true'' , ''expected #{this} to be false'' , flag(this, ''negate'') ? false : true ); });


Puedes consultar el código fuente:

[ ''to'', ''be'', ''been'' , ''is'', ''and'', ''has'', ''have'' , ''with'', ''that'', ''which'', ''at'' , ''of'', ''same'', ''but'', ''does'' ].forEach(function (chain) { Assertion.addProperty(chain); });

y hay un addProperty en utils :
https://github.com/chaijs/chai/blob/master/lib/chai/utils/addProperty.js

Con esto puedes encadenar infinitamente las propiedades como: .to.be.to.to.to.be.equal()

Vamos a crear una demostración más simple:

Supongamos que tiene un objeto .true() , con el método .true()

const assert = { ''true'': function (v) { return !!v } }

y quieres poder encadenar infinitamente. Simplemente use el defineProperty para definir nuestro getter:

Object.defineProperty(assert, ''to'', { get() { return assert } })

así que ahora puedes

assert.to.to.to.to.true(false)

código de trabajo: https://codepen.io/CodinCat/pen/LLzBEX?editors=0012

He añadido otro ejemplo más complejo aquí: https://codepen.io/CodinCat/pen/dRVjXL?editors=0012

En este ejemplo, puede ver que hay algunos comportamientos en la propiedad .true .

Almacenamos el valor de expect() en el __expectObj interno y la propiedad __value , y luego lo verificamos en el getter de .true . Así que puedes

expect(false).to.to.to.to.true