Ask coding questions

← Back to all posts
Unit Testing with Jasmine
julianjohannese (0)

I can't seem to get the unit testing to work in this file. I'm getting a type error when running my one test - "validSpacing is not a function." I've tried exporting the function from index.js in a couple of different ways (export, module.exports), but I get errors in index.js. Can anyone help?

julianjohannese (0)

Alright, I screwed up. I was just importing the function incorrectly in the test setup. Ugh.

Coder100 (17613)
  1. your js looks like this:
// CodeWars 7th Kyu problem - validSpacing
// Check string 's' for valid spacing
function validSpacing(s) {
  // There shouldn't be an mutation, but I'll 
  // copy the input anyway
  const t = s;
  // Do not allow leading or trailing spaces
  if (t.split("")[0] === " " || t.split("")[t.length-1] === " ") {
    return false;
  // Edge case for s === " "
  if (s === " ") return false 
  // Allow only one space between each word and
  // return true otherwise, including empty string
  return !/\s\s/.test(t);

validSpacing("ba aba aba ");

module.exports = { validSpacing };
  1. your tests look like this:
const { validSpacing } = require(".");
// ... tests you want to add ...
julianjohannese (0)

@Coder100 Sorry about that! That was the result of additional editing after asking the question.

Here's the test that's giving me a type error:

test("validSpacingTest", async function() {
expect(validSpacing("Hello, world ")).toBe(false);

btfuss (164)

The Unit Test works. When you add the unit test, dont include the entire code, only the contents of the function. Also, I didn't name my unit test the name of the function.