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 trymodule.exports = function(str){}
. Alternatively you can assign function as exports propexports.fc = function() {...};
By default
exports
is an empty object that points tomodule.exports
. When you overrideexports
, module.exports stays unchanged.