0

I'm working on a sample ExpressJs application written with Typescript in which I tried to implement Jest. I have a service class which I want to test but it seems like Jest is looking at the wrong source codes when I run the actual test.

Image of error

If you look at the stack trace of the error, it seems it is executing a lot more than it should. I'm only testing auth.service.ts but it seems it is scanning a lot more. Below is my auth.service.ts file.

import { generateUuid, isPasswordMatching, redisClient } from '../utils';
import { getUserService } from './user.service';

class AuthService {
  private userService = getUserService();

  async login(username: string, password: string): Promise<string | null> {
    const user = await this.userService.getUserByUsername(username);

    if (!user) return null;

    if (!(await isPasswordMatching(password, user.password))) return null;

    if (!user.token) {
      user.token = generateUuid();
      await user.save();
    }

    await redisClient.set(user.token, user.userId);

    return user.token;
  }
}

const authService = new AuthService();

export const getAuthService = (): AuthService => {
  return authService;
};

And here is my test file which is what I'm trying to execute.

import { getAuthService } from '../../src/services/auth.service';

const authService = getAuthService();

test('auth service init', () => {
  expect(authService).not.toBeNull();
});

Any idea on what could be wrong?

UPDATE: posting my auth.route.ts file as well

import { Request, Response, Router } from 'express';
import { getAuthService } from '../services';
import { loginSchema, validateParams } from '../validators';

const authService = getAuthService();

export const authRoute = (router: Router) => {
  router.post('/login', async (req: Request, res: Response) => {
    const { data, error } = await validateParams(req.body, loginSchema);

    if (error) {
      res.status(400).send({ message: error });
      return;
    }

    const token = await authService.login(data.username, data.password);

    if (!token) {
      res.status(400).send({ message: `Invalid credentials.` });
      return;
    }

    res.status(200).send({ token });
  });
};
Oneb
  • 145
  • 1
  • 12
  • Show auth.route.ts as that is where the issue was encountered. Jest is running the correct code based on your imports. – morganney Apr 19 '23 at 14:08
  • @morganney can you explain why jest is running my auth.route.ts? based on my imports, there is nothing there importing my route. i'll update my post and add your requested code. – Oneb Apr 19 '23 at 14:13
  • @morganney I've updated my post and added my auth.route.ts file as requested. – Oneb Apr 19 '23 at 14:15
  • Upon further reading, looks like this issue is related to this: https://stackoverflow.com/questions/58490472/barrel-files-webpack-and-jest – Oneb Apr 19 '23 at 14:48
  • The file you are testing depends on it. Obvious. – morganney Apr 19 '23 at 16:05
  • No it doesn't. Auth service does not depende on auth route. The issue is with the barrel files which jest crawls when it looks on it. So, not obvious. – Oneb Apr 19 '23 at 16:11
  • Now it appears you may have a cycle in your dependency graph. Do you have index files re-exporting modules? What is in utils/index? Stop blaming things on jest, it is helping you find bugs in your structure. – morganney Apr 19 '23 at 23:04
  • I'm not in any way blaming jest. I just didn't know that jest crawls on exported files on the barrel files. Anyway, I solved the issue by removing my barrel files and importing sole files on my codes. – Oneb Apr 20 '23 at 05:46

0 Answers0