What is the problem this feature will solve?
I use globally mocked objects in my tests to allow them to run without an environment:
// Something that looks like a nodemailer objectexportconstmailer={sendMail: mock.fn(()=>Promise.resolve())};Because it's global, tests can pollute each other:
it('works',()=>{mailer.sendMail({to: "user@example.com"});assert.equal(mailer.sendMail.mock.callCount(),1);});it('also works',()=>{mailer.sendMail({to: "user@example.com"});assert.equal(mailer.sendMail.mock.callCount(),1);// It fails here, `callCount()` is now 2});What is the feature you are proposing to solve the problem?
I want to be able to reset mailer.sendMail.mock.calls:
describe('something',()=>{beforeEach(()=>{mailer.sendMail.mock.reset();// Method name open to bikeshedding})}) | getcalls(){ |
| returnArrayPrototypeSlice(this.#calls,0); |
| } |
| |
| callCount(){ |
| returnthis.#calls.length; |
| } |
It could be as simple as:
reset(){this.#calls =[];}If you're okay with this design, I'm interested in creating a PR with the feature and associated tests.
What alternatives have you considered?
Use another mocking library
Current workaround
I resorted to this hack:
letprevCount=0;mailer.sendMail.mock.constructor.prototype.lastCalls=function(){constcount=this.callCount();consttail=this.calls.slice(prevCount);prevCount=count;returntail;};
What is the problem this feature will solve?
I use globally mocked objects in my tests to allow them to run without an environment:
Because it's global, tests can pollute each other:
What is the feature you are proposing to solve the problem?
I want to be able to reset
mailer.sendMail.mock.calls:node/lib/internal/test_runner/mock.js
Lines 46 to 52 in 8a4d7ac
It could be as simple as:
If you're okay with this design, I'm interested in creating a PR with the feature and associated tests.
What alternatives have you considered?
Use another mocking library
Current workaround
I resorted to this hack: