Skip to content
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

nock.delete doesn't match request (getting OPTIONS instead of DELETE) #1534

Closed
Manny91 opened this issue May 2, 2019 · 3 comments
Closed
Labels
support General questions or support.

Comments

@Manny91
Copy link

Manny91 commented May 2, 2019

I am trying to test a delete request

so in my test I'm doing:

describe("logout", () => {
    it("success", async () => {
      nock.disableNetConnect();
      nock(baseURL)
        .persist()
        .log(console.log)
        .delete("/api/auth/logout?token=fakeToken")
        .reply(200, null, {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "application:json"
        });

      await fetch(baseURL + "/api/auth/logout?token=fakeToken", {
        method: "DELETE",
        headers: { "Content-Type": "application/json" }
      }).then(res => {
        expect(res.status).toBe(200);
      }); 
            
    });
  });

the request is being done correctly but nock is not able to match it as this is what I get in the console


TypeError: Network request failed

      at XMLHttpRequest.xhr.onerror (node_modules/whatwg-fetch/dist/fetch.umd.js:473:16)
      at XMLHttpRequest.el.addEventListener.event (node_modules/jsdom/lib/jsdom/living/helpers/create-event-accessor.js:33:32)
      at invokeEventListeners (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:193:27)
      at XMLHttpRequestEventTargetImpl._dispatch (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:119:9)
      at XMLHttpRequestEventTargetImpl.dispatchEvent (node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:82:17)
      at XMLHttpRequest.dispatchEvent (node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:157:21)
      at requestErrorSteps (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:132:7)
      at Object.dispatchError (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:62:3)
      at EventEmitter.client.on.err (node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
      at Request.preflightClient.on.err (node_modules/jsdom/lib/jsdom/living/xhr-utils.js:412:47)
      at Request.onRequestError (node_modules/request/request.js:881:8)
      at node_modules/nock/lib/request_overrider.js:221:11

  console.log node_modules/nock/lib/interceptor.js:332
    matching http://localhost:3000/api/auth/logout?token=fakeToken to DELETE http://localhost:3000/api/auth/logout?token=fakeToken: false

  console.error node_modules/jsdom/lib/jsdom/virtual-console.js:29
    Error: Error: Nock: No match for request {
      "method": "OPTIONS",
      "url": "http://localhost:3000/api/auth/logout?token=fakeToken",
      "headers": {
        "origin": "http://localhost",
        "access-control-request-method": "DELETE",
        "user-agent": "Mozilla/5.0 (darwin) AppleWebKit/537.36 (KHTML, like Gecko) jsdom/11.12.0",
        "host": "localhost:3000",
        "content-length": 0
      }
    }
        at Object.dispatchError (/Users/manuelcanero/OVO/lumo-webportal/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:65:19)
        at EventEmitter.client.on.err (/Users/manuelcanero/OVO/lumo-webportal/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:676:20)
        at EventEmitter.emit (events.js:165:20)
        at Request.preflightClient.on.err (/Users/manuelcanero/OVO/lumo-webportal/node_modules/jsdom/lib/jsdom/living/xhr-utils.js:412:47)
        at Request.emit (events.js:160:13)
        at Request.onRequestError (/Users/manuelcanero/OVO/lumo-webportal/node_modules/request/request.js:881:8)
        at OverriddenClientRequest.emit (events.js:160:13)
        at /Users/manuelcanero/OVO/lumo-webportal/node_modules/nock/lib/request_overrider.js:221:11
        at process._tickCallback (internal/process/next_tick.js:150:11) undefined

for me this seems to be the same
matching http://localhost:3000/api/auth/logout?token=fakeToken to DELETE http://localhost:3000/api/auth/logout?token=fakeToken: false

I got no issues using request under the other methods (POST, GET) the ones I've tried so far but with DELETE seems to be doing a CORS request so thats why I am getting first the request with the OPTIONS method?

If I change it to use POST for example it works completely fine or get but not DELETE 😔

does anyone has faced the same issue?

I am using "nock": "10.0.6"

paulmelnikow added a commit that referenced this issue May 2, 2019
@paulmelnikow
Copy link
Member

Hi! It sounds like browsers send an OPTIONS request before a DELETE request: https://stackoverflow.com/a/43485820/893113

So I guess that is what fetch is doing.

I just added a test of a DELETE request using got, which did not seem to trigger a second request.

If you want to keep using fetch, it looks like you need to add a mock for the OPTIONS request.

@paulmelnikow paulmelnikow added the support General questions or support. label May 2, 2019
gr2m pushed a commit that referenced this issue May 2, 2019
@paulmelnikow paulmelnikow changed the title nock.delete don't match request nock.delete doesn't match request (getting OPTIONS instead of DELETE) May 2, 2019
@Manny91
Copy link
Author

Manny91 commented May 3, 2019

Hi @paulmelnikow,

thanks for your response, yes indeed its that behaviour. I added an intercept for OPTIONS method and then the delete and it worked:

heres my example working code:

describe("logout", () => {
    it("success", async () => {
      nock.disableNetConnect();
      nock(baseURL)
        .persist()
        .log(console.log)
        .intercept("/api/auth/logout?token=fakeToken", "OPTIONS")
        .reply(200, null, {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "application:json"
        })
        .delete("/api/auth/logout?token=fakeToken")
        .reply(200, null, {
          "Access-Control-Allow-Origin": "*",
          "Content-Type": "application:json"
        });

      await fetch(baseURL + "/api/auth/logout?token=fakeToken", {
        method: "DELETE",
        headers: { "Content-Type": "application/json" }
      }).then(res => {
        expect(res.status).toBe(200);
      }); 
            
    });
  });

@Manny91 Manny91 closed this as completed May 3, 2019
gr2m pushed a commit that referenced this issue Sep 4, 2019
gr2m pushed a commit that referenced this issue Sep 4, 2019
gr2m pushed a commit that referenced this issue Sep 5, 2019
@Zeyvo92
Copy link

Zeyvo92 commented Jul 7, 2020

Hi, I had the same behaviour with jest on my test, I've added "testEnvironment": "node" on my jest config, and it's working well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support General questions or support.
Projects
None yet
Development

No branches or pull requests

3 participants