Ask coding questions

← Back to all posts
Unit Test Error - JavaScript
dcalmeyer (0)

I am using the Teams for Classroom, and am excited to implement unit tests to dramatically cut down on time spent grading. The unit tests work fine for Java and Python, but I am getting errors saying my functions aren't defined when unit testing in JS. Here is a link to an example Repl: https://replit.com/join/byanefur-dcalmeyer. Any help would be greatly appreciated.

Comments
hotnewtop
dcalmeyer (0)

Perfect, thanks!!

Coder100 (16898)

Make sure you export the functions, like this:

function add(a, b) {
  return a+b;
}

module.exports = { add };

and here is a working test script:

const { add } = require(".");
expect(add(1, 2)).toBe(3);