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

Jest + Supertest + Restify not working #977

Closed
ravilution opened this issue May 7, 2016 · 11 comments
Closed

Jest + Supertest + Restify not working #977

ravilution opened this issue May 7, 2016 · 11 comments

Comments

@ravilution
Copy link

node 5.4.1
jest-cli ^12.0.2
restify ^4.0.4
supertest ^1.2.0

I want to use Jest to do backend nodejs REST API testing also. I do not want to install another test tool like mocha etc.

'use strict';
var request = require('supertest')(require('../appServer'));
describe('Test', function () {
    it('works', function () {
        request
            .get('/api')
            .expect(200)
            .end(function (err, res) {
                expect(err).toEqual(null);
            });
    });
});

In above code appServer returns a restify server instance. I am using supertest because I want to do code coverage as well. But I get below error.

Error: Cannot find module '_http_common' from 'stream.js'
    at Runtime._resolveNodeModule (/Users/me/Documents/workspace/node_modules/jest-cli/src/Runtime/Runtime.js:451:11)
    at Object.<anonymous> (/Users/me/Documents/workspace/node_modules/spdy/lib/spdy/stream.js:13:20)
    at Object.<anonymous> (/Users/me/Documents/workspace/node_modules/spdy/lib/spdy.js:19:15)
@cpojer
Copy link
Member

cpojer commented May 9, 2016

Thank you for reporting this issue. Do you use mocking or is mocking disabled? If you'd like to test your modules/components "supertest" and "appServer" you'll have to unmock them first:

jest.unmock('supertest').unmock('../appServer');

@cpojer cpojer closed this as completed May 9, 2016
@cpojer
Copy link
Member

cpojer commented May 9, 2016

Oops, wrong button, reopening :)

If you can't figure it out, can you provide a repository that points this issue out so I can take a closer look? Thank you :)

@ravilution
Copy link
Author

ravilution commented May 9, 2016

I have automock turned off. I also tried you unmock code. I also tried jest.disableAutomock();. Nothing works. Below is my jest config in package.json

  "jest": {
    "automock": false,
    "bail": true,
    "collectCoverage": true,
    "moduleFileExtensions": [
      "js"
    ],
    "scriptPreprocessor": "./node_modules/babel-jest",
    "testFileExtensions": [
      "js"
    ],
    "testPathDirs": [
      "./client/src",
      "./server"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/client/build/",
      "/docs/",
      "/logs/",
      "/coverage/"
    ]
  }

@ravilution
Copy link
Author

ravilution commented May 9, 2016

@cpojer Have you reopened? It still says closed.

@cpojer
Copy link
Member

cpojer commented May 9, 2016

I boarded a long distance flight and my internet was cut-off before I could reopen this issue.

@cpojer cpojer reopened this May 9, 2016
@ravilution
Copy link
Author

@cpojer Any updates?

@cpojer
Copy link
Member

cpojer commented May 14, 2016

No, you haven't provided a full repository that highlights this problem so there is no way for me to confirm your issue.

@ravilution
Copy link
Author

ravilution commented May 14, 2016

Below is the code. I did not want to create a repo for two files. Please try and let me know

./package.json

{
  "name": "jest-restify-supertest",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest-cli": "^12.0.2",
    "supertest": "^1.2.0"
  },
  "jest": {
    "automock": false,
    "bail": true,
    "collectCoverage": true,
    "moduleFileExtensions": [
      "js"
    ],
    "testFileExtensions": [
      "js"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/coverage/"
    ]
  },
  "dependencies": {
    "restify": "^4.0.4"
  }
}

./tests/index-test.js

'use strict';

jest.disableAutomock();
jest.unmock('supertest').unmock('restify');

var restify = require('restify');
var app = restify.createServer();

app.get('/user', function (req, res) {
  res.status(200).json({ name: 'tobi' });
});

var request = require('supertest')(app);

describe('Test', function () {
  it('works', function () {
    request(app)
      .get('/user')
      .expect('Content-Type', /json/)
      .expect('Content-Length', '15')
      .expect(200)
      .end(function (err, res) {
        if (err) throw err;
      });
  });
});

Error Message

My-MacBook-Pro:jest-restify-supertest me$ npm test

> [email protected] test /Users/me/Documents/workspace/self/jest-restify-supertest
> jest

Using Jest CLI v12.0.2, jasmine2
Running 1 test suite...[SECURITY] node-uuid: crypto not usable, falling back to insecure Math.random()
 FAIL  __tests__/index-test.js
● Runtime Error
Error: Cannot find module '_http_common' from 'stream.js'
    at Runtime._resolveNodeModule (/Users/me/Documents/workspace/self/jest-restify-supertest/node_modules/jest-cli/src/Runtime/Runtime.js:451:11)
    at Object.<anonymous> (/Users/me/Documents/workspace/self/jest-restify-supertest/node_modules/spdy/lib/spdy/stream.js:13:20)
    at Object.<anonymous> (/Users/me/Documents/workspace/self/jest-restify-supertest/node_modules/spdy/lib/spdy.js:19:15)
----------|----------|----------|----------|----------|----------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
----------|----------|----------|----------|----------|----------------|
----------|----------|----------|----------|----------|----------------|
All files |      100 |      100 |      100 |      100 |                |
----------|----------|----------|----------|----------|----------------|

npm ERR! Test failed.  See above for more details.

@cpojer
Copy link
Member

cpojer commented Aug 5, 2016

I think this should be fixed by setting the "testEnvironment" config option to "node". jsdom doesn't deal well with this stuff.

@cpojer cpojer closed this as completed Aug 5, 2016
@minhchu
Copy link

minhchu commented Apr 13, 2017

I ran over this issue today and here is how I deal with it.
package.json:

...
"jest": {
    "testEnvironment": "node",
    "testPathIgnorePatterns": [
      "/node_modules/",
      "/dist/"
    ]
  }
...

__tests__/index.test.js:

import server from '../src/index.js';
import request from 'supertest';

afterEach(() => {
    server.close();
});

describe('Restify', () => {
    test('index route should return 200', (done) => {
        request(server)
            .get('/')
            .expect(200, done)
    });
});

I think the key is to pass done to the callback

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants