Skip to main content

4 posts tagged with "nodejs"

View All Tags

· One min read
Hreniuc Cristian-Alexandru

When you want to test a function that uses a method from an object, you can mock the method and check if it was called with the right parameters. For example, this is how to mock for sendNotification method from firebaseService object:

import { firebaseService } from '../../../firebase/firebase';

let mockSendNotification: jest.Mock;

describe('Tests suit', () => {

test('Test1', async () => {
mockSendNotification = jest.fn();
firebaseService.sendNotification = mockSendNotification;

// call the function that uses firebaseService.sendNotification

expect(mockSendNotification).toHaveBeenCalledWith(
C1.notifications.N1U4S1.contents,
C1.notifications.N1U4S1.subscriptions,
);
});
});

And if you want to check if the method was called multiple times with different parameters, you can use toHaveBeenNthCalledWith:

expect(mockSendNotification).toHaveBeenNthCalledWith(
1,
C1.notifications.N4U1S2E1.contents,
C1.notifications.N4U1S2E1.subscriptions,
);
expect(mockSendNotification).toHaveBeenNthCalledWith(
2,
C1.notifications.N5U1S2E2.contents,
C1.notifications.N5U1S2E2.subscriptions,
);

· One min read
Hreniuc Cristian-Alexandru

When you have two tables and you want to create an inner join without having a relation in the entities, you can use the innerJoin method from the createQueryBuilder method like this:

.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)

This works even if you don't have a relation between the two entities.

const notifForCompanyManagers = await dbConnection.manager
.getRepository(NotificationSubscription)
.createQueryBuilder('notification_subscription')
.select('notification_subscription.token')
.addSelect('notification_subscription.userId')
.innerJoin(
UserCompany,
'user_company',
'user_company.userId = notification_subscription.userId',
)
.where(
'(user_company.manager = true or user_company.owner = true) and user_company.companyId = :companyId',
{ companyId: companyId },
)
.getMany();

· One min read
Hreniuc Cristian-Alexandru

Installl nvm

# Check latest release: https://github.com/nvm-sh/nvm/releases/latest
# This commands also adds the paths to your ~/.bashrc
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc

Install latested node version and use it

nvm install --lts
nvm use --lts
# Or
nvm use 16.15.0

Install yarn and upgrade npm:

npm install --global yarn

npm install -g npm@8.10.0

Install RESTer extension.

· One min read
Hreniuc Cristian-Alexandru

Install nodejs in a specific path:

echo 'export PATH=$HOME/programs/bin:$PATH' >> ~/.bashrc
# the `programs` dir should exist, the `bin` will be
# created if it doesn't exist, when you will be running `./configure`
source ~/.bashrc
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=$HOME/programs # your prefix
make install# --j8
# The `make install` step takes a lot, you should do it on multiple threads using --j option

Install npm on specific path:

wget https://registry.npmjs.org/npm/-/npm-5.8.0.tgz # specify what version you want 
tar xzf npm-5.8.0.tgz
cd package/
node bin/npm-cli.js install -gf --prefix=~/programs/ ../npm-5.8.0.tgz
# use the prefix where node was installed

Test them both:

node -v
#
npm -v