I'm going to test typescript(inversify) code using jest. Below mentioned the code that use to test a controller method.
describe('Test UserController',() =>{
let userService : UserServiceImpl = new UserServiceImpl();
let userController : UserController = new UserController(userService);
let req, res, next;
beforeEach(() => {
req = httpMocks.createRequest();
res = httpMocks.createResponse();
next = jest.fn();
req.authorization = 'eyJIjoxNjE0NTc2Mzc3LCJleHAiOjE2NDYxMTIzNzd9';
jest.mock("../../service/user-service-impl");
});
afterEach(() =>{
jest.resetAllMocks();
});
it('getUser method should work properly',async() => {
const userResponseStub : UserResponse = new UserResponse();
userResponseStub.username = 'username';
userResponseStub.email = 'email@test.com';
jest.spyOn(userService, "getUser").mockResolvedValue(userResponseStub);
const response = await userController.getUser(1);
expect(response).toBe(userResponseStub);
});
});
Below mentioned the configurations related to jest in package.json
file
"jest": {
"verbose": true,
"testResultsProcessor": "jest-sonar-reporter",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.{js,jsx,tsx,ts}",
"!**/node_modules/**",
"!**/vendor/**"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
],
"coverageReporters": [
"lcov",
"text"
],
"transform": {
"^.+\\.(ts|tsx)$": "<rootDir>/preprocessor.js"
},
"testRegex": [
"(/__tests__/.*|(\\.|/)(test|spec))\\.(ts?|tsx?)$"
]
},
"jestSonar": {
"sonar56x": true
}
Test case is run without any issue. But I'm going to run the application using npm start
, getting below error.
describe('Test UserController',() =>{
^
ReferenceError: describe is not defined
Please help me to solve this issue. Any help or workarounds are really appriciated.