UNPKG

japa

Version:

Lean test runner for Node.js

68 lines (67 loc) 2.04 kB
"use strict"; /** * @module Core */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Assert = void 0; /* * japa * * (c) Harminder Virk <virk@adonisjs.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ const chai_1 = require("chai"); const Exceptions_1 = require("../Exceptions"); /** * The assert interface to run assertions using chaijs. * This class is a thin wrapper over [chaijs#assert](http://www.chaijs.com/api/assert/) allowing * assertion planning and all existing methods from chaijs are supported. * * The API doesn't cover all the methods. However, all methods from the chaiJs assert syntax are * supported. */ class Assert { constructor() { // config.includeStack = true this._counts = 0; this._plannedCounts = 0; return new Proxy(this, { get(target, name, receiver) { if (typeof (target[name]) !== 'undefined') { return Reflect.get(target, name, receiver); } /* istanbul ignore else */ if (typeof (chai_1.assert[name]) !== 'undefined') { target._counts++; } return Reflect.get(chai_1.assert, name, receiver); }, }); } /** * Use chai plugins */ static use(fn) { return (0, chai_1.use)(fn); } /** * Plan for assertions */ plan(count) { this._plannedCounts = count; } /** * Evaluate whether assertions count matches the * planned counts or not. */ evaluate() { if (this._plannedCounts && this._plannedCounts !== this._counts) { const suffix = this._plannedCounts === 1 ? '' : 's'; const message = `Planned for ${this._plannedCounts} assertion${suffix}, but ran ${this._counts}`; throw new Exceptions_1.InvalidAssertionsCount(message); } } } exports.Assert = Assert;