I've started a new role and there is currently no front end unit testing going on and I want to change that. I've had some experience of Karma and Mocha using the Expect library when I've been doing React dev work in the past, so I was planning on sticking to that.
What I am working on is basically a website with considerable JS which runs the application. There's a bunch of jQuery in there as well which I'll need to sort out, but for now I'm just focussing on unit testing the JS. There are at least 100 files I've seen so far.
My setup seems good, npm test runs as I would expect it to. However, I am having some issues as none of the JavaScript has any exports set in it. I'm not sure how much of a problem this is, but I'm not in a position to change all of the production code to test it, so I'm hoping I can find another way. Essentially, I need to do the following:
examplefile.js
function add(a,b) {
return a+b;
}
examplefile.test.js
var expect = require('expect');
var examplefile = require('../../js/examplefile');
describe('examplefile', function() {
it('should exist', () => {
expect(examplefile).toExist();
});
describe('Add functionality', function() {
it('should be able to add numbers together', () => {
expect(add(2,2)).toEqual(4);
});
});
});
karma.conf.js
module.exports = function(config) {
config.set({
// ... normal karma configuration
browsers: ['Chrome'],
singleRun: true,
frameworks: ['mocha'],
files: [
// all files ending in ".test.js"
{pattern: './tests/**/*.test.js', watched: false},
// each file acts as entry point for the webpack configuration
],
preprocessors: {
// add webpack as preprocessor
'./tests/**/*.test.js': ['webpack']
},
webpack: {
// karma watches the test entry points
},
reporters: ['mocha'],
client: {
mocha: {
timeout: '5000'
}
},
webpackMiddleware: {
// webpack-dev-middleware configuration
stats: 'errors-only'
},
});
};
So when I run npm test I get one passing test (should exist) and then ReferenceError: add is not defined So how am I going to bring in the functions from my JS files to test them?
Aucun commentaire:
Enregistrer un commentaire