Categories
JavaScript Answers

How to join tests from multiple files with Mocha.js?

Sometimes, we want to join tests from multiple files with Mocha.js.

In this article, we’ll look at how to join tests from multiple files with Mocha.js.

How to join tests from multiple files with Mocha.js?

To join tests from multiple files with Mocha.js, we can require the test file in another test.

For instance, we write

const importTest = (name, path) => {
  describe(name, () => {
    require(path);
  });
}

const common = require("./common");

describe("top", () => {
  beforeEach(() => {
    console.log("running something before each test");
  });
  importTest("a", './a/a');
  importTest("b", './b/b');
  after(() => {
    console.log("after all tests");
  });
});

to define the importTest which creates a test with describe to create a new test suite.

And we call require with path to run the tests at the given path.

Next,. we call importTest with the name of the test and path to the tests in the 2nd describe to run the tests in those files.

Conclusion

To join tests from multiple files with Mocha.js, we can require the test file in another test.

Categories
JavaScript Answers

How to run Mocha tests with extra options or parameters?

Sometimes, we want to run Mocha tests with extra options or parameters.

In this article, we’ll look at how to run Mocha tests with extra options or parameters.

How to run Mocha tests with extra options or parameters?

To run Mocha tests with extra options or parameters, we can set some environment variables before running the tests and then access them from process.env.

For instance, we run the tests with

env KEY=YOUR_KEY mocha test/*.js 

Then in the tests, we get the KEY environment variable with

const key = process.env.KEY;

Conclusion

To run Mocha tests with extra options or parameters, we can set some environment variables before running the tests and then access them from process.env.

Categories
JavaScript Answers

How to create an Excel File with Node.js?

Sometimes, we want to create an Excel File with Node.js.

In this article, we’ll look at how to create an Excel File with Node.js.

How to create an Excel File with Node.js?

To create an Excel File with Node.js, we can use the excel4node package.

To install it, we run

npm i excel4node

Then we use it by writing

const excel = require('excel4node');

const workbook = new excel.Workbook();
const worksheet = workbook.addWorksheet('Sheet 1');
const worksheet2 = workbook.addWorksheet('Sheet 2');

const style = workbook.createStyle({
  font: {
    color: '#FF0800',
    size: 12
  },
  numberFormat: '$#,##0.00; ($#,##0.00); -'
});

worksheet.cell(1, 1).number(100).style(style);
worksheet.cell(1, 2).number(200).style(style);
worksheet.cell(1, 3).formula('A1 + B1').style(style);
worksheet.cell(2, 1).string('string').style(style);
worksheet.cell(3, 1).bool(true).style(style).style({
  font: {
    size: 14
  }
});
workbook.write('Excel.xlsx');

to create a new workbook with

const workbook = new excel.Workbook();

Then we create 2 worksheets with

const worksheet = workbook.addWorksheet('Sheet 1');
const worksheet2 = workbook.addWorksheet('Sheet 2');

Then we set the font styles of the cells with

const style = workbook.createStyle({
  font: {
    color: '#FF0800',
    size: 12
  },
  numberFormat: '$#,##0.00; ($#,##0.00); -'
});

Next, we add some content to the cells with

worksheet.cell(1, 1).number(100).style(style);
worksheet.cell(1, 2).number(200).style(style);
worksheet.cell(1, 3).formula('A1 + B1').style(style);
worksheet.cell(2, 1).string('string').style(style);
worksheet.cell(3, 1).bool(true).style(style).style({
  font: {
    size: 14
  }
});

Cell A1 is 1, 1.

And then we save the spreadsheet with

workbook.write('Excel.xlsx');

Conclusion

To create an Excel File with Node.js, we can use the excel4node package.

Categories
JavaScript Answers

How to select where in array of _id with Node.js MongoDB?

Sometimes, we want to select where in array of _id with Node.js MongoDB.

In this article, we’ll look at how to select where in array of _id with Node.js MongoDB.

How to select where in array of _id with Node.js MongoDB?

To select where in array of _id with Node.js MongoDB, we can use the collection find method with the $in operator.

For instance, we write

db.collection.find({
  _id: {
    $in: [1, 2, 3, 4]
  }
});

to call find with the _id property set to an object with $in set to [1, 2, 3, 4] to query for entries with _id 1, 2, 3, or 4.

Conclusion

To select where in array of _id with Node.js MongoDB, we can use the collection find method with the $in operator.

Categories
JavaScript Answers

How to make a HTTPS POST in Node.js without any third party module?

Sometimes, we want to make a HTTPS POST in Node.js without any third party module.

In this article, we’ll look at how to make a HTTPS POST in Node.js without any third party module.

How to make a HTTPS POST in Node.js without any third party module?

To make a HTTPS POST in Node.js without any third party module, we can use the https.request method.

For instance, we write

const https = require('https');

const postData = JSON.stringify({
  'msg': 'Hello World!'
});

const options = {
  hostname: 'jsonplaceholder.com',
  port: 443,
  path: '/post',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

const req = https.request(options, (res) => {
  console.log('statusCode:', res.statusCode);
  console.log('headers:', res.headers);

  res.on('data', (d) => {
    process.stdout.write(d);
  });
});

req.on('error', (e) => {
  console.error(e);
});

req.write(postData);
req.end();

to call https.request with the options for the request, which includes the request URL, method, and headers.

We get the response from the callback we pass into https.request as the 2nd argument.

And we pass in the request body by calling the req.write.

res.on lets us listen to the response by listening to the 'data' event.

And the 'error' event is emitted when there’s an error.

Conclusion

To make a HTTPS POST in Node.js without any third party module, we can use the https.request method.