What I do
Hint: I’m more than a software developer
Software delivery is hard. There are many tiny details that need to be considered, technologies to master, and business cases to learn. Delivering working software is a huge challenge.
It’s also expensive. A small software development team (2 good developers and a project manager) can easily cost $250,000/year.
Finally, it’s risky. Studies have shown that most projects are 6 to 12 months behind schedule and 50 to 100% over budget.
We try to deliver software:
- With high quality
- On budget
- On time
I present topics and give talks to help developers and Agile Managers hit those goals.
This site serves as an information resource for .NET Developers and Agile Managers to get a glimpse into how I work in my capacity as an SDLC auditor. This serves as a starting point for further conversation.
On the .NET Developer side, I focus on two topics:
- Staying on track
- Keeping the quality high
- Working with management
On the Agile Management side:
- Be efficient (or ruthless)
- Measure and adjust
There are also some fun articles on the .NET Micro Framework and embedded hardware programming. It’s a side hobby of mine and I love sharing what I’ve learned.
Unit Testing in WebStorm 11
I recently wrote a series of post on using Visual Studio to write Node.js. Specifically, I detailed how to write unit tests using Mocha. Here I show the same detail but using WebStorm from Jetbrains. Webstorm was designed for Node.js development. Let’s get started and learn the differences.
Clone the NodejsTestingSimple project
WebStorm allows developers to clone projects directly from GitHub. This feature is very nice and hopefully this will come to Visual Studio. You can also open an existing folder.
- Clone (NodejsTestingSimple)[https://github.com/wbsimms/NodejsTestingSimple] from GitHub.
- Open the project
Run the project
- Right click on app.js and select Run.
You can also choose to debug the project.
Open the npm tool window
- View -> Tool Windows -> npm
You should see your run, test, watch tasks. They all work just as in Visual Studio.
However, WebStorm has much stronger support for testing than Visual Studio (right now!). But first, you need to add a configuration for testing.
Add a testing profile
- Select Run -> Edit Configurations
- Press the green plus (+) to add a new configuration
- Choose Mocha
- Name it “Testing”
- Set the testing directory to < Your File Path >\NodejsTestingSimple\TestConsole
- Select Include subdirectories
Run your tests
- Select the Testing configuration
- Press the green run arrow
All the test run but with a much nicer visualization. WebStorm has another nice feature. It will auto-test your code when a change is detected. It doesn’t use the watch flag from Mocha.
- WebStorm has a bit tighter support for Node.js
- Code completion is much tighter
- Testing results interface is nicer
- WebStorm is very snappy
- Code formatting is better
Visual Studio is the only IDE you’ll ever need for just about any language. However, if I was ONLY writing Node.js, I would definitely choose WebStorm.
Unit testing Node.js in Visual Studio
The source for this project is located in my GitHub repository.
Unit testing is a critical skill in software development. Visual Studio has great support for unit testing in the Node.js plugin. We will be using Mocha for running tests.
You need to create a default console Node.js application as described in my previous post Setup Visual Studio for Node.js.
- Open the pacakage.config file
- Add Mocha as developer dependency by updating file seen below
- Notice that you get auto-completion for the packages
- Save the file
- Compile the project (CTRL+SHIFT+B)
Add a task to run tests
- Open the package.config file
- Add two script tasks as shown below
- Running the test task will fail because there are no test.
Add a class to test
For this example, we’ll do something very common. Create a class and test it.
- Create a folder named scripts containing a file named Simple.js
- Add the following code to the file. The code should be easy to understand.
Add your first test class
- Create a folder named test containing a file named simple.test.js
- Add the following code to the file
The documentation for Mocha explains all this. The key points are the “assert” statements. These should be easy to understand. Everything should look this this:
Run the tests
- Open the Task Runner Explorer
- Right click on test and select run
You can see that all the test ran and passed. You should change the Simple.js to intentionally break the test. For example change:
I make this mistake all the time… I think it’s to font I use… or my aging eye-sight. If you do this you’ll get the following output:
Watching your code and running tests.
Mocha has a great function to automatically run your tests when any of your files change. In C# there’s a tool named NCrunch which does the same thing.
- Run the “watch” task from the Task Explorer
- If all your tests pass, you’ll see the following
This gives everything you need to do unit testing Node.js in Visual Studio. I hope you’ve found it useful. Please contact me if you have any questions. I love to talk software.
Setup Visual Studio for Node.js
If you have known me for any length of time, you know that I’ve been in the .NET community for over 15 years. I have been in software development even longer, over 2 decades. I’ve seen many technologies and paradigms reborn (think of task runners… Make => Shell Scripts => Ant => NAnt => MSBuild => npm => Grunt => Gulp). Rarely, do us long-toothed developers see anything truly new.
Node.JS is different. It brings everything we’ve learned in the past 15 years of software development into one cohesive language:
- Unit testing
- Package management
- Module support
- Excellent IDE support
- Great on-line documentation
- Excellent community support
Visual Studio has always been the most mature IDE in the Windows ecosystem. Let’s be honest… I love Visual Studio. Parts of Microsoft development stack have their legacy warts. Even so, when people always ask me why I develop using C# and Microsoft technologies even though I have a long Unix background. I answer, because “Visual Studio is amazing”. It truly is the standard by which all other IDEs are measured. Over the last year, Microsoft has put serious effort in to Visual Studio support Node. Let’s take a look at the Node support.
Please remember, this post targets C# developers who are learning Node.js who are comfortable in Visual Studio. Alternatively, you could be a Node developer who is struggling with existing IDEs. In either case. I hope you find the following tutorials helpful.
Install Visual Studio 2015
You have several choices of Visual Studio to choose from:
For this example, we’ll be using Visual Studio 2015 Community Edition. For the uninitiated, installing Visual Studio is a big process. Prepare at least an hour of time! It’s a huge tool but very worth it.
- Download and install Node.js
Install Node Tools for Visual Studio
- Download Node Tools for Visual Studio
Install NPM support for Visual Studio
- Start Visual Studio
- Tools -> Extensions and Updates
- Search for npm
- Select and install the NPM Scripts Task Runner
- Restart Visual Studio
Create a simple Node.js project
This is the boilerplate project we’ll be using in my next post covering modern software development with Node and VS.
- Open the Node templates
- Select Blank Node.js Console Application
- You should see a basic project
- Press F5 to run
- A console should appear with the famous “Hello world” statement
- Close the window to continue
Already, you have a lot of features. Set a breakpoint (F9) in app.js at line one and press F5 again to run. The debugger will stop at line one. Can see all the variables in the Locals window. The Interactive Window also works.
Setting up the task runner
Now we’ll setup the package.json and the task runner to show Visual Studio’s support for npm scripts.
- Open package.json
- Modify the file by adding the script section as shown below and save
- Show the Task Runner Explorer
- You may need to press the reload button
- You’ll see a task named “run”
- Right click on run and select start
A new task window opens and your program runs.
You now have a full Node.js environment. My next post in this series will show how to use the package management features and write unit tests. Stay tuned!