Categories
JavaScript Answers

How to make multiple projects share node_modules directory with JavaScript?

To make multiple projects share node_modules directory with JavaScript, we use pnpm.

To install it globally, we run

npm install -g pnpm

Then we install all the packages for all the projects in the folder in one node_modules directory with

pnpm recursive install
Categories
JavaScript Answers

How to fix Sinon error Attempted to wrap function which is already wrapped with JavaScript?

To fix Sinon error Attempted to wrap function which is already wrapped with JavaScript, we should restore our stubs after the test is done.

For instance, we write

beforeEach(() => {
  sandbox = sinon.createSandbox();
  mockObj = sandbox.stub(testApp, "getObj", fakeFunction);

});

afterEach(() => {
  sandbox.restore();
});

to call createSandvox to create the sandbox.

Then we call sandvbox.stub to create a stub in the beforeEach callback, which runs before each test.

Then in the afterEach callback, we call sandbox.restore to restore the stubs after each test.

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.