'Why Jest not working as expected after upgrading to the latest version 27.5.1?
I have an existing repo that uses NestJs as base framework and Jest for testing. I am trying to upgrade the package versions and after upgrading jest to the latest version, my existing tests are not passing and also jest is not working as expected. Below is my code (reduced to only capture the errors I am having)
import { Test, TestingModule } from '@nestjs/testing';
describe('OptinOptoutService', () => {
let module: TestingModule;
let optinOptoutService: OptinOptoutService;
let userOptinService: any;
beforeEach(async () => {
userOptinService = {
isUserOptedIn: jest.fn().mockImplementation(() => Promise.resolve(true)),
getUserOptInDocument: jest.fn().mockImplementation(() => Promise.resolve({
customerId: 'some-customer-id',
isOptedIn: true,
})),
updateOptInStatus: jest.fn().mockImplementation(() => Promise.resolve(true)),
};
module = await Test.createTestingModule({
imports: [
// module imports here
],
providers: [
OptinOptoutService,
{
provide: UserOptinService,
useValue: userOptinService, // using my mock here
},
],
}).compile();
userOptinService = module.get(UserOptinService);
optinOptoutService = module.get(OptinOptoutService);
});
afterEach(() => {
jest.useRealTimers();
})
describe('handleOptIn', () => {
it('should return data if already opted-in', async () => {
// Setup a mock user that's already opted in
const getUserOptInDocumentSpy = jest.spyOn(userOptinService, 'getUserOptInDocument');
const result = await optinOptoutService.handleOptIn(user, 'idType');
expect(result).toEqual({
licenceNumber: 'licenceNumber',
licenceType: 'someType',
});
// this was working with jest v26.0.1, now not working with jest v27.5.1
expect(getUserOptInDocumentSpy).toHaveBeenCalledTimes(1);
// had to add setTimeout() like below to get the above test pass
// setTimeout(() => expect(getUserOptInDocumentSpy).toHaveBeenCalledTimes(1), 1);
});
it('should throw photocard error if RMS throws RMS_NO_ELIGIBLE_PRODUCTS', async () => {
expect.assertions(2);
// this was working with jest v26.0.1, now with jest v27.5.1, it keeps using the
// mock implementation that I have in beforeEach() block
jest.spyOn(userOptinService, 'getUserOptInDocument').mockImplementation(() =>
Promise.resolve(null)
);
try {
await optinOptoutService.handleOptIn(user, 'idType');
} catch (error: any) {
expect(error).toBeInstanceOf(CustomError);
expect(error.message).toEqual('Custom error message.');
}
});
});
});
I have tried some comments inside code to help you understand the problems bit better (I hope). My current package.json file
{
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@nestjs/axios": "^0.0.6",
"@nestjs/common": "8.4.0",
"@nestjs/core": "8.4.0",
"@nestjs/mongoose": "8.0.0",
"@nestjs/platform-express": "^8.4.0",
"@nestjs/testing": "8.4.0",
"@types/express": "4.17.13",
"@types/jest": "27.4.1",
"@types/jest-when": "^3.5.0",
"@types/node": "17.0.21",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "5.13.0",
"@typescript-eslint/eslint-plugin-tslint": "5.13.0",
"@typescript-eslint/parser": "5.13.0",
"babel-jest": "^26.6.3",
"class-transformer": "0.3.1",
"comment-json": "^4.1.0",
"eslint": "8.10.0",
"eslint-config-airbnb-typescript": "^16.1.0",
"eslint-config-prettier": "8.5.0",
"eslint-import-resolver-typescript": "2.5.0",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-jest": "26.1.1",
"eslint-plugin-prettier": "4.0.0",
"eslint-plugin-regex": "1.8.0",
"express": "4.17.3",
"jest": "^27.5.1",
"jest-when": "^3.0.1",
"jose": "2.0.5",
"jsonwebtoken": "^8.5.1",
"lint-staged": "^10.3.0",
"mongoose": "6.2.4",
"node-mocks-http": "1.11.0",
"pre-commit": "^1.2.2",
"prettier": "2.1.2",
"reflect-metadata": "0.1.13",
"rxjs": "7.1.0",
"strong-mock": "^6.0.0",
"supertest": "^6.1.3",
"ts-jest": "27.1.3",
"tslib": "^2.3.1",
"tslint": "6.1.2",
"typescript": "4.6.2",
"winston": "3.2.1",
"winston-transport": "^4.4.0"
},
"pre-commit": "lint-staged",
"lint-staged": {
"*.ts": [
"prettier -l --write",
"eslint -c .eslintrc.js --fix --ext .ts"
]
},
"dependencies": {
"@nestjs/swagger": "^5.2.0",
"@types/tough-cookie": "^4.0.0",
"jest-serial-runner": "^1.1.0",
"rxjs-etc": "^10.6.2",
"weak-napi": "^2.0.2"
},
"config": {
"mongodbMemoryServer": {
"version": "latest",
"debug": false
}
},
"resolutions": {
"axios": "0.21.4",
"**/follow-redirects": "^1.14.7",
"**/node-fetch": "^2.6.7"
}
}
My old package.json file
{
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"@babel/preset-typescript": "^7.12.7",
"@nestjs/common": "7.6.18",
"@nestjs/core": "7.3.1",
"@nestjs/mongoose": "7.2.3",
"@nestjs/platform-express": "^7.3.2",
"@nestjs/testing": "7.3.1",
"@types/express": "4.17.7",
"@types/jest": "26.0.4",
"@types/jest-when": "^2.7.2",
"@types/node": "14.0.18",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.1.1",
"@typescript-eslint/eslint-plugin-tslint": "^4.1.1",
"babel-jest": "^26.6.3",
"class-transformer": "^0.3.1",
"comment-json": "^4.1.0",
"eslint": "7.2.0",
"eslint-config-airbnb-typescript": "^10.0.0",
"eslint-config-prettier": "^6.11.0",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.0.1",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-regex": "^1.3.0",
"express": "4.17.1",
"jest": "^26.0.1",
"jest-when": "^3.0.1",
"jose": "1.27.2",
"jsonwebtoken": "^8.5.1",
"lint-staged": "^10.3.0",
"mongoose": "5.13.9",
"node-mocks-http": "1.8.1",
"pre-commit": "^1.2.2",
"prettier": "2.1.2",
"reflect-metadata": "0.1.13",
"rxjs": "6.6.0",
"strong-mock": "^6.0.0",
"supertest": "^6.1.3",
"ts-jest": "26.1.1",
"tslint": "6.1.2",
"typescript": "3.9.6",
"winston": "3.2.1",
"winston-transport": "^4.4.0"
},
"pre-commit": "lint-staged",
"lint-staged": {
"*.ts": [
"prettier -l --write",
"eslint -c .eslintrc.js --fix --ext .ts"
]
},
"dependencies": {
"@nestjs/swagger": "^4.7.11",
"@types/tough-cookie": "^4.0.0",
"jest-serial-runner": "^1.1.0",
"rxjs-etc": "^10.5.2",
"weak-napi": "^2.0.2"
},
"config": {
"mongodbMemoryServer": {
"version": "latest"
}
},
"resolutions": {
"axios": "0.21.4",
"**/follow-redirects": "^1.14.7",
"**/node-fetch": "^2.6.7"
}
}
Please suggest what am I missing here. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
