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

Create matchers for vitest #139

Closed
ThomasAribart opened this issue Jan 17, 2023 · 4 comments
Closed

Create matchers for vitest #139

ThomasAribart opened this issue Jan 17, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@ThomasAribart
Copy link

Hello and thanks for this great lib 🙌

On my project we went from jest to vitest, mainly for performances. Sadly, we lost the toHaveReceivedCommand matchers in the process 🥲

Would be awesome to have them for vitest as well! Maybe it was the aim of separating jest matchers in another lib in the first place ?

See https://vitest.dev/guide/extending-matchers.html

Cheers !

@ThomasAribart ThomasAribart added the enhancement New feature or request label Jan 17, 2023
@kldavis4
Copy link

I found this in the vitest docs: https://vitest.dev/guide/features.html#chai-and-jest-expect-compatibility

When I set test.globals: true in vite.config.js, the aws-sdk-client-mock-jest matchers seem to work.

@m-radzikowski
Copy link
Owner

Hey, sorry for the late reply. Thank you @kldavis4 for finding and sharing the way to make aws-sdk-client-mock-jest work with vitest. I will add that to the README.

While I see a raising popularity of vitest, I'm not using it right now myself. If anyone wants to add vitest matchers, or, even better, adjust current matchers so they can be use in vitest natively (if possible) - feel free to open a PR.

@colesiegel
Copy link

It still doesn't work for me, even with test.globals set to true.

expect(ddbMock).toHaveReceivedCommandWith(QueryCommand, {});

Property 'toHaveReceivedCommandWith' does not exist on type 'Assertion<AwsStub<ServiceInputTypes, ServiceOutputTypes>>'.ts(2339)

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    reporters: ['junit', 'default'],
    outputFile: './.vitest/results.xml',
    coverage: {
      // include coverage even if no tests exist
      all: true,
      include: ['src/*'],
      reportsDirectory: './.vitest/coverage',
    },
    setupFiles: './src/setupTests.ts',
    // [https://github.com/m-radzikowski/aws-sdk-client-mock/issues/139]
    globals: true,
  },
});

@mbtts
Copy link

mbtts commented Sep 12, 2023

This is really excellent test utility, thank you to all who have contributed so far.

On this point from the README:

To use those matchers with [Vitest](https://vitest.dev/), set `test.globals` to `true` in `vite.config.js`
(see [#139](https://github.com/m-radzikowski/aws-sdk-client-mock/issues/139)).

One problem here is the Vitest “Jest” matcher utils are a much smaller subset of the actual Jest matcher utils (which also rely on other Jest packages like jest-diff).

✅ If the expectation passes there is no problem, the matchers work as expected with vitest.

❌ However if the expectations then printDiffOrStringify is called:

At this point Vitest runner will bail:

TypeError: this.utils.printDiffOrStringify is not a function

Happy to stick my hand up to make a contribution.

My thoughts are there are a few possibilities:

  1. Avoid: Feature-sniff and use an alternative function/output when used in a non-Jest environment/Jest-like environment
  2. Import: jest-utils could become a dependency (and its implementation of printDiffOrStringify used verbatim)
  3. Delegate: Approach the Vitest maintainers about providing the same functionality/API (I could not see anything planned/open issue, but might have overlooked something)

In the very short term I think either the limitations should be documented -or- Vitest not be officially documented as supported for just the matchers.


@colesiegel On the typings question, I hope you got this resolved? Sharing my configuration in case it helps:

  • @aws-sdk/*: 3.409.0
  • aws-sdk-client-mock-jest: 3.0.0
  • aws-sdk-client-mock: 3.0.0
  • typescript: 5.2.2
  • vitest: 0.34.3

vitest.config.ts:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    globals: true,
    mockReset: true,
    setupFiles: ['./awsSdkClientMatchers.ts'],
  },
});

awsSdkClientMatchers.ts:

import 'aws-sdk-client-mock-jest';

tsconfig.json:

{
  "extends": "@tsconfig/node18/tsconfig",
  "compilerOptions": {
    "allowJs": true,
    "lib": ["ESNext"],
    "module": "NodeNext",
    "moduleResolution": "nodenext",
    "noEmit": true,
    "types": ["vitest/globals"]
  }
}

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

No branches or pull requests

5 participants