Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable object properties not being set properly in before/after #539

Open
meowsus opened this issue Apr 2, 2018 · 1 comment
Open
Labels

Comments

@meowsus
Copy link

meowsus commented Apr 2, 2018

I've been messing around with this for about a week and feel completely crazy at this point.

In our application we have modules. We also have a configuration object which impacts these modules.

// config.js
(function () {
    'use strict';

    window.config = window.config || {};

    window.config.moduleName = {
        value: ''
    };
})();

The spec helper loads our manifest

// spec_helper.js
//= require application

Which loads the deluge of javascript in our application (which I've omitted a ton of in this example)

# application.js.erb

<%
  # 3rd Party Dependencies
  %w(
    i18n
    lodash
    jquery3
    # etc, etc
  ).each do |asset|
    require_asset asset
  end

  # Configuration
  %w(
    config
  ).each do |asset|
    require_asset asset
  end

  # Modules
  %w(
    module
    mayhem
    meltdown
    # etc, etc
  ).each do |asset|
    require_asset asset
  end

Suppose I have the following code in my Foobar module, which has it's own config

// in config.js
(function () {
    'use strict';

    window.config = window.config || {};

    window.config.foobar = {
        delay: 4000
    };
})();

// in foobar.js
registerModule('foobar', (function () {
    'use strict';

    return {
        init: function () {
            _.delay(function () {
                $('.baz').remove();
            }, config.foobar.delay);
        }
    }
})();

This will delete the .baz element after 4000 milliseconds. Here's my test:

(function () { 
    'use strict';

    describe('foobar', function () {
        describe('init', function () {
            it('should eventually remove .baz', function (done) {
                fixture.set('<div class="baz"></div>');

                foobar.init();

                _.delay(function () {
                    expect($('.baz').length).to.equal(0);
                    done();
                }, config.foobar.delay);
            });
        });
    });
})();

As is, this test would fail, since 4000 is twice as long as the default 2000 timeout. So my test should utilize this approach to change the config, just for this test:

(function () { 
    'use strict';

    var delay = config.foobar.delay;

    before(function () {
        config.foobar.delay = 100; // of any value below 2000
    });

    after(function () {
        config.foobar.delay = delay;
    });

    describe('foobar', function () {
        describe('init', function () {
            it('should eventually remove .baz', function (done) {
                fixture.set('<div class="baz"></div>');

                foobar.init();

                _.delay(function () {
                    expect($('.baz').length).to.equal(0);
                    done();
                }, config.foobar.delay);
            });
        });
    });
})();

However, this fails consistently for me. The done method is not being called within 2000 milliseconds.

If I comment out the config.foobar.delay = delay; within the after function, the test will consistently pass.

console.loging any string in the before and after test shows that they seem to be being excuted at the expected times, but from the behavior I'm seeing, it's almost as though the after function is being executed during the test run.

Has anyone seen this or does anyone know what might be causing this issue?

@mathieujobin
Copy link
Collaborator

I'm sorry no one ever answered to you here, but after 3 years... I wondering.. did you ever figure this out ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants