jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.24k stars 6.46k forks source link

Jest + Supertest + Restify not working #977

Closed ravilution closed 8 years ago

ravilution commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@cpojer Have you reopened? It still says closed.

cpojer commented 8 years ago

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

ravilution commented 8 years ago

@cpojer Any updates?

cpojer commented 8 years ago

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

ravilution commented 8 years ago

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

> jest-restify-supertest@1.0.0 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 commented 8 years ago

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

minhchu commented 7 years ago

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[bot] commented 3 years ago

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.