thlorenz / proxyquireify

browserify >= v2 version of proxyquire. Mocks out browserify's require to allow stubbing out dependencies while testing.
MIT License
152 stars 24 forks source link

Karma+AutoWatch+Coffee tests: proxyquireify doesn't run transform when changing test #29

Closed mpfluger closed 9 years ago

mpfluger commented 9 years ago

First off, thanks for making the changes to get proxyquireify working with transforms. This has made our setup a bit easier. There is a problem we run into though. Everything works fine when running the test suite the first time, but after changing one of the tests, the transform doesn't run again. The tests are written in CoffeeScript.

Here's the output after changing a test file:

INFO [watcher]: Changed file "~/projects/My-Wallet-HD/tests/hdwallet_spec.js.coffee".
ERROR [framework.browserify]: bundle error
ERROR [framework.browserify]: ~/projects/My-Wallet-HD/tests/hdwallet_spec.js.coffee:1
/* proxyquireify injected requires to make browserify include dependencies in the bundle */;require('../src/wallet');var BigInteger, Bitcoin, HDWallet, MyWallet, WalletCrypto, WalletStore, assert, bs58check, proxyquire, stubs;
^
ParseError: regular expressions cannot begin with `*`

So it does try to bundle it again, but it doesn't transform the CoffeeScript test to JS when changing the file.

Relevant part of karma.conf.js:

    frameworks: ['jasmine', 'browserify'],
    autoWatch: true,

    preprocessors: {
      'tests/**/*.coffee' : ['browserify']
    },

    browserify: {
      configure: function(bundle) {
        bundle.on('prebundle', function() {
          bundle.transform('coffeeify');
          bundle.transform('browserify-istanbul');
          bundle.plugin('proxyquireify/plugin');
        });
      },
      debug: true
    },

    coffeePreprocessor: {
      options: {
        bare: true,
        sourceMap: true
      },
      transformPath: function(path) {
        return path.replace(/\.coffee$/, '.js');
      }
    },

    files: [
      'tests/**/*.coffee',
      {pattern: 'src/**/*.js', included: false},
bendrucker commented 9 years ago

I need a reproduction that I can run to dig into this. Haven't touched CoffeeScript in years.

mpfluger commented 9 years ago

The simple sample project:

https://github.com/mpfluger/proxyquireify_coffee

npm test will run fine, but when making a change to test/a_spec.js.coffee it shows the error seen above.

Thanks for looking into this!

bendrucker commented 9 years ago

Not related to proxyquireify actually:

bundle.once('prebundle', handler)

If you re-register transforms and plugins every time, the second time around you have CS -> PQ -> CS -> PQ. The CS transform gets and chokes on valid JS from PQ.

mpfluger commented 9 years ago

So simple, so true ;)

Thanks!

mpfluger commented 9 years ago

So this fixes the problem when editing a test, but it breaks it for editing source files. Any ideas?

bendrucker commented 9 years ago

Nothing comes to mind. Since this isn't a proxyquireify issue, I'm happy to continue discussion at karma-browserify if you're able to do some further investigation and propose a solution.

mpfluger commented 9 years ago

The problem was that I included both the src/ and test/ files for karma to watch. Removing the src/ files fixed the problem for me. And the bundle would still get created again after touching a src/ file.