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.
Related
I have a usable workflow that I want to make better. I'm building a JS library, and the way I am executing smoke tests on code is by using webpack to package the library and write it to a file that is included in an HTML file for viewing the effects of the code.
To do this, I make changes to the file in Eclipse, save it, then I must leave Leave eclipse and go to Terminal to run "npm run buildInbrowser" to execute "webpack --config inbrowser.config.js".
The configuration works perfect with regards to webpack, the configuration, and the npm setup, but when I try to configure eclipse to run those commands, it brings up an error: "env: node: No such file or directory" I've attached screenshots of my launch NPM configuration.
My system is MacOSX Catalina using Nodeclipse, npm v9.3.1 and node 16.18.0.
Again, there is no issue with me running these commands in terminal, but they won't run through node. This makes me think it's something simple that I overlooked.
As nitind pointed out, I had incorrect syntax on the PATH variable for eclipse, which was causing the problem. Also noted is that Eclipse did not populate my path variable by default, so i did have to manually enter it in. See the screen shot for the fix.
First of all, I am really new to NPM stuffs. But I do know React and PHP. So I have figured myself to create a CMS system using PHP as a backend and React as a frontend with the help of CDNs from Babel and React(And ofc axios to make data requests and sends). However, I do want to get into more proper way with webpack with the actual website. So, I have followed along the tutorial from this article. He has explained quite extraordinarily. However, he uses HTML whilst in my case, I have a PHP. So since I am not fully aware of what I am doing. I have redirected the HTMLWebPlugin to my PHP File in webpack.config.js.
plugins: [
new HtmlWebPackPlugin({
template: "./../index.php",
filename: "./index.php"
})
However, when I make changes the code and refreshes it, the webpage will not adapt to the changes unless I run "npm run build" for the next time. I do know I am running it from the built app. And this is because I am rather making changes on the entry files (index.js) when the webpage is rendering the output files (dist/main.js). But is this okay to connect these two and is there a way to automatically adapt to changes I make in the entry files?
So finally, I have found my solution. When you want to re-run "npm run build" every time a file changes. You need to install watch via npm. It checks all the files inside a directory and when you change something or on-save, it will re-run all the scripts inside package.json. So steps -
Install watch by "npm install watch"
When watch is installed, add "watch": "watch 'npm run build' ./directory-you-want-to-track"
Run "npm run watch"
Use this command:
tsc src/index.ts --watch --outDir ./lib
ref: https://www.youtube.com/watch?v=F6aHIh5NglQ
Yes, there is a way to solve this issue. You can use webpack's Hot Module Replacement feature. It's is just running webpack in development mode with proper set of config which you should find in webpack official documentation.
If you are using Vite Laravel plugin open package.json
install watch
npm install watch
and on scripts change it to
"build": "vite build --watch"
It should automatically update when you make changes
I've been starting to use create-react-app for my React projects, and as a testing library it comes with Jest.
As part of my React apps, I like to create integration tests (as well as unit tests) as I find it useful to be able to check the happy paths in the app are working as expected. For example: Render (mount) a page using Enzyme, but only mock out the http calls (using Sinon) so that the full React/Redux flow is exercised at once.
Before using create-react-app, I've used Mocha for testing, and I found mocha-steps to be a great extension for integration tests, as it allows tests in a group to be executed in sequence, and handles stopping if a step fails without stopping the entire test run.
Question: Is there any way to get Jest to behave in a similar way? Specifically, I'd like to be able to specify a series of tests in a group (such as in a describe) and have them execute sequentially in order.
I've been looking through the Jest docs, or for any other libraries that extend it, but I've come up empty. For now, it feels like the only option is to have one large test which makes me sad, and I'd prefer not to swap out Jest for Mocha if I can avoid it.
Thanks!
jest does not currently support flagging specific tests to be run serially (like ava's test.serial).
This has been requested before but I don't think they are working on it.
The workaround is for you to identify which test files are to be run concurrently and which ones are to be run serially.
For example, I usually name my unit tests with *.test.js and my integration tests with *.spec.js.
I can then run my unit tests concurrently.
$ jest '(/__tests__/.*\\.test)\\.js$'
And I can run my integration tests serially.
$ jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand
I can combine the two in my package.json.
"scripts": {
"unit": "jest '(/__tests__/.*\\.test)\\.js$'",
"integration": "jest '(/__tests__/.*\\.spec)\\.ts$' --runInBand",
"test": "npm run unit && npm run integration"
}
Running npm test can then run concurrent tests first and then the serial ones next.
I've built a single-page app off of chentsulin's electron/react boilerplate, which comes with the following node script for testing:
"test": "cross-env NODE_ENV=test BABEL_DISABLE_CACHE=1 mocha --compilers js:babel-register --recursive --require ./test/setup.js test/**/*.spec.js"
The boilerplate comes with a number of generic tests that are runnable at the outset (I confirmed this myself). It also comes with a number of webpack configs to suit different environments.
The app is fairly far along now, and is using electron-json-storage to handle local storage. I just went back to writing tests, and when I run the test node script (with or without --renderer) I get the following:
[dirpath]/node_modules/electron-json-storage/lib/utils.js:30
const app = electron.app || electron.remote.app;
^
TypeError: Cannot read property 'app' of undefined
I've tried using webpack.IgnorePlugin to ignore electron-json-storage, but that doesn't do anything. My guess is that electron-json-storage is trying to refer to an instantiated electron session that doesn't exist. What's the easiest way of dummying this up?
The problem was that I was using mocha (which came as part of the Electron-React boilerplate). Installing electron-mocha and changing the test script in the package.json resolved this problem immediately.
I have some mocha tests that I run from a docker container which tests some services in other running docker containers.
Right now, I have a shell script that finds all the mocha js files, de-newlines them and passes them as an argument to mocha itself. That script then gets run in a docker container as the dockerfile CMD.
This works ok, but it is kind of hacky and is starting to get ugly with several dozen js files.
In javaland, I'd let maven run these, but I figure there must be something better suited for node/javascript.
You can either use mocha --recursive path/to/tests if you want recursively go through all folders and run all files as tests, or you can use globs to pass to mocha like mocha tests/**/test-*.js to filter out specific files matching a pattern.