-
Notifications
You must be signed in to change notification settings - Fork 5
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
How to mock the Type with new() #21
Comments
Hey @tommy20061222! Interesting find for sure. Some hairy details: Safe-Mock currently does not support mocking the new operator because we have not built in support for the Proxy Construct operation. I will look at adding that support this week. I spent a bit of time playing with the syntax today for In the meantime!I would encourage you to avoid needing to mock the // fooBar.ts
interface bar {
helloworld: () => string;
}
export type middlewareTypeFoo = () => bar; // safeMock.ts
import {middlewareTypeFoo} from './fooBar';
export class fooBarImpl {
public invokeAll(listOfthings: middlewareTypeFoo[]) {
return listOfthings.map((item: middlewareTypeFoo) => {
return item().helloworld();
});
}
} // safeMock.test.ts
import {Mock, SafeMock, when} from 'safe-mock';
import {middlewareTypeFoo} from './fooBar';
import {fooBarImpl} from './safeMock';
describe('foobar Test', () => {
it ('foobar', () => {
const foobar1 = SafeMock.buildFunction<middlewareTypeFoo>();
const response = new fooBarImpl().invokeAll([foobar1]);
console.log(response);
});
}) Cheers! |
thx. @matthewmcnew. I was thinking about this last night and was reading one of the Stackflow thread which suggest same thing you mentioned above. Thank you for the help! At meantime, Please keep me in loop regarding you will add Proxy Construct to the library. |
Hey, Matt, if you copy above three sections of code into three separate files, then
console.log(response)
in the safeMock.test.ts prints outTypeError: (intermediate value).helloworld is not a function
. My question is how should i mocknew
method on this object? I triedwhen(foobar1.new)
, but won't even pass compiler. If i can somehow mocknew
, then i can probably mockhelloworld
as well. Thank you for the help!The text was updated successfully, but these errors were encountered: