Test Harness with Mocha API












0















I'm working on an automated build process for a library which requires that all the modules that make it up to have their own unit tests, each written by a different author. Before the builder starts, I'd like to call a testing harness that does two things:




  1. Sets up chai.expect to use as the assertion library.

  2. Modifies a few of the JS prototypes to allow for testing (like making an array of the listeners set and triggered, or calling jsDOM).


While this can mostly be achieved through the command-line, I want to avoid having to call a bash script to run mocha with a bunch of options. It feels wrong to do that, you shouldn't need to call bash in a JS script. Plus I want the results of the unit tests to determine whether the build can continue or not, perhaps also alert the author of the module that there is an issue with the tests.



My current code looks basically identical to the example provided by the Mocha team here.



The test file structure is as follows:



test
├── testRunner.js
└── unitTests
├── test1.js
├── test2.js
├── test3.js


For goal number 1 above, I tried to simply const expect = require("chai").expect; in the testRunner file, but when the test runs it fails with "expect is not defined".



For goal number 2, I've tried to use the events described in the API documentation to set up the modified prototypes that are needed for several of the tests, with no success. The idea is to emulate the multiple, identical before and after calls that are present in several of the test suites.



The API documentation has no examples of use, and trying to listen for the events described hasn't work, probably because I'm listening to the wrong thing. like so:



...(code from the example linked above)


// Run the tests.
let mochaRunner = mocha.run(function(failures){
process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
});

mochaRunner.on('start', function(eventObj){
console.log(eventObj);
});


Perhaps the whole approach is wrong, any help is highly appreciated.










share|improve this question





























    0















    I'm working on an automated build process for a library which requires that all the modules that make it up to have their own unit tests, each written by a different author. Before the builder starts, I'd like to call a testing harness that does two things:




    1. Sets up chai.expect to use as the assertion library.

    2. Modifies a few of the JS prototypes to allow for testing (like making an array of the listeners set and triggered, or calling jsDOM).


    While this can mostly be achieved through the command-line, I want to avoid having to call a bash script to run mocha with a bunch of options. It feels wrong to do that, you shouldn't need to call bash in a JS script. Plus I want the results of the unit tests to determine whether the build can continue or not, perhaps also alert the author of the module that there is an issue with the tests.



    My current code looks basically identical to the example provided by the Mocha team here.



    The test file structure is as follows:



    test
    ├── testRunner.js
    └── unitTests
    ├── test1.js
    ├── test2.js
    ├── test3.js


    For goal number 1 above, I tried to simply const expect = require("chai").expect; in the testRunner file, but when the test runs it fails with "expect is not defined".



    For goal number 2, I've tried to use the events described in the API documentation to set up the modified prototypes that are needed for several of the tests, with no success. The idea is to emulate the multiple, identical before and after calls that are present in several of the test suites.



    The API documentation has no examples of use, and trying to listen for the events described hasn't work, probably because I'm listening to the wrong thing. like so:



    ...(code from the example linked above)


    // Run the tests.
    let mochaRunner = mocha.run(function(failures){
    process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
    });

    mochaRunner.on('start', function(eventObj){
    console.log(eventObj);
    });


    Perhaps the whole approach is wrong, any help is highly appreciated.










    share|improve this question



























      0












      0








      0








      I'm working on an automated build process for a library which requires that all the modules that make it up to have their own unit tests, each written by a different author. Before the builder starts, I'd like to call a testing harness that does two things:




      1. Sets up chai.expect to use as the assertion library.

      2. Modifies a few of the JS prototypes to allow for testing (like making an array of the listeners set and triggered, or calling jsDOM).


      While this can mostly be achieved through the command-line, I want to avoid having to call a bash script to run mocha with a bunch of options. It feels wrong to do that, you shouldn't need to call bash in a JS script. Plus I want the results of the unit tests to determine whether the build can continue or not, perhaps also alert the author of the module that there is an issue with the tests.



      My current code looks basically identical to the example provided by the Mocha team here.



      The test file structure is as follows:



      test
      ├── testRunner.js
      └── unitTests
      ├── test1.js
      ├── test2.js
      ├── test3.js


      For goal number 1 above, I tried to simply const expect = require("chai").expect; in the testRunner file, but when the test runs it fails with "expect is not defined".



      For goal number 2, I've tried to use the events described in the API documentation to set up the modified prototypes that are needed for several of the tests, with no success. The idea is to emulate the multiple, identical before and after calls that are present in several of the test suites.



      The API documentation has no examples of use, and trying to listen for the events described hasn't work, probably because I'm listening to the wrong thing. like so:



      ...(code from the example linked above)


      // Run the tests.
      let mochaRunner = mocha.run(function(failures){
      process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
      });

      mochaRunner.on('start', function(eventObj){
      console.log(eventObj);
      });


      Perhaps the whole approach is wrong, any help is highly appreciated.










      share|improve this question
















      I'm working on an automated build process for a library which requires that all the modules that make it up to have their own unit tests, each written by a different author. Before the builder starts, I'd like to call a testing harness that does two things:




      1. Sets up chai.expect to use as the assertion library.

      2. Modifies a few of the JS prototypes to allow for testing (like making an array of the listeners set and triggered, or calling jsDOM).


      While this can mostly be achieved through the command-line, I want to avoid having to call a bash script to run mocha with a bunch of options. It feels wrong to do that, you shouldn't need to call bash in a JS script. Plus I want the results of the unit tests to determine whether the build can continue or not, perhaps also alert the author of the module that there is an issue with the tests.



      My current code looks basically identical to the example provided by the Mocha team here.



      The test file structure is as follows:



      test
      ├── testRunner.js
      └── unitTests
      ├── test1.js
      ├── test2.js
      ├── test3.js


      For goal number 1 above, I tried to simply const expect = require("chai").expect; in the testRunner file, but when the test runs it fails with "expect is not defined".



      For goal number 2, I've tried to use the events described in the API documentation to set up the modified prototypes that are needed for several of the tests, with no success. The idea is to emulate the multiple, identical before and after calls that are present in several of the test suites.



      The API documentation has no examples of use, and trying to listen for the events described hasn't work, probably because I'm listening to the wrong thing. like so:



      ...(code from the example linked above)


      // Run the tests.
      let mochaRunner = mocha.run(function(failures){
      process.exitCode = failures ? 1 : 0; // exit with non-zero status if there were failures
      });

      mochaRunner.on('start', function(eventObj){
      console.log(eventObj);
      });


      Perhaps the whole approach is wrong, any help is highly appreciated.







      node.js unit-testing mocha chai






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 14:39







      Asinus Rex

















      asked Nov 23 '18 at 11:52









      Asinus RexAsinus Rex

      152117




      152117
























          0






          active

          oldest

          votes











          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%2f53446210%2ftest-harness-with-mocha-api%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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%2f53446210%2ftest-harness-with-mocha-api%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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'