8

Trying to unit test. Got following error:

TypeError: Cannot read property 'prototype' of undefined

export class UserService {

constructor(@InjectRepository(User) private readonly userRepository: Repository < User>) { }

spec.ts:

describe('AuthController', () => {
let authController: AuthController;
let authService: AuthService;
let mockRepository = {

};
beforeEach(async () => {
    const module = await Test.createTestingModule({
        imports: [
            TypeOrmModule.forFeature([User]),
        ],
        controllers: [AuthController],
        providers: [AuthService, {
            provide: getRepositoryToken(User),
            useValue: mockRepository
        }]
    }).compile()
    authService = module.get<AuthService>(AuthService);
    authController = module.get<AuthController>(AuthController)
});

Can someone share a solution please?

MORE INFO:

So seems like its something wrong with typeorm

beforeEach(async () => {
    const module = await Test.createTestingModule({

    }).compile()
    authService = module.get<AuthService>(AuthService);
    authController = module.get<AuthController>(AuthController)
});

With this code I'm getting exact the same error. So only problem is adding typeorm to this test Module.

So it fails because of dependency: AuthController->AuthService->UserService->TypeORM

Btw just checked UserService using API with Postman and it works fine.

Still no result:

 module = await Test.createTestingModule({
        controllers: [AuthController],
        components: [
            {
                provide: AuthService,
                useValue: {}
            },
            {
                provide: UserService,
                useValue: {}
            },
            {
                provide: getRepositoryToken(User),
                useValue: {}
            }
        ],
        providers: [
            {
                provide: AuthService,
                useValue: {}
            },
            {
                provide: UserService,
                useValue: {}
            },
            {
                provide: getRepositoryToken(User),
                useValue: {}
            }
        ]
    }).compile()
    this.authController = module.get<AuthController>(AuthController)

Also

class AuthServiceMock {
    logIn(userName) {
        return { id:100, isDeleted:false, login:"login", password:"password"};
    }

    signUp() {
        return { expireIn: 3600, token:"token" };
    }
}

describe('AuthController', () => {
let module: TestingModule;
let authController: AuthController;
let authService: AuthService;

beforeEach(async () => {
    module = await Test.createTestingModule({
        controllers: [AuthController],
        components: [

        ],
        providers: [
            {
                provide: AuthService,
                useClass: AuthServiceMock
            },
        ]
    }).compile()
    this.authController = module.get<AuthController>(AuthController)
});
2
  • Did you solve it somehow? I"m having the same issue Commented Apr 28, 2019 at 5:38
  • @alex88 yes. I recreated project and copied all old files to new. Seems like it was some typeorm bug. Commented Apr 28, 2019 at 9:45

4 Answers 4

4

I looked at the project you provided in a comment under Kim Kern (https://github.com/rankery/wof-server)

You are using a barrel file (src/user/index.ts), exporting the UserModule

export * from './user.module';

I guess you are using this barrel file to import the module later on.

Now, every time the content of the barrel file is imported , the code contained in the built version of your src/user/user.module.ts is executed, which include the decoration of the UserModule class, which in turns will make Typeorm try to build a Repository, which causes the error.

You should remove this export from src/user/index.ts (or simply delete the file) and fix the broken imports caused by this removal and it should work.

Sign up to request clarification or add additional context in comments.

2 Comments

In order to let more user benefit form your answer, could you provide more explanation please.
You saved me alive!
2

I just passed the User entity to Repository and it works.

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private readonly userRepository: Repository<User>
    ) { }

}

Checkout docs from here: https://docs.nestjs.com/techniques/database. They have pretty good docs.

Comments

2

I 've spent hours to figure it out and it works

 async findAll() {
    
    return await this.userRepository.createCursor(this.userRepository.find()).toArray();
  }

Comments

0

When you import TypeOrmModule.forFeature(...) you also have to import TypeOrmModule.forRoot(...). But in a unit test, you probably don't want to work with your database but instead mock out all dependencies.

Your controller should not access the database directly, that's what the service is for. So if you want to test your controller and it only injects the service, you should only declare an AuthService mock and not import anything:

const module = await Test.createTestingModule({
    controllers: [AuthController],
    providers: [{
        provide: AuthService,
        useValue: authServiceMock
    }]
}).compile()

If you want to test your AuthService and it only injects the repository declare your mockRepository and leave everything else out.

6 Comments

UserService injects repository and AuthService injects UserService. I tryed ur code but still getting same error.
Weird, typeorm is not involved at all in the controller test. What's the stacktrace of the error, where does it point to?
Have a look at this test for reference: github.com/kiwikern/shassi-nest/blob/master/src/auth/…
Its point to @InjectRepository(User) at at Object.getRepositoryToken
That's really weird. The actual UserService should never be instantiated when you unit test the AuthController or the AuthService with mocks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.