Categories
JavaScript Answers

How to verify that an exception is thrown using Mocha and Chai and async and await?

Sometimes, we want to verify that an exception is thrown using Mocha and Chai and async and await.

In this article, we’ll look at how to verify that an exception is thrown using Mocha and Chai and async and await.

How to verify that an exception is thrown using Mocha and Chai and async and await?

To verify that an exception is thrown using Mocha and Chai and async and await, we can use the chai-as-promised package.

We install it by running

npm i chai-as-promised

Then we can use it by writing

const chai = require('chai')
const {
  expect
} = chai
chai.use(require('chai-as-promised'))

const wins = async () => {
  return 'Winner'
}

const fails = async () => {
  throw new Error('Contrived Error')
}

it('wins() returns Winner', async () => {
  expect(await wins()).to.equal('Winner')
})

it('fails() throws Error', async () => {
  await expect(fails()).to.be.rejectedWith(Error)
})

We add the chai-as-promised plugin with

chai.use(require('chai-as-promised'))

Then we test the wins and fails async functions by calling it with an async function.

And we get the resolved value of wins with await and use equal to check it resolve value.

And we test for rejected promise from the fails function by call fails and then use rejectedWith with awit to check the error thrown.

Conclusion

To verify that an exception is thrown using Mocha and Chai and async and await, we can use the chai-as-promised package.

Categories
JavaScript Answers

How to time out a promise if failed to complete in time with Node.js?

Sometimes, we want to time out a promise if failed to complete in time with Node.js.

In this article, we’ll look at how to time out a promise if failed to complete in time with Node.js.

How to time out a promise if failed to complete in time with Node.js?

To time out a promise if failed to complete in time with Node.js, we can use the Promise.race method.

For instance, we write

const withTimeout = (millis, promise) => {
  const timeout = new Promise((resolve, reject) =>
    setTimeout(
      () => reject(`Timed out after ${millis} ms.`),
      millis));
  return Promise.race([
    promise,
    timeout
  ]);
}

to define the withTimeout function that calls Promise.race with the promise and the timeout promise.

Promise.race returns the promise that’s settled the earliest so if timeout is settled before promise, it’ll be returned.

Then we can use withTimeout in an async function by writing

await withTimeout(5000, doSomethingAsync());

where doSomethingAsync is an async function.

Conclusion

To time out a promise if failed to complete in time with Node.js, we can use the Promise.race method.

Categories
JavaScript Answers

How to read all text from stdin to a string with Node.js?

Sometimes, we want to read all text from stdin to a string with Node.js.

In this article, we’ll look at how to read all text from stdin to a string with Node.js.

How to read all text from stdin to a string with Node.js?

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.

For instance, we write

const fs = require('fs');
const data = fs.readFileSync(0, 'utf-8');

to call readFileSync with 0 to return the text from stdin as a 'utf-8' string.

Conclusion

To read all text from stdin to a string with Node.js, we can call readFileSync with 0.

Categories
JavaScript Answers

How to create or update with Node.js Sequelize?

Sometimes, we want to create or update with Node.js Sequelize.

In this article, we’ll look at how to create or update with Node.js Sequelize.

How to create or update with Node.js Sequelize?

To create or update with Node.js Sequelize, we can use the findOne method to check if the object exists before we update it.

For instance, we write

const upsert = async (values, condition) => {
  const obj = await Model
    .findOne({
      where: condition
    })
  if (obj) {
    return obj.update(values);
  }
  return Model.create(values);
}

to call findOne with an object with the condition we’re looking for.

Then if obj exists, we call obj.update to update the object with the values.

Otherwise, we call create with values to create the object with the values.

Then we call upsert in an async function like

await upsert({
  first_name: 'jane'
}, {
  id: 1234
})

Conclusion

To create or update with Node.js Sequelize, we can use the findOne method to check if the object exists before we update it.

Categories
JavaScript Answers

How to get URL parameters with Node.js and Express?

Sometimes, we want to get URL parameters with Node.js and Express.

In this article, we’ll look at how to get URL parameters with Node.js and Express.

How to get URL parameters with Node.js and Express?

To get URL parameters with Node.js and Express, we can use the req.params property.

For instance, we write

app.get('/documents/:format/:type', (req, res) => {
  const {
    format,
    type
  } = req.params
  //...
});

to get the format and type properties from the req.params object to get the value of the format and type URL parameters.

Conclusion

To get URL parameters with Node.js and Express, we can use the req.params property.