Promise chains aren't being executed all the way through - node.js

I'm trying to do an integration test on a function that manages users. The calls it makes are asynchronous but Jest seems to be cutting out after I add the test user and check the details.
it("runs lifecycle", async done => {
// expect.assertions(1);
function check(a, b, c) {
return new Promise((resolve, reject) => {
console.log(a, b, c);
if (a != b) reject(c);
console.error(a, b, c);
resolve();
});
}
await new Promise((resolve, reject) => {
user.add(testUser).then(() => {
user.get(testUser.id).then(out => {
check(out, testUser, "adding").then(() => {
user.update(testUser1).then(() => {
user
.get(testUser1.id)
.then(out => check(out, testUser1, "update"))
.then(() => {
user.delete(testUser.id).then(() => {
user
.get(testUser1.id)
.then(out => check(out, null, "delete"))
.then(() => {
resolve();
done();
});
});
});
});
});
});
});
}).then(() => {
expect(true).toBeTruthy;
done();
});
done();
testUser and testUser1 are objects with some of their properties changed. I have tested this outside of Jest and the code runs fine.
console.log user.js:68
Successfully created new user: test
console.log user.test.js:26
{ id: 'test',
email: 'you#me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo#me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
console.error user.test.js:28
{ id: 'test',
email: 'you#me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+271234567' } { id: 'test',
email: 'yo#me.com',
first: 'Wilhelm',
last: 'Erasmus',
phone: '+277654321' } 'adding'
FAIL ./user.test.js (7.269s)
User APIs
✕ runs lifecycle (2779ms)
● User APIs › runs lifecycle
Error
adding
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | Unknown | Unknown | Unknown | Unknown | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 9.916s
Ran all test suites.
console.log user.js:114
Successfully deleted test
Jest did not exit one second after the test run has completed.
This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

I fixed the problem. I'm not sure why this solution works and I am happy to accept an answer that explains it better.
var addUser = user.add(testUser);
var checkAdd = addUser
.then(() => user.get(testUser.id))
.then(check1 => check(check1, testUser, "Add"));
var updateUser = checkAdd.then(() => user.update(testUser1));
var checkUpdate = updateUser
.then(() => user.get(testUser1.id))
.then(check2 => check(check2, testUser1, "Add"));
var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
var checkDelete = deleteUser.then(() => {
expect(user.exists).resolves.toBeFalsy;
});
return Promise.all([
addUser,
checkAdd,
updateUser,
checkUpdate,
deleteUser,
checkDelete
]);

Related

mock-apollo-client returnig "Response not successful: Received status code 401"

I'm writing a toy app to learn more about Serverless Framework and AWS AppSync etc.
I'm trying to do TDD as much as possible. I'm using mock-apollo-client to mock the ApolloClient, and I've run into a problem. When trying to write a test to make sure the arguments to the query are passed, the test always returns a 401 Unauthorized error. It seems as though the real end point is still being called, because when a valid x-api-key is added to the instantiation of the ApolloClient, the test returns the real value from the AppSync server, and not the mock value I'm expecting. I'm using a mock, not spy, so I'm not expecting the real end point to actually be hit. Furthermore When I do add a valid x-api-key the test fails because the function is never called.
api › recipes › Given a valid recipe id › Should call query with the id as a param
expect(jest.fn()).toBeCalledTimes(expected)
Expected number of calls: 1
Received number of calls: 0
I'm expected the test to fail, because the query currently isn't called with any arguments, but instead it fails because the mock function is never called.
What am I doing wrong?
Code File
import { ApolloClient, gql, InMemoryCache } from '#apollo/client';
const client = new ApolloClient({
uri: 'https://redacted.appsync-api.redacted.amazonaws.com/graphql',
headers: {
'x-api-key': 'key-redacted',
},
cache: new InMemoryCache(),
});
export const GET_RECIPE_QUERY = gql`
query {
getRecipe (title:"Lemon Cheese Cake") {
title,
ingredients{
name,
amount,
unit
},
steps
}
}
`;
const gqlQuery = (title) => {
return client
.query({
query: GET_RECIPE_QUERY,
variables : { title }
});
};
export const getRecipe = async (id) => {
const result = await gqlQuery(id);
return result.data.getRecipe;
};
Test file
import { createMockClient } from 'mock-apollo-client';
import { GET_RECIPE_QUERY, getRecipe } from './recipes';
const mockRecipe = {
title: 'Luke\'s Chocolate Orange',
ingredients: [
{
name: 'Orange',
amount: 1,
},
{
name: 'Chocolate',
amount: 250,
unit: 'grams',
},
],
steps: [
'Peel orange',
'Open chocolate',
'Eat chocolate',
'Throw orange away',
],
};
const mockClient = createMockClient();
const queryHandler = jest.fn().mockResolvedValue({data: {recipe: mockRecipe}});
mockClient.setRequestHandler(GET_RECIPE_QUERY, queryHandler);
describe('api', () => {
describe('recipes', () => {
describe('Given a valid recipe id', () => {
it('Should call query with the id as a param', async () => {
const id = 'Luke\'s Chocolate Orange';
const result = await getRecipe(id);
expect(queryHandler).toBeCalledTimes(1);
expect(queryHandler).toBeCalledWith(id);
});
});
});
});
Packages
Versions
#apollo/client
3.5.10
graphql
16.3.0
#testing-library/jest-dom
5.16.2
#testing-library/react
12.1.4
#testing-library/user-event
13.5.0
jest
27.5.1
mock-apollo-client
1.2.0
mock-apollo-client always use the with ApolloProvider, so that you pass the mock apollo client via React context Provider to descendant components.
However, your code cannot pass the mock apollo client to the component in this way. Your code initiates requests directly from the Apollo Client. We need to intercept these GraphQL requests. There are several ways to do this such as msw. However, I'll continue to use the mock-apollo-client library to demonstrate.
You need to mock ApolloClient class of the #apollo/client module. We need to use Mocking Partials, we don't want to mock other things exported from #apollo/client. Since the mock-apollo-client library already provides createMockClient function to create mocked apollo client, we don't need to mock by ourself.
An working example:
recipes.ts:
import { ApolloClient, gql, InMemoryCache } from '#apollo/client';
const client = new ApolloClient({
uri: 'https://redacted.appsync-api.redacted.amazonaws.com/graphql',
headers: {
'x-api-key': 'key-redacted',
},
cache: new InMemoryCache(),
});
export const GET_RECIPE_QUERY = gql`
query {
getRecipe(title: "Lemon Cheese Cake") {
title
ingredients {
name
amount
unit
}
steps
}
}
`;
const gqlQuery = (title) => {
return client.query({
query: GET_RECIPE_QUERY,
variables: { title },
});
};
export const getRecipe = async (id) => {
const result = await gqlQuery(id);
return result.data.getRecipe;
};
recipes.test.ts:
import { createMockClient } from 'mock-apollo-client';
const mockRecipe = {
title: "Luke's Chocolate Orange",
ingredients: [
{ name: 'Orange', amount: 1, unit: 'abc' },
{ name: 'Chocolate', amount: 250, unit: 'grams' },
],
steps: ['Peel orange', 'Open chocolate', 'Eat chocolate', 'Throw orange away'],
};
const mockClient = createMockClient();
describe('api', () => {
describe('recipes', () => {
describe('Given a valid recipe id', () => {
beforeEach(() => {
jest.resetModules();
});
it('Should call query with the id as a param', async () => {
jest.doMock('#apollo/client', () => {
return {
...jest.requireActual('#apollo/client'),
ApolloClient: jest.fn(() => mockClient),
};
});
const queryHandler = jest.fn().mockResolvedValue({ data: { getRecipe: mockRecipe } });
const { GET_RECIPE_QUERY, getRecipe } = require('./recipes');
mockClient.setRequestHandler(GET_RECIPE_QUERY, queryHandler);
const title = "Luke's Chocolate Orange";
const result = await getRecipe(title);
expect(result).toEqual(mockRecipe);
expect(queryHandler).toBeCalledWith({ title });
});
});
});
});
Test result:
PASS src/stackoverflow/71612556/recipes.test.ts
api
recipes
Given a valid recipe id
✓ Should call query with the id as a param (91 ms)
------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------|---------|----------|---------|---------|-------------------
All files | 90.91 | 100 | 66.67 | 90.91 |
mocks | 75 | 100 | 0 | 75 |
handlers.js | 66.67 | 100 | 0 | 66.67 | 14
server.js | 100 | 100 | 100 | 100 |
stackoverflow/71612556 | 100 | 100 | 100 | 100 |
recipes.ts | 100 | 100 | 100 | 100 |
------------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 2.775 s
You can find the source code here

I need to run this test of my NestJs application

I'm trying to do this test, for a simple function, but I can't. I posted my test code and error.
I tried to do it in several different ways but I was not successful.
I'm using NestJS CLI and the test using jestJs
// My coding
createSession(login: string, password: string) {
const search: UserEntity = this.users.find(
(user: UserEntity) => user.cpf === login,
);
if (search) return search.senha === password;
else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
}
// My test
it('should retrieve getHello', async () => {
await expect(
service.createSession(mockLogin.login, mockLogin.password),
).rejects.toEqual(
new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED),
);
});
// The error
● LoginService › Get service › should retrieve getHello
HttpException: UNAUTHORIZED
22 | );
23 | if (search) return search.senha === password;
> 24 | else throw new HttpException('UNAUTHORIZED', HttpStatus.UNAUTHORIZED);
| ^
25 | }
26 | }
27 |
I managed to solve it as follows:
it('should retrieve', async () => {
await expect(
service.createSession(mockLogin.login, mockLogin.password),
).rejects.toThrow();
});

Jest mock nested function from another file

I am learning NodeJs and Jest. I am having trouble with unit tests. I just translated my actual code to a simple logic. I have two files as below.
// age.js
function getAge(birthYear) {
const age = 2021-birthYear;
return age
}
module.exports = { getAge }
// user.js
const { getAge } = require("./age");
async function isMinor(){
const bYear = 1991
const age = await getAge(bYear)
if( age <= 18) {
return true
}
return false
}
module.exports = { isMinor }
isMinor calls getAge from another file, I want to test isMinor without actually calling getAge. I referred to this article and wrote my test, but I still encountered some issues.
// user.test.js
const { isMinor } = require("./user")
describe("Age Test", () => {
// Question 1: how can I properly import getAge function here and mock a return value for it? I also tried mockImplementation and mockReturnedValue, but they didn't work
// I don't want to actually invoke getAge function
beforeEach(() => {
jest.mock("./age", () => ({
getAge: () => 99,
}))
})
// Question 2: How can I teardown the moch after the test
afterEach(() =>{
getAge.mockRestore()
})
test("should be an adult", async () => {
const isMinor = await isMinor();
expect(isMinor).toEqual(false);
});
});
I expect to receive 99 from getAge, but it returns null. I appreciate any helps. Thank you.
Since you're only testing isMinor with mock values you'll want to test it with multiple values to cover all of the different scenarios (branches), so you can create a mock for the ./age.js only once by simply calling:
const { getAge } = require('./age');
jest.mock('./age');
It will generate a mock function for each module function only for this test file
Modules that are mocked with jest.mock are mocked only for the file that calls jest.mock. Another file that imports the module will get the original implementation even if it runs after the test file that mocks the module.
So there will be no need for you to restore the original implementation.
The biggest advantage from using auto-mocks is when the method from the implementation (in this case getAge) is removed - the test will fail.
The only thing left to do would be to set the mock's return value that you want to test with. And since it's expected to return a promise you should use .mockResolvedValue()
user.test.js
const { isMinor } = require("./user");
const { getAge } = require('./age');
jest.mock('./age');
describe("Age Test", () => {
describe('getAge returning more than 18', () => {
beforeAll(() => {
getAge.mockResolvedValue(99)
})
test("should be an adult", async () => {
expect(await isMinor()).toEqual(false);
});
})
describe('getAge returning less than 18', () => {
beforeAll(() => {
getAge.mockResolvedValue(13)
})
test("should be a minor", async () => {
expect(await isMinor()).toEqual(true);
});
})
});
Working example
Below example use "jest": "^26.6.3".
user.js:
const { getAge } = require('./age');
async function isMinor() {
const bYear = 1991;
const age = await getAge(bYear);
console.log('age: ', age);
if (age <= 18) {
return true;
}
return false;
}
module.exports = { isMinor };
Option 1: use jest.mock() in beforeEach hook functional scope, it will NOT be hoised to the top of the code. So you need to require modules after mocking by jest.mock() method.
describe('Age Test', () => {
beforeEach(() => {
jest.mock('./age', () => ({
getAge: jest.fn(() => 99),
}));
});
test('should be an adult', async () => {
const { isMinor } = require('./user');
const { getAge } = require('./age');
const actual = await isMinor();
expect(actual).toBeFalsy();
expect(getAge).toBeCalledWith(1991);
});
});
unit test result:
PASS examples/66288290/user.test.js
Age Test
✓ should be an adult (1911 ms)
console.log
age: 99
at examples/66288290/user.js:6:11
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 87.5 | 50 | 100 | 87.5 |
user.js | 87.5 | 50 | 100 | 87.5 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.197 s
Option 2: use jest.mock() in the module scope, it will be hoisted to the top of the code. Even if you require the modules at the top of the file. The ./age module you require is already be mocked.
const { isMinor } = require('./user');
const { getAge } = require('./age');
jest.mock('./age', () => ({
getAge: jest.fn(() => 99),
}));
describe('Age Test', () => {
afterAll(() => {
jest.resetAllMocks();
});
test('should be an adult', async () => {
const actual = await isMinor();
expect(actual).toBeFalsy();
expect(getAge).toBeCalledWith(1991);
});
});
unit test result:
PASS examples/66288290/user.test.js
Age Test
✓ should be an adult (11 ms)
console.log
age: 99
at examples/66288290/user.js:6:11
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 87.5 | 50 | 100 | 87.5 |
user.js | 87.5 | 50 | 100 | 87.5 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.502 s

Mocking Date.Now jest toHaveBeenCalledWith in nestJs

I am trying to figure out how to mock a call to Date.now with jest in my nestjs application.
I have a repository method that soft deletes a resource
async destroy(uuid: string): Promise<boolean> {
await this.userRepository.update({ userUUID: uuid }, { deletedDate: Date.now() });
return true;
}
to soft delete we just add a timestamp of when it was requested to be deleted
Following some discussions on here and other sites I came up with this test.
describe('destroy', () => {
it('should delete a user schemas in the user data store', async () => {
const getNow = () => Date.now();
jest
.spyOn(global.Date, 'now')
.mockImplementationOnce(() =>
Date.now().valueOf()
);
const targetResource = 'some-uuid';
const result = await service.destroy(targetResource);
expect(result).toBeTruthy();
expect(userRepositoryMock.update).toHaveBeenCalledWith({ userUUID: targetResource }, { deletedDate: getNow() });
});
});
I assumed that .spyOn(global.Date) mocked the entire global dat function , but the Date.now() in my repository is still returning the actual date rather than the mock.
My question is, is there a way to provide the mock return value of Date.now called in the repository from the test or should I just DI inject a DateProvider to the repository class which I can then mock from my test?
jest.spyOn(Date, 'now') should work.
E.g.
userService.ts:
import UserRepository from './userRepository';
class UserService {
private userRepository: UserRepository;
constructor(userRepository: UserRepository) {
this.userRepository = userRepository;
}
public async destroy(uuid: string): Promise<boolean> {
await this.userRepository.update({ userUUID: uuid }, { deletedDate: Date.now() });
return true;
}
}
export default UserService;
userRepository.ts:
class UserRepository {
public async update(where, updater) {
return 'real update';
}
}
export default UserRepository;
userService.test.ts:
import UserService from './userService';
describe('60204284', () => {
describe('#UserService', () => {
describe('#destroy', () => {
it('should soft delete user', async () => {
const mUserRepository = { update: jest.fn() };
const userService = new UserService(mUserRepository);
jest.spyOn(Date, 'now').mockReturnValueOnce(1000);
const actual = await userService.destroy('uuid-xxx');
expect(actual).toBeTruthy();
expect(mUserRepository.update).toBeCalledWith({ userUUID: 'uuid-xxx' }, { deletedDate: 1000 });
});
});
});
});
Unit test results with 100% coverage:
PASS stackoverflow/60204284/userService.test.ts
60204284
#UserService
#destroy
✓ should soft delete user (9ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
userService.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.572s, estimated 11s
source code: https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60204284

Why is the spyOn function in the test not working with sendGrid?

I am setting up a graphql server with graphql-yoga and `prisma using Typescript. When a user signs up, an email with a link for validation will be sent to the given email address.
Everything is working fine, but i want to write a test for the mutation before refactoring the functionality, which checks if the 'send' function of SendGrid hast been called.
I tried spying on the function with jest.spyOn, but all I get is an error, that comes from not providing an API key for SendGrid in the tesing environment.
I have used spyOn before, and it worked, though this is the first time I am using jest with Typescript.
SignUp Mutation
import * as sgMail from '#sendgrid/mail';
sgMail.setApiKey(process.env.MAIL_API_KEY);
export const Mutation = {
async signUpUser(parent, { data }, { prisma }, info) {
[...]
const emailData = {
from: 'test#test.de',
to: `${user.email}`,
subject: 'Account validation',
text: `validation Id: ${registration.id}`
};
await sgMail.send(emailData);
return user;
}
}
Trying spyOn
import * as sgMail from '#sendgrid/mail';
const signUpUserMutation = gql`
mutation($data: ValidationInput) {
signUpUser (data: $data) {
id
email
}
}
`;
it('should send a registration email, with a link, containing the id of the registration', async () => {
spyOn(sgMail, "send").and.returnValue(Promise.resolve('Success'));
const variables = {
data: {
email: "test#test.de",
password: "anyPassword"
}
};
await client.mutate({ mutation: signUpUserMutation, variables});
expect(sgMail.send).toHaveBeenCalled();
});
Running the test gives me:
Error: GraphQL error: Unauthorized
Commenting out the function call of send in the mutation and running the test gives me:
Error: expect(spy).toHaveBeenCalled()
Expected spy to have been called, but it was not called.
You don't mock #sendgrid/mail module in a correct way. That's why the error happened. Here is the solution without using GraphQL test client. But you can use GraphQL test client to test your GraphQL resolver and GraphQL Schema after you mock #sendgrid/mail module correctly.
mutations.ts:
import * as sgMail from '#sendgrid/mail';
sgMail.setApiKey(process.env.MAIL_API_KEY || '');
export const Mutation = {
async signUpUser(parent, { data }, { prisma }, info) {
const user = { email: 'example#gmail.com' };
const registration = { id: '1' };
const emailData = {
from: 'test#test.de',
to: `${user.email}`,
subject: 'Account validation',
text: `validation Id: ${registration.id}`
};
await sgMail.send(emailData);
return user;
}
};
mutations.spec.ts:
import { Mutation } from './mutations';
import * as sgMail from '#sendgrid/mail';
import { RequestResponse } from 'request';
jest.mock('#sendgrid/mail', () => {
return {
setApiKey: jest.fn(),
send: jest.fn()
};
});
describe('Mutation', () => {
describe('#signUpUser', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('should send a registration email, with a link, containing the id of the registration', async () => {
(sgMail.send as jest.MockedFunction<typeof sgMail.send>).mockResolvedValueOnce([{} as RequestResponse, {}]);
const actualValue = await Mutation.signUpUser({}, { data: {} }, { prisma: {} }, {});
expect(actualValue).toEqual({ email: 'example#gmail.com' });
expect(sgMail.send).toBeCalledWith({
from: 'test#test.de',
to: 'example#gmail.com',
subject: 'Account validation',
text: `validation Id: 1`
});
});
});
});
Unit test result with 100% coverage:
PASS src/stackoverflow/56379585/mutations.spec.ts (12.419s)
Mutation
#signUpUser
✓ should send a registration email, with a link, containing the id of the registration (23ms)
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
mutations.ts | 100 | 100 | 100 | 100 | |
--------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.315s
Here is the completed demo: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/56379585

Resources