fbpx

Testing exceptions with Async Await

Normally when testing your code throwing exceptions is goes something like this: (examples in typescript with Jasmine)


function isTwo(num: number): number {
  if(num != 2) throw new Error("Yo! It's not number 2!!");
  return num;
}

// ... inside describe block
it("should throw an error", () => {
  const throwError = function() { isTwo(3) };

  expect(throwError).toThrowError("Yo! It's not number2!!")
}

We wrap our function in an anonymous function and pass it to the expectation.  All is good and we move on with our day!

But what if our function is returning a promise! Aha, things can’t work the same.

Let’s use async / await to help us.


function isTwo(num: number): Promise<number> {
  new Promise(resolve, reject) {
    if(num != 2) reject("Yo! It's not number 2!!");
    resolve(num);
  }
}

// ... inside describe block
it("should throw an error", async () => {
  try {
    const throwError = isTwo(3); 
  } catch(error) {
    expect(error.message).toBe(""Yo! It's not number 2!!");
  }
}

Environment Variables in Webpack

Diving into webpack module loader can often seem like a confusing and daunting task. Luckily, setting environment variables is rather straight forward.

At the top of your webpack.config.js file define your environment variables like so:

/**
 * Example Constants
 */
const ENV = process.env.ENV = process.env.NODE_ENV = 'development';
const API_URL = process.env.API_URL = 'http://localhost:3000';
const METADATA = {
  API_URL: API_URL,
  ENV: ENV
};

Then in the plugins array use DefinePlugin method to define global variables configured at compile time:

/**
 * Make Webpack Constants Available Globally
 */
   new webpack.DefinePlugin({
     'ENV': JSON.stringify(METADATA.ENV),
     'API_URL': JSON.stringify(METADATA.API_URL),
     'process.env': {
       'ENV': JSON.stringify(METADATA.ENV),
       'NODE_ENV': JSON.stringify(METADATA.ENV),
       'API_URL' : JSON.stringify(METADATA.API_URL),
     }
   }),

Re-compile and now your variables will be globally available.

Immutable Objects using Object.assign

Is immutability important? Mutating data can produce code that’s hard to read and error prone. But we can avoid this mess using vanilla javascript’s ES6 feature Object.assign!

Let’s look at the problem….

var obj1 = { key: 'some value' };

var obj2 = obj1;
obj2.key = 'another value';

console.log( obj1 === obj2) // true
console.log( obj1.key ) // 'another value'

When we changed obj2’s key property, it also changed obj1’s key property. No bueno! The solution:

var obj1 = { key: 'some value' };

var obj2 = Object.assign( {}, obj1, { key: 'another value' });

console.log( obj1 === obj2) // false
console.log( obj1.key ) // 'some value'
console.log( obj2.key ) // 'another value'

As you can see we changed obj2’s key property and it left obj1’s state intact! Now that’s immutable! Object.assign takes objects as its parameters and passing in an empty object as the ‘target’ keeps our ‘source’ objects intact.

Object.assign is widely supported by desktop and mobile browsers, please check Mozilla for more info.