17

I have followed the unit test example but I am unable to run a test, i don't know why it doesn't work. I have this following error : Cannot find module 'src/Application/Auth/Command/LoginCommandHandler' from 'LoginCommandHandler.spec.ts' but my handler is correctly imported. Thanks for your help.

Here is my unit test:

import { LoginCommandHandler } from 'src/Application/Auth/Command/LoginCommandHandler';
import { UserRepository } from 'src/Infrastructure/User/Repository/UserRepository';
import { EncryptionAdapter } from 'src/Infrastructure/Adapter/EncryptionAdapter';

// ...

const module: TestingModule = await Test.createTestingModule({
      providers: [LoginCommandHandler, UserRepository, EncryptionAdapter],
    }).compile();

    userRepository = module.get(UserRepository);
    encryptionAdapter = module.get(EncryptionAdapter);
    handler = new LoginCommandHandler(userRepository, encryptionAdapter);

Here is my src/Application/Auth/Command/LoginCommandHandler/LoginCommandHandler :

export class LoginCommandHandler {
  constructor(
    @Inject('IUserRepository')
    private readonly userRepository: IUserRepository,
    @Inject('IEncryptionAdapter')
    private readonly encryptionAdapter: IEncryptionAdapter,
  ) {}
// ...

And here is my AuthModule :

@Module({
  imports: [
    // ...
    TypeOrmModule.forFeature([User]),
  ],
  providers: [
    // ...
    { provide: 'IUserRepository', useClass: UserRepository },
    { provide: 'IEncryptionAdapter', useClass: EncryptionAdapter },
    LoginCommandHandler,
  ],
})
export class AuthModule {}

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
Mathieu Marchois
  • 209
  • 1
  • 4
  • 10

3 Answers3

37

Add this to your jest config in the package.json file:

"moduleNameMapper": {
  "^src/(.*)$": "<rootDir>/$1"
}
Eliezer Steinbock
  • 4,728
  • 5
  • 31
  • 43
13

Jest is having trouble finding the module related to the absolute path you are using to import. You can find more information at this stackoverflow question.

In short, you just need to tell Jest about where to look for your modules (either in the moduleDirectories field from the jest.config, or in the moduleNameMapper also in the jest.config)

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
12

Use relative paths instead of absolute paths

import statement with absolute path looks like:

import { AuthService } from 'src/auth/auth.service'

import statement with relative path looks like:

import { AuthService } from '../auth/auth.service'

The creator of the Nest.js suggests here and here that using the absolute paths is a bad practice. Also, the TypeScript's official documentation recommends using relative paths for our own modules. There's also an ESLint rule that explains why absolute paths should be avoided. You need to convert the paths of the import statements in your project to the relative paths as shown above.


Configure VS Code to import using relative paths by default

VS Code by default uses absolute path as shown above, when we auto-import using code completion or Command/Ctrl + .. We need to change this default setting to use relative paths.

Go to VS Code settings and search for a setting: Import Module Specifier. Change it from shortest to relative. Make sure you change it in the TypeScript option, not just JavaScript.

Now from here on, VS Code will automatically import using the relative paths.


That's it! Hope that helps!

Yogesh Umesh Vaity
  • 41,009
  • 21
  • 145
  • 105
  • 2
    Thanks for the VS Code default settings tip. Super helpful and it sorted my module/testing issues in Nest. – Loren.A Dec 18 '21 at 16:50
  • 1
    This was a very useful answer - thank you. Are there any downsides to using relative paths instead of absolute paths? – Michael Jay Jun 13 '22 at 15:14
  • @MichaelJay, None that I'm aware of. In the past, we used to have problems with relative paths when we moved files around. But now the IDEs are gettting smarter, they suggest us to automatically refactor the changed paths. – Yogesh Umesh Vaity Jun 25 '22 at 11:34