-
-
Notifications
You must be signed in to change notification settings - Fork 260
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
Suggestion: implementing auto-mocking capabilities #335
Comments
Oh! That's a cool way to go about it actually. There's an open PR in Nest's main repo to add this Auto mock kind of setup as well. I need to bring it back up and see what else we want to do on it |
Thanks :)) Nice! Let me know if you wanna see the code, it's short. The PR is open a long time for a while by the way :| |
@omermorad If you have a more general purpose NestJS wrapper for testing I'm definitely open to including it in this repository. At the moment the mock functionality lives inside We could definitely consider adding a new package called for example |
@WonderPanda yes, you are totally right, the original package is for general purposes, so it doesn't make sense to add nest dependencies there. Sounds good, so do you want me to create a fresh new PR that adds a new package called Another thing - what do you think about adding Mocha/Sinon support (maybe Sinon stub function) to the original |
I'm definitely open to it assuming that we can get the type safety guarantees on feature parity with what is in |
But it's for nestjs specifically, I'm using the |
Yeah, I understand. I think that we definitely want to have a dedicated package for the NestJS testing eg In my previous comment I just meant that if you were looking at adding in functionality to the existing package I think it would make the most sense to split these initiatives across multiple PRs so that we can get the NestJS testing package in first and then we can figure out the best way to support mocha/sinon/etc |
Ok, got you. |
That sounds awesome, thanks @omermorad! |
😎😎 |
@WonderPanda I want to draw your attention to something; I've made a little bit of a research (actually "us", thanks @potoos) and found this package called @potoos (a friend who works with me) - can you please explain the differences we discussed and how can it help us here with the new package we want to create? |
@omermorad It doesn't surprise me that someone has take the automocking further. The current implementation is a pretty small wrapper around Jest. If the other library is objectively better I'm fine to look at using it as the foundation for some NestJS testing utilities |
@WonderPanda the main differentiator between the libraries that we've found is the deep mocking capabilities for nested objects. We've found that @golevelup/ts-jest doesn't do this well out-of-the-box. We couldn't quite understand why createMock didn't stub all of the first-level dependencies and only some. Could be our misuse. |
Are you able to show a quick snippet that demonstrates the difference between the two? |
I can try :) Example 1 Take a look on the query runner mock ( describe('Some Unit Test', () => {
let payoutService: PayoutService, service;
const queryRunnerMock = mock<QueryRunner>();
const errorMock = new Error('Some Error');
beforeAll(() => {
const { unit, unitRef } = Spec.createUnit<PayoutService>(PayoutService)
.mock(PayoutDao)
.using({
createPayout: () => Promise.resolve({ id: 2 }),
})
.mock(ClientService)
.using({
getAccountId: () => Promise.resolve({ token: '' }),
})
.mock(Connection)
.using({
createQueryRunner: () => queryRunnerMock,
})
.compile();
payoutService = unit;
service = unitRef.get(SomeService);
});
describe('when something happens', () => {
test('then check something', async () => {
service.createTransfer.mockImplementationOnce(() => {
throw errorMock;
});
payoutService.performActionForTest();
expect(queryRunnerMock.startTransaction).toHaveBeenCalled();
expect(queryRunnerMock.rollbackTransaction).toHaveBeenCalled();
expect(queryRunnerMock.release).toHaveBeenCalled();
});
});
}); Example 2 The differences are hard to tell, I think it is related to the inner implementation of the describe('PayoutService', () => {
let payoutService: PayoutService, service;
const queryRunnerMock = createMock<QueryRunner>({
connect: jest.fn(),
startTransaction: jest.fn(),
commitTransaction: jest.fn(),
rollbackTransaction: jest.fn(),
release: jest.fn(),
});
const errorMock = new Error('Some Error');
beforeAll(() => {
const { unit, unitRef } = Spec.createUnit<PayoutService>(PayoutService)
.mock(PayoutDao)
.using({
createPayout: () => Promise.resolve({ id: 2 }),
})
.mock(ClientService)
.using({
getAccountId: () => Promise.resolve({ token: '' }),
})
.mock(Connection)
.using({
createQueryRunner: () => queryRunnerMock,
})
.compile();
payoutService = unit;
service = unitRef.get(SomeService);
});
describe('when something happens', () => {
test('then check something', async () => {
service.createTransfer.mockImplementationOnce(() => {
throw errorMock;
});
payoutService.performActionForTest();
expect(queryRunnerMock.startTransaction).toHaveBeenCalled();
expect(queryRunnerMock.rollbackTransaction).toHaveBeenCalled();
expect(queryRunnerMock.release).toHaveBeenCalled();
});
});
}); I hope it makes sense for you |
@WonderPanda any update? |
@omermorad I don't have a lot of time to investigate this further right now, currently trying to get a side project into market. If the functionality of |
The auto mock will be available when #342 is released |
Hi,
The
testing
package ingolevelup
is great, the easy functionality ofcreateMock
makes it easy to mock things without any effort; @jmcdo29 - saw your issue at nest repo (nestjs/nest#6277 - feat(testing): add auto-mocking capabilities) and thought maybe I can implement it, so I did.My questions are (1) Do you still want this feature? (2) Is this the right repo to create a PR in?
Here is a code snippet example:
Where all the rest of the dependencies of the class
SomeService
are auto-mocked, everything is usingcreateMock()
of courseThe text was updated successfully, but these errors were encountered: