Can't work as a compiler with mocha #93

Closed
freewind opened this Issue May 20, 2012 · 16 comments

Comments

Projects
None yet
5 participants
@freewind

Mocha can specify compilers, so we don't need to convert other files to js files before testing.

But I try to:

mocha --compilers _js:streamline

which doesn't work properly. Is there way to let them work together?

@bjouhier

This comment has been minimized.

Show comment Hide comment
@bjouhier

bjouhier May 20, 2012

Owner

I tried it and it worked. My test file: foo._js

require('should');

describe('test1', function() {
    describe('#f1()', function() {
        it('should work', function(_) {
            setTimeout(_, 0);
            [1, 2, 3].indexOf(5).should.equal(-1);
        })
    })
})

I run it with:

mocha --compiler _js:streamline foo._js

Note: I had to install all packages with -g:

sudo npm install -g mocha
sudo npm install -g should
sudo npm install -g streamline
Owner

bjouhier commented May 20, 2012

I tried it and it worked. My test file: foo._js

require('should');

describe('test1', function() {
    describe('#f1()', function() {
        it('should work', function(_) {
            setTimeout(_, 0);
            [1, 2, 3].indexOf(5).should.equal(-1);
        })
    })
})

I run it with:

mocha --compiler _js:streamline foo._js

Note: I had to install all packages with -g:

sudo npm install -g mocha
sudo npm install -g should
sudo npm install -g streamline
@bjouhier

This comment has been minimized.

Show comment Hide comment
@bjouhier

bjouhier May 20, 2012

Owner

Ignore previous comment. My test was too simplistic. The source was not transformed at all but the test still worked!

The problem comes from the fact that require('streamline') does not register the require hooks. You have to call require('streamline').register() to register them.

I don't want to change require('streamline') because I want the ability to pass options to the register call.
I've fixed it in the dvlp branch by adding a streamline/register.js module that does the registration. With this additional file you should be able to run mocha as:

mocha --compilers _js:streamline/register foo._js
Owner

bjouhier commented May 20, 2012

Ignore previous comment. My test was too simplistic. The source was not transformed at all but the test still worked!

The problem comes from the fact that require('streamline') does not register the require hooks. You have to call require('streamline').register() to register them.

I don't want to change require('streamline') because I want the ability to pass options to the register call.
I've fixed it in the dvlp branch by adding a streamline/register.js module that does the registration. With this additional file you should be able to run mocha as:

mocha --compilers _js:streamline/register foo._js
@freewind

This comment has been minimized.

Show comment Hide comment
@freewind

freewind May 20, 2012

I have cloned the project to local and switched to dvlp branch. How can I install it?

I have cloned the project to local and switched to dvlp branch. How can I install it?

@freewind

This comment has been minimized.

Show comment Hide comment
@freewind

freewind May 20, 2012

What I'm doing now:

git clone https://github.com/Sage/streamlinejs.git
cd streamlinejs
git checkout dvlp

Then copy all the files from streamlinejs to C:\Users\Freewind\AppData\Roaming\npm\node_modules\streamline and the node_modules of my working project.

Then run mocha:

mocha --compilers _js:streamline/register

But it reports error:

module.js:337
    throw new Error("Cannot find module '" + request + "'");
          ^
Error: Cannot find module 'streamline/register'
    at Function._resolveFilename (module.js:337:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:359:17)
    at require (module.js:375:17)

What I'm doing now:

git clone https://github.com/Sage/streamlinejs.git
cd streamlinejs
git checkout dvlp

Then copy all the files from streamlinejs to C:\Users\Freewind\AppData\Roaming\npm\node_modules\streamline and the node_modules of my working project.

Then run mocha:

mocha --compilers _js:streamline/register

But it reports error:

module.js:337
    throw new Error("Cannot find module '" + request + "'");
          ^
Error: Cannot find module 'streamline/register'
    at Function._resolveFilename (module.js:337:11)
    at Function._load (module.js:279:25)
    at Module.require (module.js:359:17)
    at require (module.js:375:17)
@freewind

This comment has been minimized.

Show comment Hide comment
@freewind

freewind May 20, 2012

I checked the commit history, seems that you have just modified the version number in package.json, no other modification?

I checked the commit history, seems that you have just modified the version number in package.json, no other modification?

@bjouhier bjouhier closed this in e61ca33 May 20, 2012

@bjouhier

This comment has been minimized.

Show comment Hide comment
@bjouhier

bjouhier May 20, 2012

Owner

Ooops. I'm really sorry. I forgot to run the git add command. The dvlp branch should be ok now.

It is not published to NPM. What you can do is git clone it and then run npm install -g (without specifying any package) from inside the streamlinejs directory:

git clone https://github.com/Sage/streamlinejs.git
cd streamlinejs
git checkout dvlp
npm install -g
Owner

bjouhier commented May 20, 2012

Ooops. I'm really sorry. I forgot to run the git add command. The dvlp branch should be ok now.

It is not published to NPM. What you can do is git clone it and then run npm install -g (without specifying any package) from inside the streamlinejs directory:

git clone https://github.com/Sage/streamlinejs.git
cd streamlinejs
git checkout dvlp
npm install -g
@freewind

This comment has been minimized.

Show comment Hide comment
@freewind

freewind May 21, 2012

It works well now, thank you!

It works well now, thank you!

@aseemk

This comment has been minimized.

Show comment Hide comment
@aseemk

aseemk May 23, 2012

Contributor

I had run into this same problem w/ Expresso (Mocha's predecessor), so this is a welcome addition!

Contributor

aseemk commented May 23, 2012

I had run into this same problem w/ Expresso (Mocha's predecessor), so this is a welcome addition!

@yaronn

This comment has been minimized.

Show comment Hide comment
@yaronn

yaronn Jul 14, 2012

does this also work on coffeescript? I'm able to do it on _js but when I try _coffee like this:

mocha --compilers _coffee:streamline/register ui._coffee

I get "unexpected string" on the line "require 'assert'" so I assume the _js compiler is used instead of _coffee.

yaronn commented Jul 14, 2012

does this also work on coffeescript? I'm able to do it on _js but when I try _coffee like this:

mocha --compilers _coffee:streamline/register ui._coffee

I get "unexpected string" on the line "require 'assert'" so I assume the _js compiler is used instead of _coffee.

@aseemk

This comment has been minimized.

Show comment Hide comment
@aseemk

aseemk Jul 14, 2012

Contributor

Just spent some time investigating this. Interesting. But first, the current workaround/solution is to explicitly require CoffeeScript too/first:

mocha --compilers coffee:coffee-script _coffee:streamline/register ui._coffee

The details:

  • In streamline/register (lib/compiler/underscored.js), we don't require() CoffeeScript ourselves, since JS-only developers don't necessarily want that (since that auto-registers its require() hook and will cause .coffee files to get loaded).
  • Similarly then, we only register a ._coffee hook if CoffeeScript is already present / has already been require()'d and registered. Thus, you need to load CoffeeScript first here.
  • Mocha takes both the extension and the module name to require() because it uses the extension itself, since it loads files manually, not using Node's native require(). So unfortunately streamline/register doesn't receive that info that you're interested in ._coffee files.

One thing we could do here in Streamline is change the second bullet above: always register a ._coffee hook, even if CoffeeScript isn't present. But that might be undesirable for JS-only developers: ._coffee files would then load where they weren't before. (And at worst, they would fail to compile if CoffeeScript isn't present. We could try to require() CoffeeScript just-in-time here, but then .coffee files would also load.)

Tricky situation. What are your guys' thoughts?

Contributor

aseemk commented Jul 14, 2012

Just spent some time investigating this. Interesting. But first, the current workaround/solution is to explicitly require CoffeeScript too/first:

mocha --compilers coffee:coffee-script _coffee:streamline/register ui._coffee

The details:

  • In streamline/register (lib/compiler/underscored.js), we don't require() CoffeeScript ourselves, since JS-only developers don't necessarily want that (since that auto-registers its require() hook and will cause .coffee files to get loaded).
  • Similarly then, we only register a ._coffee hook if CoffeeScript is already present / has already been require()'d and registered. Thus, you need to load CoffeeScript first here.
  • Mocha takes both the extension and the module name to require() because it uses the extension itself, since it loads files manually, not using Node's native require(). So unfortunately streamline/register doesn't receive that info that you're interested in ._coffee files.

One thing we could do here in Streamline is change the second bullet above: always register a ._coffee hook, even if CoffeeScript isn't present. But that might be undesirable for JS-only developers: ._coffee files would then load where they weren't before. (And at worst, they would fail to compile if CoffeeScript isn't present. We could try to require() CoffeeScript just-in-time here, but then .coffee files would also load.)

Tricky situation. What are your guys' thoughts?

@yaronn

This comment has been minimized.

Show comment Hide comment
@yaronn

yaronn Jul 14, 2012

maybe use a separate register file for _coffee? there is already _coffee and _js as separate compilers

yaronn commented Jul 14, 2012

maybe use a separate register file for _coffee? there is already _coffee and _js as separate compilers

@yaronn

This comment has been minimized.

Show comment Hide comment
@yaronn

yaronn Jul 14, 2012

btw the workaround gives me this error (on windows 7):

at Object.statSync (fs.js:422:18)
at lookupFiles (C:\Users\naveh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:393:17)

for now I've built a batch to compile _coffee to js and run mocha as usual

yaronn commented Jul 14, 2012

btw the workaround gives me this error (on windows 7):

at Object.statSync (fs.js:422:18)
at lookupFiles (C:\Users\naveh\AppData\Roaming\npm\node_modules\mocha\bin\_mocha:393:17)

for now I've built a batch to compile _coffee to js and run mocha as usual

@aseemk

This comment has been minimized.

Show comment Hide comment
@aseemk

aseemk Jul 14, 2012

Contributor

Oh, I got the mocha syntax a bit wrong:

mocha --compilers coffee:coffee-script,_coffee:streamline/register ui._coffee

Contributor

aseemk commented Jul 14, 2012

Oh, I got the mocha syntax a bit wrong:

mocha --compilers coffee:coffee-script,_coffee:streamline/register ui._coffee

@yaronn

This comment has been minimized.

Show comment Hide comment
@yaronn

yaronn Jul 14, 2012

thanks, that works great!

On Sat, Jul 14, 2012 at 8:20 PM, Aseem Kishore <
reply@reply.github.com

wrote:

Oh, I got the mocha syntax a bit wrong:

mocha --compilers coffee:coffee-script,_coffee:streamline/register ui._coffee


Reply to this email directly or view it on GitHub:
#93 (comment)

I'm on Twitter (@yaronnaveh http://twitter.com/#!/YaronNaveh)

yaronn commented Jul 14, 2012

thanks, that works great!

On Sat, Jul 14, 2012 at 8:20 PM, Aseem Kishore <
reply@reply.github.com

wrote:

Oh, I got the mocha syntax a bit wrong:

mocha --compilers coffee:coffee-script,_coffee:streamline/register ui._coffee


Reply to this email directly or view it on GitHub:
#93 (comment)

I'm on Twitter (@yaronnaveh http://twitter.com/#!/YaronNaveh)

@aseemk

This comment has been minimized.

Show comment Hide comment
@aseemk

aseemk Jul 19, 2012

Contributor

Just wondering if anyone has any thoughts on this: is the current behavior the best? Or should we always add a hook for ._coffee files even if CoffeeScript hasn't been included yet at registration time? Or should we add an additional register file that includes CoffeeScript too, as @yaronn suggests?

Contributor

aseemk commented Jul 19, 2012

Just wondering if anyone has any thoughts on this: is the current behavior the best? Or should we always add a hook for ._coffee files even if CoffeeScript hasn't been included yet at registration time? Or should we add an additional register file that includes CoffeeScript too, as @yaronn suggests?

@ruxkor

This comment has been minimized.

Show comment Hide comment
@ruxkor

ruxkor Sep 16, 2012

I just stumbled upon the same issue (I forgot about the comma separation and tried to use spaces).

imho a small paragraph in the main readme would suffice, so this information can be seen at first glance.

ruxkor commented Sep 16, 2012

I just stumbled upon the same issue (I forgot about the comma separation and tried to use spaces).

imho a small paragraph in the main readme would suffice, so this information can be seen at first glance.

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