简体   繁体   中英

Testing with jasmine-node : understanding error

I'm trying to learn javascript by doing some of the exercises over on exercism.io (which provides pre-written tests that we need to make pass). It was going okay for a while but with this latest exercise I can't seem to figure out how the tests work.

The aim of the exercise is to write a programme that will count the number of times a given string appears within another string. This is how the test file looks:

var words = require('./word-count');

describe("words()", function() {
  it("counts one word", function() {
    var expectedCounts = { word: 1 };
    expect(words("word")).toEqual(expectedCounts);
  });

  // more skipped tests  

});

The first thing I did was create the word-count.js file but I don't know how code in that file needs to be formatted/setup to be read by the spec file. After glancing through this tutorial I setup word-count.js like so:

exports = function(str){}

However this (and everything else I've tried) gives me the error

  1) words() counts one word
   Message:
     TypeError: object is not a function
   Stacktrace:
     TypeError: object is not a function
    at null.<anonymous> (/home/sheeka/exercism/javascript/word-count/word-count_test.spec.js:6:12)

I don't understand this error, or what it's looking for. I also don't understand how the tests are working since it doesn't look to me like the spec file is calling on any functions or variables from word-count.js

Any help understanding this or pointing me in the direction of resources I could use to figure this out would be much appreciated. I did try running a google search but everything I've found looks much too advanced for my purposes.

Assigning exports = function(str){} don't make your function public. please try module.exports = function(str){} . Alternatively you can assign function as exports prop exports.fc = function() {...};

By default exports is an empty object that points to module.exports . When you override exports , module.exports stays unchanged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM