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

Disable Nock for a spec or a list of specs #7

Closed
assemmarwan opened this issue Jun 3, 2019 · 3 comments
Closed

Disable Nock for a spec or a list of specs #7

assemmarwan opened this issue Jun 3, 2019 · 3 comments

Comments

@assemmarwan
Copy link

I am trying to test Socket.IO functions in a spec file as well as other files containing HTTP requests using nock-record. The Socket.IO tests fail when run along with the tests but succeed when run alone (targeting one spec file).

Is there a way to disable them for a certain spec files?

@edorivai
Copy link
Owner

edorivai commented Jun 4, 2019

I'm not 100% sure that I understand your problem. But my guess is that the interceptors from your HTTP test files are intercepting Socket.IO requests.

This would be because nock-record uses Nock Recordings under the hood, which uses basic nock interceptors under the hood. They intercept all outbound HTTP requests within a single Node process as long as the recording is active. This cannot be split on a per-file basis, since it's actually overwriting the native http module under the hood.

One way you can counteract this would be to run each file in a separate node process. Jest does this by default for example.

@edorivai
Copy link
Owner

edorivai commented Sep 2, 2019

I'm going to close this issue, let me know if you have further questions

@edorivai edorivai closed this as completed Sep 2, 2019
@arminrosu
Copy link

Jest does this by default for example.

@edorivai that is not entirely true.

I have a test suite (let's call it "public") where one suite makes a request to the internet, and another that gets nocked (let's call it "walled").

If the "walled" suite is executed before the "public", the latter fails with:

NetConnectNotAllowedError {
  name: 'NetConnectNotAllowedError',
  code: 'ENETUNREACH',
  message: 'Nock: Disallowed net connect for "acme.com"',

This is due to how jest handles modules and is a known issue for both nock and jest

Running them in "public" > "walled" sequence with jest --runInBand works. Please note, we need --runInBand on our CI.

My fix was to call nock.restore after my "walled" suites. E.g.:

import { setupRecorder } from 'nock-record'
import * as nock from 'nock'

// ...
const record = setupRecorder()

describe('sample', () => {
  test('should retrieve user info', async () => {
    const { completeRecording, assertScopesFinished } = await record(
      'github-edorivai'
    )
    const result = await getGithubProfile('edorivai')

    completeRecording()
    assertScopesFinished()

    expect(result).toMatchSnapshot()
  })

  afterAll(() => {
    nock.restore()
  })
})

Hope that helps.

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

No branches or pull requests

3 participants