Home  >  Q&A  >  body text

Javascript - Mocha Chai unit tests not running

I'm working on a project with the Front End Web Development course bootcamp and unfortunately our professor can't answer this question of mine.

I have a simple war card game that takes two players and plays it for 26 turns to see who wins. In this game I have 4 classes as follows

These are set up as modules so that they can be exported and then imported into any corresponding JS files that require them to run.

Then I have a set of unit test files which will then try to use each corresponding file and then generate tests.html for the tests

This will access each corresponding unit test js file and then run it via the described method. However, they don't actually run the corresponding "it" function for the unit test (this is verified by the description and console.log() in the it function. When I try to go to the console on the web browser, it This will also do it without throwing any errors for debugging.

The first link is the master branch I committed in week 6, you will see that the tests work when I don't import and then have the corresponding classes in the description. https://github.com/jeffhennen/Projects/tree/master/Week6/Week6Final

I want it to use live code in files instead of unit tests, while also keeping each corresponding file separate, like I did in this section. https://github.com/jeffhennen/Projects/tree/Test/Week6/Week6Final

This is my test.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
    <div id = "mocha"><p><a href=".">Index</a></p></div>
    <div id = "messages"></div>
    <div id = "fixtures"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script src="node_modules/chai/chai.js"></script>
    <script type="module" src="Scripts/Card.js"></script>
    <script type="module" src="Scripts/Deck.js"></script>
    <script type="module" src="Scripts/Player.js"></script>
    <script type="module" src="Scripts/War.js"></script>
    <script>mocha.setup('bdd')</script>
    <script type="module" src="UnitTests/CardTest.js"></script> 
    <script type="module" src="UnitTests/DeckTest.js"></script>
    <script type="module" src="UnitTests/PlayerTest.js"></script>
    <script>mocha.run();</script>

</body>
</html>

Here is an example of my CardTest.js file

var expect = chai.expect;
import Card from '../Scripts/Card.js';

describe('Card Functions', () => {

    describe('Constructor', () => {

        console.log("Inside card describe constructor");
        let card = new Card("Club", "King", 13);
        console.log(card);
        it('Should create the card with the value of the card\'s suit equal to param 0', () => {

            console.log("test1");
            expect(card._suit).to.equal("Club");
        });

        it('Should create the card with the value of the card\'s string value equal to the param 1', () => {

            console.log("test2");
            expect(card._number).to.equal("King");
        });

        it('Should assign the numeric value of the card\'s string value to the card object', () => {

            console.log("test3");
            expect(card._value).to.equal(13);
        });
    });
});

This is my Card.js file

class Card{

    constructor(suit, number, value){

        this._suit = suit;
        this._value = value;
        this._number = number;
    }

    get suit(){

        return this._suit;
    }

    get value(){

        return this._value;
    }

    get number(){

        return this._number;
    }
    
}
export default Card;

P粉964682904P粉964682904181 days ago373

reply all(1)I'll reply

  • P粉786800174

    P粉7868001742024-04-03 11:37:23

    I found that to get it running, all I needed to do was update tests.html to the following, I just updated the tags to

    sssccc
    
    
    
    
    
    
        
    
    
        
        
    sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc sssccc

    reply
    0
  • Cancelreply