Ember Cli Rails and Feature tests: a story of environment inconsistencies when testing

Using pure magic

Bod was building its application using this magical tool called ember-cli-rails.

Why did Bod use this tool?

Because Ember cli rails allows you to unify an Ember CLI app and a Ruby on Rails app. You get the benefits of both worlds. It is worth to take a look at the Gem README to realize all the pros and const of using this gem. Bod felt in love with this gem.

Continuing with Bod story…After Bod finished the basic setup of the gem, everything just work. It was pure magic!!!

Following the advice of the RoR community, Bob started to write tests, a lot of feature tests. Thanks to ember-cli-rails writing end to end tests is quite simple, so, Bod was very happy writing tests for everything in his app. The tests were a bit slow, but, he was happy, he was able to ensure all his application was working perfectly.

But, something happened, Bod started to use the Ember ENV variable inside his ember application, and everything started to behave rare…

He was into panic!!! the magic had betrayed him

Bod started to feel that his application was laughing on him, he felt sad. But, Bod decided to investigate what was happening.

Researching into magic

All Bod’s research can be summarize in this sentence:

THE EMBER ENVIRONMENT IS EQUAL TO “development” WHEN RUNNING FEATURE TESTS!!!

Bod started to dig int the `ember-cli-rails` code and found the reason for the problem inside the code base

After doing an analysis of the situation, it makes sense. Ember CLI use the environment to build the application, and the behavior of the application when the environment is equal to test is not desired when running feature tests. On the other hand, RAILS ENV and EMBER ENV should be consistent and `EmberCli.env` should be consistent with the env used to build the ember application.

There are some other reasons to try to build the application in testing mode instead of development. The ember-cli-rails gem build and watch the application when the environment is equal to development, but, watching is not necessary.

Tweaking the magic

It is VERY UNDERSTANDABLE why the environment used to build the app should be either development or production. The limitation is inherit from Ember CLI. Ember CLI does some configurations to be able to run the tests in the nice single page we see tests when we run ember test –server.

Bod was conscious of that, but he really wanted to have a consistent environment in both sites of the application when running tests, that’s why he decided to make the following changes:

Ember World

Bod added a conditional to verify if the ember application was running inside the Rails process, which allow him to disable the specific Ember configurations for testing and disable Ember Mirage to use the API end points for end to end tests.

Rails World

The ember-cli-rails gem was limited by the Ember test environment constraints, so, building the application with the current implementation was imposible. It is necessary to monkey patch.

It is possible to monkey patch the gem in a rails initializer and allow it to build the ember application with test environment.

After implementing those changes the EMBER_ENV and RAILS_ENV are when running feature tests!!!

Conclusion

  • Fix the inconsistency between EMBER_ENV and RAILS_ENV is possible, but the Ember configuration in testing mode make it complicate to adopt, because you have to modify some code that ember auto generates by default.
  • The inconsistency between EMBER_ENV and RAILS_ENV is understandable but not right. Probably people is not going to feel the difference if they don’t use ENV.environment, but it doesn’t feel ok that it returns development when running tests.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *