TGIMBA .NET Core Upgrade – JavaScript Unit Tests with JSTest.Net – Truly Awesome!!

Git  Code Commit

Today, I am focusing on adding unit tests for the JavaScript I added in the last post. I looked at a number of contenders, but all of them seemed to tied to a browser or additional server or something ‘extra’. I wanted to run them just like I ran the C# server side unit tests.

I will do integration tests (some on the server as I did with the existing .NET 4.6.x code I ported over (to be replaced) and some via Selenium (also like I did in the .NET 4.6.x website)), but I wanted to focus on that a little further down the road. Ideally, that would be written along with the unit tests following a Test Driven Design (TDD) methodology, but I am anxious to explore the Graphic User Interface (GUI) perspective 🙂

I did find one contender that (so far) has met my requirements – JSTest.Net created by Chris Baxter. Locally, it was as easy to install via Nuget and my simple test worked.

  • Sample Test

1

  • File/Function under test

2

  • Running test

3

So, I added my tests and initially, everything worked as expected. I then tried a pipeline build and was surprised when I got an error. Upon more digging, the nuget restore of this project was not found.

Error1

This predictably caused the build to fail.

Error2

Some initial research did not reveal an easy solution, so I disabled the check in build integration and checked in and deployed the site manually. I did not want to spend a lot of time (again) dealing with the pipeline.

Then, I hit another hurdle. Injecting what I thought would have worked failed. I wanted to inject a Hyper Text Markup Language (HTML) document so my Load method could be tested whether it found my target div or not. I kepting getting errors about illegal characters. So, I actually sat down and read the documentation (only when you absolutely have to 🙂 ) and determined that the append file was only for JavaScript files. So, I decided to put that off and concentrate on true disconnected unit tests without injection.

Guided by the documentation, I was able to create unit tests for:

  • ErrorHandler.js

4

  • Utility.js

5

  • Run together

6

With this working, I contemplated how I could get my pipeline to work with this apparently rogue nuget package. So, I forked JSTest.Net, downloaded it and tried building it…lots of errors. I also noticed that it was no longer maintained. Given how useful this package is to me and my needs, I may attempt to get my fork compiling and working, but for now, I am going to reference the nuget .dll file (old school, I know) and store with my code. Not ideal, but it works 🙂

Adding the JSTest.dll file as a local reference and running all tests throw VS 2017 works.
7

Initially, the pipeline broke because of my references. I removed that test project, added a .Net Core test project (duh Eric 🙂 ), ported over my test code and voila it works 🙂
8

Website after build.
9

I would still like to figure out the injection thing so I can test my Global.js Load() method, so I will either do that or move on to the Login page.

Stay tuned!

References

  1. https://archive.codeplex.com/?p=jstest
  2. https://shawngsweeney.wordpress.com/2012/03/29/unit-testing-with-mstest-and-jstest-net/
  3. https://stackoverflow.com/questions/36320625/visual-studio-team-services-build-fails-due-to-missing-nuget-packages
    -https://shawngsweeney.wordpress.com/2012/03/29/unit-testing-javascript-with-mstest-and-jstest-net/
  4. https://github.com/cbaxter/JSTest.NET/wiki
  5. https://www.nuget.org/packages/JSTest.NET/
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s