1 | ;
|
2 | /**
|
3 | * @module Core
|
4 | */
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.Assert = void 0;
|
7 | /*
|
8 | * japa
|
9 | *
|
10 | * (c) Harminder Virk <virk@adonisjs.com>
|
11 | *
|
12 | * For the full copyright and license information, please view the LICENSE
|
13 | * file that was distributed with this source code.
|
14 | */
|
15 | const chai_1 = require("chai");
|
16 | const Exceptions_1 = require("../Exceptions");
|
17 | /**
|
18 | * The assert interface to run assertions using chaijs.
|
19 | * This class is a thin wrapper over [chaijs#assert](http://www.chaijs.com/api/assert/) allowing
|
20 | * assertion planning and all existing methods from chaijs are supported.
|
21 | *
|
22 | * The API doesn't cover all the methods. However, all methods from the chaiJs assert syntax are
|
23 | * supported.
|
24 | */
|
25 | class Assert {
|
26 | constructor() {
|
27 | // config.includeStack = true
|
28 | this._counts = 0;
|
29 | this._plannedCounts = 0;
|
30 | return new Proxy(this, {
|
31 | get(target, name, receiver) {
|
32 | if (typeof (target[name]) !== 'undefined') {
|
33 | return Reflect.get(target, name, receiver);
|
34 | }
|
35 | /* istanbul ignore else */
|
36 | if (typeof (chai_1.assert[name]) !== 'undefined') {
|
37 | target._counts++;
|
38 | }
|
39 | return Reflect.get(chai_1.assert, name, receiver);
|
40 | },
|
41 | });
|
42 | }
|
43 | /**
|
44 | * Use chai plugins
|
45 | */
|
46 | static use(fn) {
|
47 | return (0, chai_1.use)(fn);
|
48 | }
|
49 | /**
|
50 | * Plan for assertions
|
51 | */
|
52 | plan(count) {
|
53 | this._plannedCounts = count;
|
54 | }
|
55 | /**
|
56 | * Evaluate whether assertions count matches the
|
57 | * planned counts or not.
|
58 | */
|
59 | evaluate() {
|
60 | if (this._plannedCounts && this._plannedCounts !== this._counts) {
|
61 | const suffix = this._plannedCounts === 1 ? '' : 's';
|
62 | const message = `Planned for ${this._plannedCounts} assertion${suffix}, but ran ${this._counts}`;
|
63 | throw new Exceptions_1.InvalidAssertionsCount(message);
|
64 | }
|
65 | }
|
66 | }
|
67 | exports.Assert = Assert;
|