Unable to run tests because Nest cannot find a module

Viewed 7522

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 {}

3 Answers

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

"moduleNameMapper": {
  "^src/(.*)$": "<rootDir>/$1"
}

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)

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 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!

Related