adding custom prelude to allow for stubbing require calls #14

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet
5 participants
@gr2m

gr2m commented Nov 29, 2013

This is quite a hack, rather quick than elegant. I'd like to
use it to discuss if such a feature (probably with a more
elegant API) would be something worth adding to karma-browserify?

For me personally, it would make a lot of sense, and my dev life
much easier. Let me know what you think.

Current API is as follows:

carMock = require('mocks/car')
global.stubRequire('path/to/car', carMock)

doSomethingThatRequiresCar()
expect(carMock.featur).to.be.called()

// revert the stub
global.unstubRequire('path/to/car')

makes sense?

@xdissent

This comment has been minimized.

Show comment Hide comment
@xdissent

xdissent Nov 30, 2013

Owner

Does browserify itself allow this somehow? If not, I'm still interested - this would be super helpful.

Owner

xdissent commented Nov 30, 2013

Does browserify itself allow this somehow? If not, I'm still interested - this would be super helpful.

@alexgorbatchev

This comment has been minimized.

Show comment Hide comment
@alexgorbatchev

alexgorbatchev Feb 13, 2014

Collaborator

@gr2m can this be pulled out into a separate module? I don't think this specifically belongs in karma-browserify but I can see how useful this could be.

Collaborator

alexgorbatchev commented Feb 13, 2014

@gr2m can this be pulled out into a separate module? I don't think this specifically belongs in karma-browserify but I can see how useful this could be.

@gr2m

This comment has been minimized.

Show comment Hide comment
@gr2m

gr2m Feb 13, 2014

@alexgorbatchev I don't know how, I'm patching _prelude.js directly, how would I do that in a separate module? Could you help me with that?

gr2m commented Feb 13, 2014

@alexgorbatchev I don't know how, I'm patching _prelude.js directly, how would I do that in a separate module? Could you help me with that?

@deltaidea

This comment has been minimized.

Show comment Hide comment
@deltaidea

deltaidea Apr 4, 2014

You can do this with Sinon.JS.

carMock = require "mocks/car"

originalRequire = require
sinon.stub global, "require", ( module ) ->
  # Return carMock for car, else require module normally,
  # so you can use require() even while it is stubbed.
  if module is "path/to/car"
    carMock
  else
    originalRequire module

doSomethingThatRequiresCar()

# Check that carMock has been called.

# Revert the stub.
require.restore()

P.S. Sorry for my coffee accent.

You can do this with Sinon.JS.

carMock = require "mocks/car"

originalRequire = require
sinon.stub global, "require", ( module ) ->
  # Return carMock for car, else require module normally,
  # so you can use require() even while it is stubbed.
  if module is "path/to/car"
    carMock
  else
    originalRequire module

doSomethingThatRequiresCar()

# Check that carMock has been called.

# Revert the stub.
require.restore()

P.S. Sorry for my coffee accent.

@deltaidea

This comment has been minimized.

Show comment Hide comment
@deltaidea

deltaidea Apr 4, 2014

Or like this if you want:

originalRequire = require
requireMap = {}
sinon.stub global, "require", ( module ) ->
  # You can still use require() even while it is stubbed.
  if requireMap[ module ]?
    requireMap[ module ]
  else
    originalRequire module

requireMap[ "path/to/car" ] = require "mocks/car"
requireMap[ "path/to/dog" ] = require "mocks/dog"
requireMap[ "path/to/cat" ] = require "mocks/cat"

doSomethingThatRequiresCar()

# Check that carMock has been called.

# Unstub only car:
requireMap[ "path/to/car" ] = null
# or revert the stub completely:
require.restore()

Or like this if you want:

originalRequire = require
requireMap = {}
sinon.stub global, "require", ( module ) ->
  # You can still use require() even while it is stubbed.
  if requireMap[ module ]?
    requireMap[ module ]
  else
    originalRequire module

requireMap[ "path/to/car" ] = require "mocks/car"
requireMap[ "path/to/dog" ] = require "mocks/dog"
requireMap[ "path/to/cat" ] = require "mocks/cat"

doSomethingThatRequiresCar()

# Check that carMock has been called.

# Unstub only car:
requireMap[ "path/to/car" ] = null
# or revert the stub completely:
require.restore()

@gr2m gr2m referenced this pull request in hoodiehq/hoodie Apr 16, 2014

Closed

[build] use gulp instead of grunt #284

0 of 2 tasks complete
@gr2m

This comment has been minimized.

Show comment Hide comment
@gr2m

gr2m Jul 12, 2014

That unfortunately does not work, because require === originalRequire is true. It ends up with RangeError: Maximum call stack size exceeded

gr2m commented Jul 12, 2014

That unfortunately does not work, because require === originalRequire is true. It ends up with RangeError: Maximum call stack size exceeded

@bendrucker bendrucker closed this Nov 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment