0

I am currently using Jasmine ("jasmine": "^2.4.1") with Node v6.1.0

I am both trying to provide and consume multiple modules in one file - however keep getting errors like object undefined or null etc.

I have played around with different syntax styles but can't quite get it right, where am I going wrong?

The documentation is for both Jasmine and RequireJs is just not helping.

In my module I export like this:

function Player() {
}

Player.prototype.play = function(song) {
  this.currentlyPlayingSong = song;
  this.isPlaying = true;
};

Player.prototype.pause = function() {
  this.isPlaying = false;
};

Player.prototype.resume = function() {
  if (this.isPlaying) {
    throw new Error("song is already playing");
  }

  this.isPlaying = true;
};

Player.prototype.makeFavorite = function() {
  this.currentlyPlayingSong.persistFavoriteStatus(true);
};

module.exports = Player;

function Song() {
}

Song.prototype.persistFavoriteStatus = function(value) {
  // something complicated
  throw new Error("not yet implemented");
};

module.exports = Song;

And when I consume it in my Spec:

var { Player, Song } = require('../app/example-module');
shenku
  • 11,969
  • 12
  • 64
  • 118

1 Answers1

0

In your example the second module.exports cancels the first - module.exports = Player; is overwritten by module.exports = Song; - and Player is never actually exported.

Without knowing why you need to define and consume multiple modules from a single file, I suggest you try to use one file per module, which results in more files but a simpler overall code structure.

If your concern with multiple files is about bundling and distribution, there a various build tools available that can bundle them into a single file for precisely this purpose.

I hope this is helpful.

Steve M
  • 21
  • 3