moll / node-mitm

Intercept and mock outgoing Node.js network TCP connections and HTTP requests for testing. Intercepts and gives you a Net.Socket, Http.IncomingMessage and Http.ServerResponse to test and respond with. Super useful when testing code that hits remote servers.
Other
636 stars 48 forks source link

Cannot read property 'prototype' of undefined #19

Closed psech closed 9 years ago

psech commented 9 years ago

I'm going to have my tests with jasmiene maintained by karma test runner (including browserify adapter).

Test project

var Mitm = require("mitm");

describe('HAL Utility', function () {
//  beforeEach(function () { this.mitm = Mitm(); });
//  afterEach(function () { this.mitm.disable(); });

    it('should return RUNNING for running instance', function () {
        expect('RUNNING').toBe('RUNNING');
    });
});

My karma config is quite common:

    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['browserify', 'jasmine'],

    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
      'src/modules/*.js': ['browserify']
    },

    // web server port
    port: 9876,

    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome', 'PhantomJS'],

After running

node node_modules/karma/bin/karma start karma.conf.js --singleRun

I have got following output:

INFO [karma]: Karma v0.12.31 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.8 (Mac OS X)]: Connected on socket Y3ZlYiRBAZDuF923wMAt with id 63186634
INFO [Chrome 42.0.2311 (Mac OS X 10.10.3)]: Connected on socket 7o59wVOykbU13NoZwMAs with id 46537050
INFO [framework.browserify]: 1091378 bytes written (1.28 seconds)
INFO [framework.browserify]: bundle built
PhantomJS 1.9.8 (Mac OS X) ERROR
  TypeError: 'undefined' is not an object (evaluating 'Net.Socket.prototype')
  at /var/folders/5v/ft23hsts6hs9qjwxvkkzllj40000gp/T/39a05f4fb3614de9e6a3949c0615e8b6.browserify:7118:0 <- node_modules/mitm/lib/socket.js:6:0

Chrome 42.0.2311 (Mac OS X 10.10.3) ERROR
  Uncaught TypeError: Cannot read property 'prototype' of undefined
  at /var/folders/5v/ft23hsts6hs9qjwxvkkzllj40000gp/T/39a05f4fb3614de9e6a3949c0615e8b6.browserify:7118:0 <- node_modules/mitm/lib/socket.js:6:0
moll commented 9 years ago

Hey,

Thanks for the elaborate report and question. Could it be because you're trying to run it in a browser? Mitm is unfortunately only for Node.js (and Io.js though I've not yet tried it). If you want to hijack browser's XMLHttpRequest requests you could look into http://sinonjs.org or https://www.npmjs.com/package/faux-jax or any similar modules.

psech commented 9 years ago

Thank you for your answer.

I will do following:

moll commented 9 years ago

check browserless testing approach (basing on your automated tests :))

That depends on whether you really want to test your JavaScript code that uses Node's net module or whether you want to test your front-end code. The two libs I mentioned work in the browser because they're for testing front-end code. Mitm.js is, currently at least, only for testing the Node.js side of things. ;-)

investigate possibilities to force browserify to run mitm in browser

That's probably not going to work as browsers don't implement networking the same way as Node.js does.

I'll close this issue as it doesn't have actionable items, but feel free to carry on the conversation. :-) Good luck!