Testing, testing, 1 2 3~~
This week, I had the chance to explore ECMAScript and fiddle around with test cases. In this blog post, I present to you my experience and what I learned along the way.
To start…
What is ECMAScript anyways?
ECMAScript is a scripting language and a subset of JavaScript. Think of it as the engine which powers the the car(JavaScript and other scripting languages). Analogy found on StackOverflow. There are standards(Car manufacturers) on the web that specifies how this engine should perform under the hood. If you’re interested, please have a look at the current development of ECMAScript 2019.
In software development, a piece of code should be accompanied by one or more test cases. This goes to ensure your program is doing what it is expected, and you, as the programmer will be notified when it misbehaves.
There are 3 ways of testing I’m currently aware of:
- Manual testing — Open your program, and click the button, then another, then another. This may be fine if your software is tiny(simple script), but it will become the definition of insanity as the size of your code base increases.
- Writing tests after writing code — Create a few test cases to ensure your code is working as intended.
- TDD(Test Driven Development) — Churning out test cases prior to writing your class/function. This style of testing forces the developer to really think how the code should behave before it is created.
For ECMAScript, its tests resides within this repository on GitHub. This is how to setup the project:
I’m using a Hackintosh, mainly Mac OS for development work. On my Z-shell, the third command gives me this error:
I’ve googled but resulted with no clear solution, so I sought help from my professor David Humphrey. This was his comment:
So I switched to bash, and it ran without a hitch. Looking at this issue, I wanted to see if I could update some of the older test cases from 2009 with the latest helper functions.
This operator was new to me:
!== and ===
Upon further research, I discovered that those two compare both the value and type of the operand.
So why is this necessary?
In JavaScript, there is type coercion.
What does that mean?
!== and === should almost always be used in JavaScript to prevent possible logic flaws caused by coercion of data types.
Back to the test case. This is the newly updated function:
Couple of things to note:
- Assertions, are used to assert a TRUTH. This call is commonly used among testing frameworks.
- typeof is used to return an object’s type.
- Infinity is a value! It “Display a number that exceeds the limit of a floating point number”.
- Typed Arrays in JavaScript provide a way to access raw binary data. This is useful for storing and manipulating raw data for Audio and Video files. Learn more here.
I updated 2 test cases by the end of my journey. Check it out on Gist!
Haout.