deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

tracking: complete `std/expect`

Open iuioiua opened this issue 1 year ago • 25 comments

The following APIs are currently not yet implemented in std/expect:

  • [ ] toMatchSnapShot
  • [ ] toMatchInlineSnapShot
  • [ ] toThrowErrorMatchingSnapShot
  • [ ] toThrowErrorMatchingInlineSnapShot
  • [x] expect.anything #4366
  • [x] expect.any #4366
  • [x] expect.arrayContaining #4366
  • [x] expect.closeTo #4508
  • [ ] expect.objectContaining
  • [x] expect.stringContaining #4508
  • [x] expect.stringMatching #4508
  • [ ] expect.assertions #6032
  • [x] expect.hasAssertions
  • [x] expect.addEqualityTester #4255
  • [x] expect.addSnapshotSerializer #4537
  • [x] expect.extend #4412

For those wanting to contribute, please submit one PR per API.

iuioiua avatar Dec 15 '23 00:12 iuioiua

Hey @iuioiua, I would love to contribute here.

raashidanwar avatar Dec 18 '23 02:12 raashidanwar

Hey @iuioiua,

I would love to contribute here.

Great! Let us know which ones you'd like to work on, so there's no overlap with other's work.

iuioiua avatar Dec 18 '23 03:12 iuioiua

As you start working on the async matchers, you might end up fixing https://github.com/denoland/deno_std/issues/3947 as a side effect.

syhol avatar Dec 18 '23 17:12 syhol

@iuioiua I will be picking.

expect.objectContaining
expect.not.objectContaining
expect.stringContaining
expect.not.stringContaining
expect.stringMatching
expect.not.stringMatching

raashidanwar avatar Jan 01 '24 17:01 raashidanwar

Hi @iuioiua @raashidanwar Can I try for this too?

I'll pick

expect.assertions
expect.hasAssertions
expect.addSnapshotSerializer
expect.extend

eryue0220 avatar Jan 24 '24 02:01 eryue0220

Hi @iuioiua @raashidanwar Can I try for this too?

I'll pick

expect.assertions
expect.hasAssertions
expect.addSnapshotSerializer
expect.extend

For sure! Thank you. Please let us know if you need any help with anything.

iuioiua avatar Jan 24 '24 02:01 iuioiua

Hi, @iuioiua

I have a question about is there a way that I can know whether the current Deno.test is completed or not?

eryue0220 avatar Jan 25 '24 16:01 eryue0220

Can you elaborate? Perhaps, something like this is what you're after.

let isTestDone = false;
Deno.test("my test", () => {
  ...
  isTestDone = true;
});

iuioiua avatar Jan 28 '24 01:01 iuioiua

Can you elaborate? Perhaps, something like this is what you're after.

let isTestDone = false;
Deno.test("my test", () => {
  ...
  isTestDone = true;
});

Oh, sorry ~

What I mean that is there any methods that know the callback is done as for the Deno.test('test', callback)? If use a variable is a little tricky I think, because someone may write code like:

And expect.hasAssertions API is want to check if there were any assertions executed before, for example:

Deno.test('test suite', () => {
  expect.hasAssertion();
});

this will throw an error, there was no any assertions in test suite function at all . And the following code will pass:

Deno.test('test suite', () => {
    expect.hasAssertions();
    expect(1 + 2).toEqual(3);
});

On the other hand, why I know current test suite function is done or not, thus can throw an error exactly when the test suite function is completed.

eryue0220 avatar Jan 28 '24 05:01 eryue0220

I don't think we have such hook in Deno.test. These are the current options of Deno.test https://deno.land/[email protected]?s=Deno.TestDefinition

kt3k avatar Jan 29 '24 07:01 kt3k

I don't think we have such hook in Deno.test. These are the current options of Deno.test https://deno.land/[email protected]?s=Deno.TestDefinition

Is there any possibility to add some similar hook in deno_core, such as before and after?

eryue0220 avatar Feb 01 '24 01:02 eryue0220

Is there any possibility to add some similar hook in deno_core, such as before and after?

Not sure if we are still open to such addition. When the last time we discussed similar topic, we decided to add test steps (See https://docs.deno.com/runtime/manual/basics/testing/#test-steps ). That essentially added hook capability to deno test.

So maybe it's a bit unlikely we add another hook capability to deno test, but I'm not completely sure.

kt3k avatar Feb 01 '24 09:02 kt3k

It might be non ideal, but maybe we can add support of expect.hasAssertions() only for describe it runners. They are wrappers of Deno.test and t.step, and we can add arbitrary hooks before and after it.

kt3k avatar Feb 01 '24 09:02 kt3k

Is there any possibility to add some similar hook in deno_core, such as before and after?

Not sure if we are still open to such addition. When the last time we discussed similar topic, we decided to add test steps (See https://docs.deno.com/runtime/manual/basics/testing/#test-steps ). That essentially added hook capability to deno test.

So maybe it's a bit unlikely we add another hook capability to deno test, but I'm not completely sure.

Thanks for the information, but I think it's still not same as I mentioned before. Because t.step requires every developer to write their own assertion logic if they want to use expect.hasAssertion API. Or it will not work if use the api only.

eryue0220 avatar Feb 01 '24 14:02 eryue0220

It might be non ideal, but maybe we can add support of expect.hasAssertions() only for describe it runners. They are wrappers of Deno.test and t.step, and we can add arbitrary hooks before and after it.这可能并不理想,但也许我们可以仅为 describe it 跑步者添加 expect.hasAssertions() 的支持。它们是 Deno.testt.step 的包装器,我们可以在其前后添加任意钩子。

It's also a good option, and can combine with deno_lint to do some limits.

eryue0220 avatar Feb 01 '24 14:02 eryue0220

I'd like to work on: expect.anything expect.any expect.arrayContaining expect.not.arrayContaining

javihernant avatar Feb 19 '24 18:02 javihernant

Just out of interest: here the methods are called *Containing while native array api has includes and the assert mod uses *Includes . Is this intentional?

timreichen avatar Feb 19 '24 19:02 timreichen

Just out of interest: here the methods are called *Containing while native array api has includes and the assert mod uses *Includes . Is this intentional?

I think this is trying to imitate jest api. Take a look: expect.arrayContaining(array)

javihernant avatar Feb 19 '24 19:02 javihernant

I wonder if it would make sense to rename them to *Includes to align better with the web api and assert mod. They all call array.includes() under the hood and containing() has a very different meaning in Segments.prototype.containing() web api.

timreichen avatar Feb 19 '24 19:02 timreichen

I wonder if it would make sense to rename them to *Includes to align better with the web api and assert mod.

The expectation for std/expect is the compatibility to jest. Inventing new names here doesn't make sense to me.

kt3k avatar Feb 20 '24 01:02 kt3k

For anyone wanting to contribute, we also need to make the Expected interface public (it's currently private) and include documentation, with examples, for each method.

iuioiua avatar Mar 16 '24 05:03 iuioiua

@kt3k, is fn() supposed to be part of the public API?

iuioiua avatar Mar 16 '24 05:03 iuioiua

Yes, it corresponds to jest.fn https://jestjs.io/docs/mock-functions

kt3k avatar Mar 16 '24 21:03 kt3k