Open ThomasAribart opened 1 year ago
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.
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.
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,
},
});
This is really excellent test utility, thank you to all who have contributed so far.
On this point from the README:
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:
printDiffOrStringify
used verbatim)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.0aws-sdk-client-mock-jest
: 3.0.0aws-sdk-client-mock
: 3.0.0typescript
: 5.2.2vitest
: 0.34.3vitest.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"]
}
}
@m-radzikowski Can this be re-opened? Vitest matchers don't yet exist for this project, and only kind-of work as a side effect of Vitest being very diligent about being Jest-compatable.
Dedicated Vitest matchers would go a long ways, keeping the issue open may help with fishing for a contribution π€
This will be solved when #231 is merged.
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)
I still see this with 4.1.0. It works when running the tests but TS is not happy in vscode. Any chance someone knows how to fix it?
Using
import { DynamoDBDocumentClient, UpdateCommand } from '@aws-sdk/lib-dynamodb';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest/vitest';
import { beforeEach, describe, expect, it } from 'vitest';
describe('something', () => {
const ddbMock = mockClient(DynamoDBDocumentClient);
beforeEach(() => {
ddbMock.reset();
});
it('something', async () => {
expect(ddbMock).toHaveReceivedCommandWith(UpdateCommand, {});
});
});
Hello and thanks for this great lib π
On my project we went from
jest
tovitest
, mainly for performances. Sadly, we lost thetoHaveReceivedCommand
matchers in the process π₯²Would be awesome to have them for
vitest
as well! Maybe it was the aim of separatingjest
matchers in another lib in the first place ?See https://vitest.dev/guide/extending-matchers.html
Cheers !