winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚑
https://winglang.io
Other
5.05k stars 196 forks source link

`expect.throws` #5502

Open eladb opened 9 months ago

eladb commented 9 months ago

Use Case

Expect that a code block throws an error.

Proposed Solution

Proposed API:

inflight throws(block: inflight (): void, contains: str?);
throws(block: (): void, contains: str?);

The contains will partial match against the error message text (e.g. contains()).

We need a separate API for inflight and preflight because if this is an inflight closure it is implicitly asynchronous, so it cannot be called preflight.

Example:

bring expect;

// this is the preflight version
expect.throws(() => { throw "my"; }); // `contains` is optional

test "an error is thrown" {
  // this is the inflight version
  expect.throws(() => {
    throw "i am an error";
  }, contains: "error");
}

Implementation Notes

No response

Component

No response

Community Notes

tsuf239 commented 9 months ago

we also have a similar issue: https://github.com/winglang/wing/issues/5238 which can be closed since this one (#5502) has a better syntax :)