3

How do I execute different Testcases with different structure through NodeJS and Mocha.

Moving forward I intend to integrate Selenium + NodeJS + Mocha

I have just started to explore NodeJS with Mocha and need some help.

  1. Installed node.js:

    C:\Users\AtechM_03>node -v
    v6.11.2
    
  2. Installed npm:

    C:\Users\AtechM_03>npm -v
    3.10.10
    
  3. Configured nodeclipse as per this link and my Project structure looks like:

enter image description here

  1. Installed Mocha at the default location (through command-line) as per this link.

    C:\Users\AtechM_03>npm install -g mocha
    C:\Users\AtechM_03\AppData\Roaming\npm\mocha -> C:\Users\AtechM_03\AppData\Roaming\npnode_modules\mocha\bin\mocha
    C:\Users\AtechM_03\AppData\Roaming\npm\_mocha -> C:\Users\AtechM_03\AppData\Roaming\n\node_modules\mocha\bin\_mocha
    C:\Users\AtechM_03\AppData\Roaming\npm
    `-- mocha@3.5.3
    
  2. Followed this link to write a program in NodeJS integrating Mocha.

  3. Created a directory named test with in NodeProject space.

  4. Within test folder created a file named test.js

  5. Executed npm init to interactively create a package.json file.

        C:\Users\AtechM_03>cd C:\Users\AtechM_03\LearnAutmation\NodeProject
    
        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm init
        This utility will walk you through creating a package.json file.
        It only covers the most common items, and tries to guess sensible defaults.
    
        See `npm help json` for definitive documentation on these fields
        and exactly what they do.
    
        Use `npm install <pkg> --save` afterwards to install a package and
        save it as a dependency in the package.json file.
    
        Press ^C at any time to quit.
        name: (NodeProject) test
        version: (1.0.0) 1.0.0
        description: test123
        entry point: (index.js) test.js
        test command: (mocha) mocha
        git repository:
        keywords:
        author: debanjan
        license: (ISC)
        About to write to C:\Users\AtechM_03\LearnAutmation\NodeProject\package.json:
    
        {
          "name": "test",
          "version": "1.0.0",
          "description": "test123",
          "main": "test.js",
          "directories": {
            "test": "test"
          },
          "dependencies": {
            "g": "^2.0.1",
            "selenium-webdriver": "^3.5.0"
          },
          "devDependencies": {
            "mocha": "^3.5.3"
          },
          "scripts": {
            "test": "mocha"
          },
          "author": "debanjan",
          "license": "ISC"
        }
    
    
        Is this ok? (yes)
    
    C:\Users\AtechM_03\LearnAutmation\NodeProject>
    
  6. package.json got generated within the Project Scope i.e. under C:\Users\AtechM_03\LearnAutmation\NodeProject as follows:

        {
          "name": "test",
          "version": "1.0.0",
          "description": "test123",
          "main": "test.js",
          "directories": {
            "test": "test"
          },
          "dependencies": {
            "g": "^2.0.1",
            "selenium-webdriver": "^3.5.0"
          },
          "devDependencies": {
            "mocha": "^3.5.3"
          },
          "scripts": {
            "test": "mocha"
          },
          "author": "debanjan",
          "license": "ISC"
        }
    
  7. Added code to test.js as follows:

            // Require the built in 'assertion' library
            var assert = require('assert');
            // Create a group of tests about Arrays
            describe('Array', function() {
              // Within our Array group, Create a group of tests for indexOf
              describe('#indexOf()', function() {
                // A string explanation of what we're testing
                it('should return -1 when the value is not present', function(){
                  // Our actual test: -1 should equal indexOf(...)
                  assert.equal(-1, [1,2,3].indexOf(4));
                });
              });
    
            //Create a test suite (group) called Math
              describe('Math', function() {
                  // Test One: A string explanation of what we're testing
                  it('should test if 3*3 = 9', function(){
                // Our actual test: 3*3 SHOULD EQUAL 9
                assert.equal(9, 3*3);
                  });
                  // Test Two: A string explanation of what we're testing
                  it('should test if (3-4)*8 = -8', function(){
                // Our actual test: (3-4)*8 SHOULD EQUAL -8
                assert.equal(-8, (3-4)*8);
                  });
              });
    
            });
    
  8. Executed npm test from project space which runs successfully:

            C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
            > temperature@1.0.0 test C:\Users\AtechM_03\LearnAutmation\NodeProject
            > mocha
    
    
    
              Array
                #indexOf()
                  v should return -1 when the value is not present
                Math
                  v should test if 3*3 = 9
                  v should test if (3-4)*8 = -8
    
    
              3 passing (18ms)
    
  9. Followed this link to write a second program in NodeJS integrating Mocha.

  10. Created a separate directory named temperature with in NodeProject space.
  11. In the temperature directory created a file named app.js and a folder name test
  12. Within the test folder, created a file named test.js
  13. Moved the previous package.json to a sub-directory and executed npm init to interactively create a new package.json file again.

            C:\Users\AtechM_03>cd C:\Users\AtechM_03\LearnAutmation\NodeProject
    
            C:\Users\AtechM_03\LearnAutmation\NodeProject>npm init
            This utility will walk you through creating a package.json file.
            It only covers the most common items, and tries to guess sensible defaults.
    
            See `npm help json` for definitive documentation on these fields
            and exactly what they do.
    
            Use `npm install <pkg> --save` afterwards to install a package and
            save it as a dependency in the package.json file.
    
            Press ^C at any time to quit.
            name: (NodeProject) temperature
            version: (1.0.0) 1.0.0
            description: temp
            entry point: (index.js) app.js
            test command: (mocha) mocha
            git repository:
            keywords:
            author: debanjanb
            license: (ISC)
            About to write to C:\Users\AtechM_03\LearnAutmation\NodeProject\package.json:
    
            {
              "name": "temperature",
              "version": "1.0.0",
              "description": "temp",
              "main": "app.js",
              "directories": {
                "test": "test"
              },
              "dependencies": {
                "g": "^2.0.1",
                "selenium-webdriver": "^3.5.0"
              },
              "devDependencies": {
                "mocha": "^3.5.3"
              },
              "scripts": {
                "test": "mocha"
              },
              "author": "debanjanb",
              "license": "ISC"
            }
    
    
        Is this ok? (yes)
    
  14. New package.json gets created as follows:

            {
              "name": "temperature",
              "version": "1.0.0",
              "description": "temp",
              "main": "app.js",
              "directories": {
                "test": "test"
              },
              "dependencies": {
                "g": "^2.0.1",
                "selenium-webdriver": "^3.5.0"
              },
              "devDependencies": {
                "mocha": "^3.5.3"
              },
              "scripts": {
                "test": "mocha"
              },
              "author": "debanjanb",
              "license": "ISC"
            }
    
  15. The current temperature Testcase looks like:

enter image description here

  1. Tried to execute this second program through npm test from the Project space but it still executes the first program as follows:

        C:\Users\AtechM_03\LearnAutmation\NodeProject>npm test
    
        > temperature@1.0.0 test C:\Users\AtechM_03\LearnAutmation\NodeProject
        > mocha
    
    
    
          Array
            #indexOf()
              v should return -1 when the value is not present
            Math
              v should test if 3*3 = 9
              v should test if (3-4)*8 = -8
    
    
          3 passing (18ms)
    

Question :

I know my second program app.js is incomplete and executing it will show error (e.g. 0 passing (20ms)) but my app.js is not getting invoked at all.

Can someone please guide/suggest me what I am doing wrong here?

Any suggestion/guide/pointer will be helpful.

Update:

As of now my current code for app.js is incomplete and contains the following code:

        cToF = function(celsius) {
          if(!Number.isInteger(celsius)) return undefined;
          return celsius * 9 / 5 + 32;
        }

        fToC = function(fahrenheit) {
          if(!Number.isInteger(fahrenheit)) return undefined;
          return (fahrenheit - 32) * 5 / 9;
        }

As per this website I am following I expect an error as 0 passing (20ms)

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

3

You've got quite the long description but the thing I extracted from it is that you have essentially a structure like this:

NodeProject
├── temperature
│   └── test
└── test

Then you go into NodeProject and run Mocha. By default Mocha will look only for test in the same directory where it is invoked. So it won't look for temperature/test. If you want Mocha to run the tests in temperature/test you have to tell Mocha explicitly. For instance, this would work:

mocha test temperature/test

I'll address a common misconception here because I often see people make the mistake: merely using the --recursive flag is not enough. If you use this flag, then after Mocha has identified directories in which to find tests, it will look in them recursively. However, it does not change how Mocha identifies directories in which to find tests. Specifically, if you use mocha --recursive, it will still only look in test, and it will look in subdirectories of test. This won't make it look in temperature/test. If you do have subdirectories in test and temperature/test, you could do:

mocha --recursive test temperature/test
Louis
  • 146,715
  • 28
  • 274
  • 320