Categories
JavaScript Answers

How to fix the For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves error with Jest and JavaScript?

To fix the For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves error with Jest and JavaScript, we should make sure we use await with promises.

For instance, we write

it("assertion success", async () => {
  const result = await resolvingPromise;
  expect(result).to.equal("promise resolved");
});

to use await to get the resolved value of the resolvingPromise promise.

Then we check the resolve value of the promise with expect and to.equal.

Categories
JavaScript Answers

How to cache the RUN npm install instruction when docker build a Dockerfile with JavaScript?

To cache the RUN npm install instruction when docker build a Dockerfile with JavaScript, we copy the package.json file to the Docker volume.

For instance, in our Dockerfile, we write

# install node modules
WORKDIR  /usr/app
COPY     package.json /usr/app/package.json
RUN      npm install

# install application
COPY     . /usr/app

to add

COPY     package.json /usr/app/package.json

to copy package.json from the project to /usr/app/package.json in the Docker volumne.

Then npm install the packages that isn’t already installed in the volumne.

Categories
JavaScript Answers

How to fix ‘Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.’ with JavaScript?

To fix ‘Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.’ with JavaScript, we should make sure the background worker is ready to receive messages.

For instance, we write

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  chrome.tabs.sendMessage(tabs[0].id, { greeting: "hello" }, (response) => {
    console.log(response);
  });
});

in background.js to receive message with query.

Then we add message listener in content.js with

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log(request, sender, sendResponse);
  sendResponse("received:", JSON.stringify(request));
});

We call addListener to listen for messages and get the received message from request.

Categories
JavaScript Answers

How to use module.exports as a constructor with Node.js?

To use module.exports as a constructor with Node.js, we export it as a default export.

For instance, we write

class Square {
  constructor(width) {
    this.width = width;
  }
  area() {
    return Math.pow(this.width, 2);
  }
}

module.exports = Square;

to export the Square class in square.js.

Then we write

const Square = require("./square");

const s = new Square(5);
const area = s.area();

to import the Square constructor from square.js with require.

And then we call Square and use its methods.

Categories
JavaScript Answers

How to install a Node.js module without using npm?

To install a Node.js module without using npm, we download the module into the node_modules directory.

After we downloaded the module into the folder, we write

const moduleName = require("foo");

in our code to import the foo module assuming that’s what we downloaded.