Rails 5 testing with Jasmine - “export class” required for Jasmine tests, but breaks my pages





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have a few javascript files which are define helper classes which used in some of my pages which use Angular for the user interaction. I just require these files in manifest for the controllers that need them, and it's all working fine.



The problem has come in trying to test these classes using Jasmine. My Jasmine tests were throwing type errors until I put the "export" keyword in front of the javascript class definitions in these files. Then the tests worked fine, but the web pages that use these classes would throw a syntax error on encountering the export keyword.



The javascript classes are pretty straightforward, just two ES6 class definitions like this;



class ProjectBudget {
constructor() {

this._map = new Map();
this._total = 0;
this._postedTotal = 0;
}
load(rowObject) {
[...]
}
//other methods
}

class ProjectBudgetItem {

constructor(id, name, amount, parent, hasBudgetDetail = false) {
this._accountId = id;
this._accountName = name;
this._rowState = "loaded";

}
//various methods
}


The test looks like this:



import   {ProjectBudget, ProjectBudgetItem} from "../../app/assets/javascripts/budgeting_scripts/project_budget_classes.js"

describe("ProjectBudgets", () => {
it("can create a projectBudget", () => {
// expect(foo).toEqual("Foo");

let pb = new ProjectBudget();

expect(pb.dirty).toEqual(false);
let obj={accountId: 3, name: "Carpenters", budgetAmount: 1000, hasDetailedbudget: false}
pb.load(obj)
expect(pb.total).toEqual(1000);
})
});


The only way I can get the test to run is to change
'class ProjectBudget' to 'export class ProjectBudget' otherwise I get an error like this:



 FAILED TESTS:


ProjectBudgets
✖ can create a projectBudget
Chrome 70.0.3538 (Mac OS X 10.13.6)
TypeError: o.ProjectBudget is not a constructor
at <Jasmine>
at UserContext.<anonymous> (webpack:///spec/javascripts/budget_spec.js:7:17 <- spec/javascripts/budget_spec-2f776bfad8fdb52497f7.js:1:711)
at <Jasmine>


Unfortunately once I do this, when the my application loads the webpage which uses these classes, the browser throws a syntax error on the word "export".



Not sure how to resolve this conflict.










share|improve this question































    0















    I have a few javascript files which are define helper classes which used in some of my pages which use Angular for the user interaction. I just require these files in manifest for the controllers that need them, and it's all working fine.



    The problem has come in trying to test these classes using Jasmine. My Jasmine tests were throwing type errors until I put the "export" keyword in front of the javascript class definitions in these files. Then the tests worked fine, but the web pages that use these classes would throw a syntax error on encountering the export keyword.



    The javascript classes are pretty straightforward, just two ES6 class definitions like this;



    class ProjectBudget {
    constructor() {

    this._map = new Map();
    this._total = 0;
    this._postedTotal = 0;
    }
    load(rowObject) {
    [...]
    }
    //other methods
    }

    class ProjectBudgetItem {

    constructor(id, name, amount, parent, hasBudgetDetail = false) {
    this._accountId = id;
    this._accountName = name;
    this._rowState = "loaded";

    }
    //various methods
    }


    The test looks like this:



    import   {ProjectBudget, ProjectBudgetItem} from "../../app/assets/javascripts/budgeting_scripts/project_budget_classes.js"

    describe("ProjectBudgets", () => {
    it("can create a projectBudget", () => {
    // expect(foo).toEqual("Foo");

    let pb = new ProjectBudget();

    expect(pb.dirty).toEqual(false);
    let obj={accountId: 3, name: "Carpenters", budgetAmount: 1000, hasDetailedbudget: false}
    pb.load(obj)
    expect(pb.total).toEqual(1000);
    })
    });


    The only way I can get the test to run is to change
    'class ProjectBudget' to 'export class ProjectBudget' otherwise I get an error like this:



     FAILED TESTS:


    ProjectBudgets
    ✖ can create a projectBudget
    Chrome 70.0.3538 (Mac OS X 10.13.6)
    TypeError: o.ProjectBudget is not a constructor
    at <Jasmine>
    at UserContext.<anonymous> (webpack:///spec/javascripts/budget_spec.js:7:17 <- spec/javascripts/budget_spec-2f776bfad8fdb52497f7.js:1:711)
    at <Jasmine>


    Unfortunately once I do this, when the my application loads the webpage which uses these classes, the browser throws a syntax error on the word "export".



    Not sure how to resolve this conflict.










    share|improve this question



























      0












      0








      0








      I have a few javascript files which are define helper classes which used in some of my pages which use Angular for the user interaction. I just require these files in manifest for the controllers that need them, and it's all working fine.



      The problem has come in trying to test these classes using Jasmine. My Jasmine tests were throwing type errors until I put the "export" keyword in front of the javascript class definitions in these files. Then the tests worked fine, but the web pages that use these classes would throw a syntax error on encountering the export keyword.



      The javascript classes are pretty straightforward, just two ES6 class definitions like this;



      class ProjectBudget {
      constructor() {

      this._map = new Map();
      this._total = 0;
      this._postedTotal = 0;
      }
      load(rowObject) {
      [...]
      }
      //other methods
      }

      class ProjectBudgetItem {

      constructor(id, name, amount, parent, hasBudgetDetail = false) {
      this._accountId = id;
      this._accountName = name;
      this._rowState = "loaded";

      }
      //various methods
      }


      The test looks like this:



      import   {ProjectBudget, ProjectBudgetItem} from "../../app/assets/javascripts/budgeting_scripts/project_budget_classes.js"

      describe("ProjectBudgets", () => {
      it("can create a projectBudget", () => {
      // expect(foo).toEqual("Foo");

      let pb = new ProjectBudget();

      expect(pb.dirty).toEqual(false);
      let obj={accountId: 3, name: "Carpenters", budgetAmount: 1000, hasDetailedbudget: false}
      pb.load(obj)
      expect(pb.total).toEqual(1000);
      })
      });


      The only way I can get the test to run is to change
      'class ProjectBudget' to 'export class ProjectBudget' otherwise I get an error like this:



       FAILED TESTS:


      ProjectBudgets
      ✖ can create a projectBudget
      Chrome 70.0.3538 (Mac OS X 10.13.6)
      TypeError: o.ProjectBudget is not a constructor
      at <Jasmine>
      at UserContext.<anonymous> (webpack:///spec/javascripts/budget_spec.js:7:17 <- spec/javascripts/budget_spec-2f776bfad8fdb52497f7.js:1:711)
      at <Jasmine>


      Unfortunately once I do this, when the my application loads the webpage which uses these classes, the browser throws a syntax error on the word "export".



      Not sure how to resolve this conflict.










      share|improve this question
















      I have a few javascript files which are define helper classes which used in some of my pages which use Angular for the user interaction. I just require these files in manifest for the controllers that need them, and it's all working fine.



      The problem has come in trying to test these classes using Jasmine. My Jasmine tests were throwing type errors until I put the "export" keyword in front of the javascript class definitions in these files. Then the tests worked fine, but the web pages that use these classes would throw a syntax error on encountering the export keyword.



      The javascript classes are pretty straightforward, just two ES6 class definitions like this;



      class ProjectBudget {
      constructor() {

      this._map = new Map();
      this._total = 0;
      this._postedTotal = 0;
      }
      load(rowObject) {
      [...]
      }
      //other methods
      }

      class ProjectBudgetItem {

      constructor(id, name, amount, parent, hasBudgetDetail = false) {
      this._accountId = id;
      this._accountName = name;
      this._rowState = "loaded";

      }
      //various methods
      }


      The test looks like this:



      import   {ProjectBudget, ProjectBudgetItem} from "../../app/assets/javascripts/budgeting_scripts/project_budget_classes.js"

      describe("ProjectBudgets", () => {
      it("can create a projectBudget", () => {
      // expect(foo).toEqual("Foo");

      let pb = new ProjectBudget();

      expect(pb.dirty).toEqual(false);
      let obj={accountId: 3, name: "Carpenters", budgetAmount: 1000, hasDetailedbudget: false}
      pb.load(obj)
      expect(pb.total).toEqual(1000);
      })
      });


      The only way I can get the test to run is to change
      'class ProjectBudget' to 'export class ProjectBudget' otherwise I get an error like this:



       FAILED TESTS:


      ProjectBudgets
      ✖ can create a projectBudget
      Chrome 70.0.3538 (Mac OS X 10.13.6)
      TypeError: o.ProjectBudget is not a constructor
      at <Jasmine>
      at UserContext.<anonymous> (webpack:///spec/javascripts/budget_spec.js:7:17 <- spec/javascripts/budget_spec-2f776bfad8fdb52497f7.js:1:711)
      at <Jasmine>


      Unfortunately once I do this, when the my application loads the webpage which uses these classes, the browser throws a syntax error on the word "export".



      Not sure how to resolve this conflict.







      ruby-on-rails rspec jasmine ruby-on-rails-5 rspec-rails






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 19:55







      user1023110

















      asked Nov 25 '18 at 3:27









      user1023110user1023110

      1,0341726




      1,0341726
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The solution in this case was to eliminate the import statement and include the scripts I need (without the 'export' keyword) under the 'files' key in karma.conf.js. Maybe not the best solution as it would be nice to be able to specify in the test what files to load rather than in the configuration file, but at least it worked.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464394%2frails-5-testing-with-jasmine-export-class-required-for-jasmine-tests-but-br%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            The solution in this case was to eliminate the import statement and include the scripts I need (without the 'export' keyword) under the 'files' key in karma.conf.js. Maybe not the best solution as it would be nice to be able to specify in the test what files to load rather than in the configuration file, but at least it worked.






            share|improve this answer




























              0














              The solution in this case was to eliminate the import statement and include the scripts I need (without the 'export' keyword) under the 'files' key in karma.conf.js. Maybe not the best solution as it would be nice to be able to specify in the test what files to load rather than in the configuration file, but at least it worked.






              share|improve this answer


























                0












                0








                0







                The solution in this case was to eliminate the import statement and include the scripts I need (without the 'export' keyword) under the 'files' key in karma.conf.js. Maybe not the best solution as it would be nice to be able to specify in the test what files to load rather than in the configuration file, but at least it worked.






                share|improve this answer













                The solution in this case was to eliminate the import statement and include the scripts I need (without the 'export' keyword) under the 'files' key in karma.conf.js. Maybe not the best solution as it would be nice to be able to specify in the test what files to load rather than in the configuration file, but at least it worked.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 26 '18 at 23:34









                user1023110user1023110

                1,0341726




                1,0341726
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464394%2frails-5-testing-with-jasmine-export-class-required-for-jasmine-tests-but-br%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Xamarin.form Move up view when keyboard appear

                    JBPM : POST request for execute process go wrong