samedi 14 décembre 2019

How to unit test pure Javascript business logic that will run on the frontend

I am working on implementing a card-based hidden roles party game as a single-page Vue.js app backed by a Django API. I am trying to have all of the actual game logic run in the frontend, and just use the API for synchronizing all of the players in the game, etc. I have a set of functions which are pure "business logic" which build the deck of cards based on a JSON configuration file, and perform other game-rule specific processing, but don't directly depend on API calls or UI behavior. As this code will be central to the game working correctly, I definitely want to build a good set of unit tests for it, but I'm not sure of the easiest way to test "pure" javascript.

I currently have Jasmine setup to test this file, having it export the functions to be tested with module.exports, and I can see the passing tests. However, when I try to import the file into my Vue.js component, and run it with the Webpack dev server I get the following error:

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Object.eval (gamelogic.js?df67:64)
    at eval (gamelogic.js:73)
    at Object../src/gamelogic.js (app.js:1567)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (selector.js?type=script&index=0!./src/components/Game.vue:4)
    at Object../node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/Game.vue (app.js:964)
    at __webpack_require__ (app.js:679)
    at fn (app.js:89)
    at eval (Game.vue?ec12:1)

My export line is currently module.exports = buildDeck, and I import it into my Vue component with import gameLogic from "../gamelogic". The error message leads me to believe that it is the export portion that doesn't work correctly, but that is what I need to do to allow Jasmine to access my code for testing.

I've imported other pre-made node modules into my Vue application and they work correctly, but I haven't found any clear resources on how to set up code as a node module that also plays nicely as a module imported in the frontend with Webpack. I would appreciate any advice that you might have, and if necessary I can give more of my code to help with diagnosing the issue.

Aucun commentaire:

Enregistrer un commentaire