All files chai-ts-as-promised.ts

100% Statements 10/10
50% Branches 1/2
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 2361x     1x                                                                                                                                                                                                                                                                                                                                                                                                                                                 1x 1x   1x       1x   1x   1x 1x      
import ChaiAsPromised from 'chai-as-promised';
 
import type { Matches } from './chai-ts';
import { ChaiTS } from './chai-ts';
 
declare global {
  export namespace Chai {
    interface TypedAssertion<A> extends Assertion {
      // From chai-as-promise
      eventually: TypedPromisedAssertion<A>;
    }
 
    interface PromisedAssertion {
      /**
       * Returns a promise that resolves to the tested value.
       *
       * @example
       * const a: unknown = await expect(Promise.resolve(5)).to.eventually.be.equal(5).yieldValue();
       * expect(a).to.equal(5);
       */
      yieldValue(): Promise<unknown>;
    }
 
    interface TypedPromisedAssertion<A> extends TypedEventually<A>, PromiseLike<unknown> {
      /**
       * Returns a promise that resolves to the tested value cast to the appropriate type.
       *
       * @example
       * const a: number = await expect(Promise.resolve(5)).to.eventually.be.equal(5).yieldValue();
       * expect(a).to.equal(5);
       */
      yieldValue(): Promise<Awaited<A>>;
    }
 
    interface TypedEventually<A> extends Eventually {
      /**
       * Same as the default `eq` however expecting an argument whose type narrows the
       * type of the tested value - i.e., if the expected value can be assigned to a variable
       * that has the type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.narrowEq(5)
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.narrowEq(5);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.narrowEq(5);
       */
      narrowEq: NarrowPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `equal` however expecting an argument whose type narrows the
       * type of the tested value - i.e., if the expected value can be assigned to a variable
       * that has the type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.narrowEqual(5)
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.narrowEqual(5);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.narrowEqual(5);
       */
      narrowEqual: NarrowPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `equals` however expecting an argument whose type narrows the
       * type of the tested value - i.e., if the expected value can be assigned to a variable
       * that has the type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.narrowEquals(5);
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.narrowEquals(5);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.narrowEquals(5);
       */
      narrowEquals: NarrowPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `eql` however expecting an argument whose type narrows the
       * type of the tested value - i.e., if the expected value can be assigned to a variable
       * that has the type of the tested value.
       *
       * Besides testing the type, this method simplifies implementing tests because it enables
       * code completion when writing the expected value.
       *
       * @example
       * await expect(Promise.resolve([1, 2, 3])).to.eventually.be.narrowEql([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3] as unknown[])).to.eventually.be.narrowEql([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.narrowEql([1, 2, 3]);
       * await expect(Promise.resolve({ a: 1, b: true, c: 'value' }))
       *   .to.eventually.be.narrowEql({ a: 1, b: true, c: 'value' });
       */
      narrowEql: NarrowPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `eqls` however expecting an argument whose type narrows the
       * type of the tested value - i.e., if the expected value can be assigned to a variable
       * that has the type of the tested value.
       *
       * Besides testing the type, this method simplifies implementing tests because it enables
       * code completion when writing the expected value.
       *
       * @example
       * await expect(Promise.resolve([1, 2, 3])).to.eventually.be.narrowEqls([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3] as unknown[])).to.eventually.be.narrowEqls([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.narrowEqls([1, 2, 3]);
       * await expect(Promise.resolve({ a: 1, b: true, c: 'value' }))
       *   .to.eventually.be.narrowEqls({ a: 1, b: true, c: 'value' });
       */
      narrowEqls: NarrowPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `eq` however expecting an argument whose type matches the
       * type of the tested value - i.e., if the expected value can only be assigned to a variable
       * that has the exact type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.matchEq(5);
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.matchEq(5 as unknown);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.matchEq<unknown>(5);
       */
      matchEq: MatchPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `equal` however expecting an argument whose type matches the
       * type of the tested value - i.e., if the expected value can only be assigned to a variable
       * that has the exact type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.matchEqual(5);
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.matchEqual(5 as unknown);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.matchEqual<unknown>(5);
       */
      matchEqual: MatchPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `equals` however expecting an argument whose type matches the
       * type of the tested value - i.e., if the expected value can only be assigned to a variable
       * that has the exact type of the tested value.
       *
       * @example
       * await expect(Promise.resolve(5)).to.eventually.be.matchEquals(5);
       * await expect(Promise.resolve(5) as unknown).to.eventually.be.matchEquals(5 as unknown);
       * await expect(Promise.resolve(5) as Promise<unknown>).to.eventually.be.matchEquals<unknown>(5);
       */
      matchEquals: MatchPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `eql` however expecting an argument whose type matches the
       * type of the tested value - i.e., if the expected value can only be assigned to a variable
       * that has the exact type of the tested value.
       *
       * @example
       * await expect(Promise.resolve([1, 2, 3])).to.eventually.matchEql([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.matchEql([1, 2, 3] as unknown[]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.matchEql<unknown[]>([1, 2, 3]);
       */
      matchEql: MatchPromisedEqual<Awaited<A>>;
 
      /**
       * Same as the default `eqls` however expecting an argument whose type matches the
       * type of the tested value - i.e., if the expected value can only be assigned to a variable
       * that has the exact type of the tested value.
       *
       * @example
       * await expect(Promise.resolve([1, 2, 3])).to.eventually.matchEqls([1, 2, 3]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.matchEqls([1, 2, 3] as unknown[]);
       * await expect(Promise.resolve([1, 2, 3]) as Promise<unknown[]>).to.eventually.be.matchEqls<unknown[]>([1, 2, 3]);
       */
      matchEqls: MatchPromisedEqual<Awaited<A>>;
 
      // Assertion Properties
      a: TypedPromisedAssertion<A>;
      an: TypedPromisedAssertion<A>;
      arguments: TypedPromisedAssertion<A>;
      Arguments: TypedPromisedAssertion<A>;
      be: TypedPromisedAssertion<A>;
      empty: TypedPromisedAssertion<A>;
      exist: TypedPromisedAssertion<A>;
      extensible: TypedPromisedAssertion<A>;
      false: TypedPromisedAssertion<A>;
      finite: TypedPromisedAssertion<A>;
      frozen: TypedPromisedAssertion<A>;
      itself: TypedPromisedAssertion<A>;
      key(string: string): TypedPromisedAssertion<A>;
      NaN: TypedPromisedAssertion<A>;
      not: TypedPromisedAssertion<A>;
      null: TypedPromisedAssertion<A>;
      ok: TypedPromisedAssertion<A>;
      sealed: TypedPromisedAssertion<A>;
      string(string: string, message?: string): TypedPromisedAssertion<A>;
      to: TypedPromisedAssertion<A>;
      true: TypedPromisedAssertion<A>;
      undefined: TypedPromisedAssertion<A>;
 
      // Equal Properties
      equal: TypedPromisedEqual<Awaited<A>>;
      equals: TypedPromisedEqual<Awaited<A>>;
      eq: TypedPromisedEqual<Awaited<A>>;
      eql: TypedPromisedEqual<Awaited<A>>;
      eqls: TypedPromisedEqual<Awaited<A>>;
    }
 
    interface TypedPromisedEqual<A> {
      (value: unknown, message?: string): TypedPromisedAssertion<A>;
    }
 
    interface NarrowPromisedEqual<A> {
      (value: A, message?: string): TypedPromisedAssertion<A>;
    }
 
    interface MatchPromisedEqual<A> {
      <E>(value: Matches<A, E> extends true ? E : never, message?: string): TypedPromisedAssertion<A>;
    }
  }
}
 
/**
 * Initializer for the `chai-ts-as-promised` plugin, which also initializes the
 * `chai-ts` and `chai-as-promised` plugins.
 *
 * @example
 * import { use } from chai;
 * import { ChaiTSAsPromised } from 'chai-ts';
 * use(ChaiTSAsPromised);
 */
export const ChaiTSAsPromised: Chai.ChaiPlugin = (chai) => {
  chai.use(ChaiTS);
 
  const assertionPrototype = chai.Assertion.prototype as unknown as { yieldValue: unknown };
 
  // we need to ensure that ChaiAsPromised as is not replacing the yield value because it
  // always returns the assertion object if there is no eventually (see chai-as-promised.doAsserterAsyncAndAddThen)
  const yieldValue = assertionPrototype.yieldValue;
 
  chai.use(ChaiAsPromised);
 
  Eif (assertionPrototype.yieldValue !== yieldValue) {
    (chai.Assertion.prototype as unknown as { yieldValue: unknown }).yieldValue = yieldValue;
  }
};