Skip to content

Commit

Permalink
feat: mock node-fetch's export pattern
Browse files Browse the repository at this point in the history
Modified `FetchMock.sandbox`'s method to return an interface that more
closely mocks `node-fetch`'s export pattern.
Created tests to verify the new return value.
  • Loading branch information
chet-manley committed Apr 12, 2020
1 parent 3500688 commit d7a6577
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,17 @@ FetchMock.sandbox = function() {
const sandbox = Object.assign(
proxy, // Ensures that the entire returned object is a callable function
FetchMock, // prototype methods
this.createInstance() // instance data
this.createInstance(), // instance data
{
Headers: this.config.Headers,
Request: this.config.Request,
Response: this.config.Response
}
);

sandbox.bindMethods();
sandbox.isSandbox = true;
sandbox.default = sandbox;
return sandbox;
};

Expand Down
15 changes: 15 additions & 0 deletions test/specs/sandbox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,20 @@ module.exports = (fetchMock, theGlobal) => {
fm.config.fetch = originalFetch;
expect(() => fm.spy()).not.to.throw();
});

it('exports a properly mocked node-fetch module shape', () => {
// uses node-fetch default require pattern
const {
default: fetch,
Headers,
Request,
Response
} = fetchMock.sandbox();

expect(fetch.name).to.equal('proxy');
expect(new Headers()).to.be.an.instanceOf(fetchMock.config.Headers);
expect(new Request()).to.be.an.instanceOf(fetchMock.config.Request);
expect(new Response()).to.be.an.instanceOf(fetchMock.config.Response);
});
});
};

0 comments on commit d7a6577

Please sign in to comment.