I am new to TDD and using Intern v4 to write my unit tests. I've gone through the documentation but i'm still having trouble writing the test. Would appreciate if someone could point me in the right direction.
My javascript code is as follows:
var app = (function(){
let name = 'Mufasa';
let fullname = '';
return {
print: function(surname) {
fullname = `hello ${name} ${surname}`;
console.log(fullname);
return fullname;
}
}
})();
app.print('Vader');
I have included intern in my project via npm.
My unit test file test1.js is as follows:
const { suite, test } = intern.getInterface('tdd');
const { assert } = intern.getPlugin('chai');
// how do i write my first test case here to check the output when i pass/do not pass the surname parameter
Since you're using non-modular JS, your application will be loaded into the global namespace (i.e., it will be accessible as window.app
). A test might look like:
suite('app', () => {
test('print', () => {
const result = app.print('Smith');
assert.equal(result, 'hello Mufasa Smith');
})
});
For Intern to be able to call your app.print
function, your application script will need to be loaded. Assuming your code is intended for the browser, you can either use Intern's loadScript
function in your test suite, or you can load your script as a plugin.
suite('app', () => {
before(() => {
return intern.loadScript('path/to/script.js');
});
// tests
});
or
// intern.json
{
"plugins": "path/to/script.js"
// rest of config
}