Categories
JavaScript Answers

How to use async/await with a forEach loop with Node.js?

Sometimes, we want to use async/await with a forEach loop with Node.js.

In this article, we’ll look at how to use async/await with a forEach loop with Node.js.

How to use async/await with a forEach loop with Node.js?

To use async/await with a forEach loop with Node.js, we can use the for-of loop.

For instance, we write

async function printFiles() {
  const files = await getFilePaths();

  for (const file of files) {
    const contents = await fs.readFile(file, 'utf8');
    console.log(contents);
  }
}

to loop through the files array and call fs.readFile on the file.

The await keyword will wait for the promise returned by readFile to resolve before moving to the next iteration.

Conclusion

To use async/await with a forEach loop with Node.js, we can use the for-of loop.

Categories
JavaScript Answers

How to find the version of an installed NPM package?

Sometimes, we want to find the version of an installed NPM package.

In this article, we’ll look at how to find the version of an installed NPM package.

How to find the version of an installed NPM package?

To find the version of an installed NPM package, we can use the npm list command.

We run

npm list

to find the version numbers for local packages.

And we run

npm list -g

to find the version numbers for global packages.

We can also run npm list with the package name to find the version of the package installed.

For instance, we run

npm list left-pad

to find the version of the left-pad package installed.

Conclusion

To find the version of an installed NPM package, we can use the npm list command.

Categories
JavaScript Answers

How to pass command line arguments to a Node.js program?

Sometimes, we want to pass command line arguments to a Node.js program.

In this article, we’ll look at how to pass command line arguments to a Node.js program.

How to pass command line arguments to a Node.js program?

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.

For instance, we write

process.argv.forEach((val, index, array) => {
  console.log(index, val);
});

to call process.argv.forEach with a callback to get the value for each command line argument in our Node app.

val has the command line argument value at the given array index.

Conclusion

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.

Categories
JavaScript Answers

How to check if a string contains ‘abc’ or ‘cde’ with Jest?

Sometimes, we want to check if a string contains ‘abc’ or ‘cde’ with Jest.

In this article, we’ll look at how to check if a string contains ‘abc’ or ‘cde’ with Jest.

How to check if a string contains ‘abc’ or ‘cde’ with Jest?

To check if a string contains ‘abc’ or ‘cde’ with Jest, we can call toMatch with a regex that matches both strings.

For instance, we write

expect(str).toMatch(/(abc|cde)/i)

to check if str is 'abc' or 'cde' in a case-insensitive manner.

The i flag lets us check for str in a case-insensitive manner.

Conclusion

To check if a string contains ‘abc’ or ‘cde’ with Jest, we can call toMatch with a regex that matches both strings.

Categories
JavaScript Answers

How to mock a Node module with Jest?

Sometimes, we want to mock a Node module with Jest.

In this article, we’ll look at how to mock a Node module with Jest.

How to mock a Node module with Jest?

To mock a Node module with Jest, we can use the jest.createMockFromModule method.

For instance, we write

const utils = jest.createMockFromModule('../utils').default;
utils.isAuthorized = jest.fn(secret => secret === 'abc');

test('jest.createMockFromModule mocks utils module', () => {
  expect(utils.authorize.mock).toBeTruthy();
  expect(utils.isAuthorized('abc')).toEqual(true);
});

to call jest.createMockFromModule to mock the ./utils module.

Then we set utils.isAuthorized to a mocked function created by jest.fn with a callback to mock its implementation.

Next, we check if utils.authorize.mock is present with expect.

And we call utils.isAuthorized('abc') to see if it returns true as expected.

Conclusion

To mock a Node module with Jest, we can use the jest.createMockFromModule method.