I'm developing a Node.js module, and I want to use Karma to auto-test it while working.
In my config file, I setup this:
// list of files / patterns to load in the browser
files: [
'./index.js',
'./test/indexSpecs.js'
],
Obviously, since Node.js isn't included in the browser files, I get this error:
Uncaught ReferenceError: require is not defined
If i add:
files: [
'./node_modules/**/*.js',
'./index.js',
'./test/indexSpecs.js'
],
I get a bunch of errors. I think js files get loaded in alphabetical order, which is wrong.
I also think that Node.js cannot be run in a browser, so what I'm trying to do may be totally wrong. Is there an alternative?
I'm developing a Node.js module, and I want to use Karma to auto-test it while working.
You should not. Karma is designed for client-side code.
To auto-test your code, the simplest way is to create a npm script similar to this one (with mocha):
"scripts": {
"test": "mocha ./**",
"test:watch": "npm run test -- -w"
}
Then, use npm test
to run the tests on demand, or npm run test:watch
to continuously run the tests.
You can also use a grunt or gulp script with a watch task if you prefer.
You are right about karma not being a good fit for testing server side code. It is going to run everything in the context of a browser, which is causing the issues you are seeing. If you wanted to develop a module for the server and the client you could use karma in conjunction with browserfiy, but you would still need to run the tests in a node environment.
Instead, I would suggest using mocha : a simple and powerful test runner that works great for testing node modules.
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.