l2beat/earl

Feature request: Add support of Assertion Functions to earl assertions.

Opened this issue · 0 comments

Request:
Add support of Assertion Functions to earl assertions.

Motivation:
The next code does not pass types validation:

import { suite, test } from "mocha";
import { expect } from 'earl'

type UnionType = {status: 400} | {status: 200, body: string}
var testObject: UnionType = {status: 200, body: "hello"};

suite("reports", () => {
  test("create report", async () => {
    
    expect(testObject.status).toEqual(200);
    expect(testObject.body).toEqual("hello");
  });
});

On line 11 the error is shown:
Property 'body' does not exist on type 'UnionType'.
Property 'body' does not exist on type '{ status: 400; }'.

image
It doesn't work as expected because earl do not support Assertion Functions
But if i use workaround with asserts:

import { suite, test } from "mocha";
import { expect } from 'earl'

type UnionType = {status: 400} | {status: 200, body: string}
var testObject: UnionType = {status: 200, body: "hello"};

suite("reports", () => {
  test("create report", async () => {
    
    expect_toBe(testObject.status, 200);
    expect(testObject.body).toEqual("hello");
  });
});
    
function expect_toBe<T>(arg: any, value: T): asserts arg is T {
  expect(arg).toEqual(value);
}

it works fine.

Is it possible to add such assertions to earl functions definitions?