Reputation: 1355
When I run the e2e test(as integration tests) I get a getRepository null error. I import the entire app module in my test spec file(below),the error occurs, which says something like this:
[Nest] 47867 - 08/22/2022, 8:07:35 PM ERROR [ExceptionsHandler] Cannot read property 'getRepository' of null
TypeError: Cannot read property 'getRepository' of null
TypeError: Cannot read property 'getRepository' of null
at new usersService (/mypath/src/users/users.service.ts:35:42)
at Injector.instantiateClass (/mypath/node_modules/@nestjs/core/injector/injector.js:330:19)
at callback (/mypath/node_modules/@nestjs/core/injector/injector.js:48:41)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at Injector.loadInstance (/mypath/node_modules/@nestjs/core/injector/injector.js:52:9)
at Injector.loadProvider (/mypath/node_modules/@nestjs/core/injector/injector.js:74:9)
at Injector.resolveComponentHost (/mypath/node_modules/@nestjs/core/injector/injector.js:192:13)
at async Promise.all (index 0)
at Injector.loadCtorMetadata (/mypath/node_modules/@nestjs/core/injector/injector.js:358:23)
at Injector.resolveConstructorParams (/mypath/node_modules/@nestjs/core/injector/injector.js:89:26)
FAIL test/user.service.int-spec.ts (10.525 s)
GET /users
✓ (GET) / (102 ms)
✕ (GET) /users (62 ms)
● GET /users › (GET) /users
expected 200 "OK", got 500 "Internal Server Error"
26 |
27 | it('(GET) /users', () => {
> 28 | return request(app.getHttpServer()).get('/users').expect(200);
| ^
29 | });
30 | });
31 |
at Object.<anonymous> (test/user.service.int-spec.ts:28:59)
----
at Test._assertStatus (node_modules/supertest/lib/test.js:252:14)
at node_modules/supertest/lib/test.js:306:17
at Test._assertFunction (node_modules/supertest/lib/test.js:285:13)
at Test.assert (node_modules/supertest/lib/test.js:164:23)
at Server.localAssert (node_modules/supertest/lib/test.js:120:14)
The folder structure is this:
parent_folder
| package.json
| package-lock.json
| ---src
| \-User
| \-user.module.ts
| \-user.service.ts
| \-user.controller.ts
|...
| ---tests
| \-jest-int.json
| \-user.int-spec.ts
The jest config file contains this:
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": "../", // <<<--- I use absolute path path, so rootDir is "../"
"roots": ["<rootDir>"],
"modulePaths": ["<rootDir>"],
"moduleDirectories": ["node_modules"],
"testEnvironment": "node",
"testRegex": ".int-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
The module contains this only:
import { Module } from '@nestjs/common';
import { UsersController } from './Users.controller';
import { UsersService } from './Users.service';
@Module({
providers: [UsersService],
controllers: [UsersController],
})
export class UserModule {}
The service contains:
@Injectable()
export class UsersService {
private userRepository: Repository<User>;
private fileRepository: Repository<file>;
private filedworthRepository: Repository<filedworth>;
private filedValueRepository: Repository<filedValue>;
private fileStrategyRepository: Repository<fileStrategy>;
private valueRepository: Repository<Value>;
private defaultData = {
created_date: new Date(),
updated_date: new Date(),
created_by: 'user',
updated_by: 'user',
};
constructor(@Inject(CONNECTION) private connection: DataSource) {
this.userRepository = connection.getRepository(user);
this.fileRepository = connection.getRepository(file);
this.filedworthRepository =
connection.getRepository(filedworth);
this.filedValueRepository =
connection.getRepository(filedValue);
this.fileStrategyRepository =
connection.getRepository(fileStrategy);
this.ValueRepository = connection.getRepository(Value);
}
...//other methods
}
The controller is:
@Controller('users')
export class usersController {
constructor(private usersService: usersService) {}
@Get()
getusers(
@Query()
query: GetUsersFilterDto,
) {
return this.usersService.getusers(query);
}
...//others
}
and the test file itself contains the following:
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { AppModule } from './../src/app.module';
describe('GET /users ', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
app = moduleFixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
it('(GET) /', () => {
return request(app.getHttpServer()).get('/').expect(200);
});
it('(GET) /users', () => {
return request(app.getHttpServer()).get('/users').expect(200);
});
});
I run the test using:
npm run test:int // "test:int": "jest -i --no-cache --detectOpenHandles --config ./test/jest-int.json"
Is there something I'm missing? Hopefully these are enough details to convey my issue.
Upvotes: 1
Views: 201
Reputation: 1355
What Georgi has mentioned was true, I hadn't provided the values of the dependency CONNECTION
that was being injected. But, the syntax mentioned there didn't work, this did:
beforeAll(async () => {
const connection = await DataSource(myDB_Config);//hosturl, port etc.
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(CONNECTION)
.useValue(connection)
.compile();
app = moduleFixture.createNestApplication();
await app.init();
});
Upvotes: 2
Reputation: 74
You need to add CONNECTION as a provider to your test app: TestingModule =
await Test.createTestingModule({
imports: [AppModule],
providers: [
{ provide: CONNECTION, getRepository: jest.fn() },
],
}).compile();
Upvotes: 2