Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lunigorn opened this issue Jun 8, 2024 · 0 comments
Open

Comments

@Lunigorn
Copy link

Lunigorn commented Jun 8, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant