5 JavaScript Technical Interview Questions That You Should Be Prepared For
By: Gp Lee
Tags: Coding, JavaScript, Software Development, Programming, Web Development

3. How do you test code?

There are many ways you can ensure that you write quality codes. First, let’s compare unit test and end-to-end (E2E) test.

3.1) JavaScript Unit Test

Unit test tests a block of code (i.e., function) “unit” to ensure that they all run as expected. This tests allow developers to check individual areas of a program to see where(and why) errors occur.

Benefit of Unit Testing

1. It makes your project more agile. Whenever you add a new feature, you many have to change the old code. Changing a code that’s been tested already is costly and risky. However, if you run unit tests on the new code, you will be confident that the new code won’t break any old feature.

2. Helps finding bugs early in the development cycle.

Popular Unit Testing Tool

Jest

Jest is a JavaScript testing framework maintained by Facebook.

3.2) JavaScript E2E Test

End-to-End (E2E) testing is a technique used to test if the application behaves as expected. In simple terms, a software testing methodology to test an application flow from start to end. The main purpose of E2E is to simulate the real user scenario.

Popular E2E Testing Tool

Cypress.io

Fast, easy and reliable testing for anything that runs in a browser.

TDD vs. BDD

“What are you testing for?” is a great question to ask when comparing The Unit test and E2E test. In behavior driven development (BDD), you are looking for the behavior, for example, what will happen to this system under a certain condition. But in test driven development (TDD), you have a test for a method which will assert some conditions, but as the system evolves these tests may give you false results.

To simply this further, it can be written

In TDD, I don’t care much about the output. The only thing needed is to carry out the test in a particular way.

In BDD, I don’t mind how you come up with the output, only that the output has to be correct under the given condition.

Summary

BDD has evolved from TDD, as a way to eliminate the shortfalls of TDD.

Best to implement both approaches — one to support the quality of the code the developer writes, and the other to support the behavior of the system defined by the product owner.

3.3. Linters

lint, or a linter is a static code analysis tool used to flag programming errors, bugs, stylistic errors, and suspicious constructs. They scan your scripts for common issues and errors, and give you back a report with line numbers that you can use to fix things. In addition to actual bugs and errors, they also check for subjective, stylistic preferences as well.

Benefits of linters

1. Linter helps catching common mistakes and bugs easily.

2. Linter helps increase productivity by allowing colleagues to use same code styles.

Popular linters

ESLint

The custom configurations is the one of the best features of ESLint. While JSLint does not allow for configuration, which means it enforces a single style guide, ESLint is little bit more flexible.

You could adopt big IT companies’ coding style easily with using ESLint.

Google

“extends”: [“eslint:recommended”, “google”]

AirBnb

"extends": ["eslint:recommended", "airbnb"]

Standard

"extends": ["eslint:recommended", "standard"]

3.4. Online code editors

Many developers use online code editor for everyday developments & debugging and it indeed one of the best testing tools for modern developers.

jsfiddle & repl.it

Good for checking confusing JavaScript types and variable scopes.

repl.it

CodeSandbox

Good for testing SPA code (React, Vue or Angular).

Codepen

Good for testing CSS and HTML. CSS is hard to debug and find the errors because CSS often does not throw any errors.