Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Running tests outside of the Treehouse console

I've set up my docs very similar to how Gil is teaching but I continue to get en error in the terminal:

<code> ReferenceError: document is not defined at Object.<anonymous> (/Users/steveuser/coding/javascript/hangman/script.js:1:84)</code>

What I've done:

1) created the function with a module.exports in my script.js file

const buildAlphabet = (arr) => {
  arr.map((letter, index) => {
    let htmlpattern = `<div class="letter" id="${letter}">${letter}</div>`;
    alphabetKeys.innerHTML += htmlpattern;
  });
}

module.exports.buildAlphabet = buildAlphabet;

2) imported that function in my testing file called alphabet_test

var expect = require('chai').expect;

describe('buildAlphabet', () => {
  var buildAlphabet = require('../../script').buildAlphabet;

  it('should accept an array as an argument', () => {

  })
});

I've quadruple checked the path to my script.js file which is sitting at the root of this project directory, and so I'm wondering if I need another utility to get this to work. My test files are in hangman/hangman_tests/test/alphabet_test.js

My initial test that I set up with Gil worked fine in the terminal: the Sanity check.

var expect = require('chai').expect;

// Test Suite
describe('Mocha', () => {
  it('should run our tests using NPM', () => {
    expect(true).to.be.ok;
  });
})

I then kept following the tutorial...everything seems very straightforward...but can't get te second test document to find my script.js file.

Thanks for any help you can provide.

2 Answers

Okay, I think I may understand that 'document' is referring to the browser host object and not my script.js file, which is what I thought was happening.

I do have a number of document.querySelectors in my script.js file.

That would make sense why the Sanity Check passed but my script.js is not passing since sanity check doesn't mention the host document object.

Can anyone recommend a current stable npm utility to get 'document' defined?

If that is what's happening....

Nothing like talking to myself to answer my own problem. Well, actually, it was others' problem, too and they solved it with this: https://github.com/rstacruz/jsdom-global

I'm using it like this and that error message has gone away when running npm test

var expect = require('chai').expect;
const jsdom = require('jsdom-global')();

describe('buildAlphabet', () => {
  var buildAlphabet = require('../../script').buildAlphabet;
  jsdom();
  it('should accept an array as an argument', () => {

  })
});