ember-browserify icon indicating copy to clipboard operation
ember-browserify copied to clipboard

npm-installed dependencies are not available during tests

Open djackson-sykes opened this issue 10 years ago • 22 comments

As an example:

npm install --save-dev ember-browserify
npm install --save-dev moment

From app/router.js

import moment from 'npm:moment';

works just fine, but in any test module (e.g. tests/adapters/application.js), the same line provokes an error:

Uncaught Error: Could not find module npm:moment

Environment:

  • ember-cli: 0.1.5
  • ember: 1.8.1

Tests using ember-cli-mocha.

djackson-sykes avatar Jan 07 '15 23:01 djackson-sykes

Thanks for reporting this.

I think fixing this may require changes in ember-cli proper, because I'm not seeing a hook that will let me postprocess the tests tree.

ef4 avatar Jan 09 '15 03:01 ef4

Depends on this: https://github.com/ember-cli/ember-cli/pull/2939

Once that's merged ~~ember-browserify 0.5.0~~ should fix this.

ef4 avatar Jan 09 '15 04:01 ef4

I originally released this in 0.5.0 but hid it behind an environment variable in 0.6.0 because I don't want to break anybody who upgrades prematurely before ember-cli releases.

ef4 avatar Jan 09 '15 05:01 ef4

Thanks for being so quick with this!

djackson-sykes avatar Jan 09 '15 05:01 djackson-sykes

I was about to enable this by default now that the ember-cli change shipped, but I found a bug.

This browserifies the app and test trees separately, which results in two copies of any module that's used in both. If you expected them to share state, you can have nasty bugs.

Working on a fix.

ef4 avatar Jan 24 '15 06:01 ef4

I haven't had a chance to fix this yet. The functionality is deployed and available for people who want it, but you need to turn it on by setting the environment variable BROWSERIFY_TESTS=true. The only caveat is the bug I mentioned above -- you get two independent copies of any module that you import from both app and tests.

ef4 avatar Feb 07 '15 18:02 ef4

What work needs to happen for this to land without the BROWSERIFY_TESTS flag? This has bitten us a few times and it's potentially a blocking issue for our build environment.

Thanks!

eoneill avatar Mar 11 '15 07:03 eoneill

To do this correctly we'd need to crawl both the app and vendor trees to gather dependencies and then call browserify only once, so that we don't get duplicate modules. Instead of running browserify separately for each tree.

ef4 avatar Mar 12 '15 17:03 ef4

:+1: for this. Browserify is the way forward, importing its modules is so nice but need it in tests.

johnnyshields avatar Mar 13 '15 10:03 johnnyshields

this module is brilliant, i'm also very much hoping to be able to use this in my tests

mgenev avatar Apr 08 '15 06:04 mgenev

@ef4: can I throw a PR at you to move BROWSERIFY_TESTS=true to a flag in environment.js?

xkb avatar May 09 '15 05:05 xkb

@ef4 pinging on this. We really would like first-class browserify support for Ember, and w/out this issue resolved we are having messy hacks to get our tests to work.

johnnyshields avatar Jun 19 '15 06:06 johnnyshields

The ember-cli folks are working on a big internal refactor that will make ember-browserify-like functionality first-class.

ef4 avatar Jun 19 '15 21:06 ef4

@ef4 is there an issue I can follow on ember-cli for this?

johnnyshields avatar Jul 23 '15 16:07 johnnyshields

https://github.com/ember-cli/ember-cli/issues/4211

ef4 avatar Jul 23 '15 17:07 ef4

Thanks for the workaround @xkb & @ef4.

I'm still running into issues unfortunately :( I've confirmed that L28 is getting executed.

I get the following error when trying to import sinon-chai for ember-mocha.

Error: Could not find module `npm:sinon-chai` imported from `my-project/tests/unit/controllers/delete-activity-test`

via

import sinonChai from "npm:sinon-chai";

rupurt avatar Oct 01 '15 13:10 rupurt

As a temporary solution I'm importing the package indirectly through a dummy file in the app tree:

app/helpers/my-cool-npm-package.js

import myCoolNpmPackage from 'npm:my-cool-npm-package';
export default myCoolNpmPackage;

tests/unit/models/product.js

import myCoolNpmPackage from 'hello-world/helpers/my-cool-npm-package'

Works fine.

jonnedeprez avatar Dec 17 '15 15:12 jonnedeprez

@jonnedeprez does this mean that my-cool-npm-package is bundled into production builds? I have an npm-dependency that I need to use only in my tests.

oliverwilkie avatar Jan 05 '16 17:01 oliverwilkie

@oliverwilkie You can manually exclude the package from production builds

// config/environment.js
module.exports = function(environment) {
  var ENV = {
    environment: environment,
    baseURL: '/',
    locationType: 'auto',

    // ...

    browserify: {
      // your browserify options if you have any
      ignores: [ ]
    },
  };

  if (environment === 'production') {
    ENV.browserify.ignores.push('my-cool-npm-package');
  }

  return ENV;
};

dwickern avatar Jan 08 '16 21:01 dwickern

@stefanpenner curious as to why you closed this issue? Is there a linked issue that resolves this?

johnnyshields avatar Apr 27 '16 06:04 johnnyshields

Yeah, as far as I know there is still a legit unresolved issue here. Trying to use ember-browserify from the tests tree is likely to troll people. The TESTS flag described above is not a full solution, just a different bad behavior that is preferable for some use cases.

ef4 avatar Apr 27 '16 18:04 ef4

any updates on this?

izelnakri avatar Jan 12 '17 15:01 izelnakri