I have written a NestJS test repo. While in the beforeAll I'm fetching the data and assigning it to proper variables, in the describe.each the values appear undefined.
Here is the following code:
...
relevant imports
...
describe('Modules', () => {
let app: INestApplication;
let configService: ConfigService;
let apiService: ApiService;
let sutUrl: string;
let userLoginToken: string;
let adminLoginToken: string;
beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
configService = moduleFixture.get<ConfigService>(ConfigService);
apiService = moduleFixture.get<ApiService>(ApiService);
sutUrl = configService.get('SUT_URL');
adminToken = await apiService.getLoginToken({
email: configService.get('ADMIN_EMAIL'),
password: configService.get('ADMIN_PASSWORD'),
});
userToken = await apiService.getoginToken({
email: configService.get<string>('USER_EMAIL'),
password: configService.get<string>('USER_PASSWORD'),
});
app = moduleFixture.createNestApplication();
await app.init();
});
describe.each([
{ userType: UserType.Admin, loginToken: adminToken },
{ userType: UserType.User, loginToken: userToken },
])('validate main modules are loaded', ({ loginToken }) => {
it('should get a non-empty filter list', async () => {
return request(sutUrl) <- the is working
.post(`some/route`)
.set({ Authorization: `Bearer ${loginToken}` }) <- this is undefined
........restOfMethod......
});
...
});
So while debugging and console.log in the beforeAll, right before the await app.init() i get the actual values i need for the describe.each. when hovering over the values of each loginToken, the correct values are shown.
BUT when destructing in the 'it' method... it's undefined. Couldn't find why it happens.
- The ConfigModule in the AppModule is defined 'isGlobal: true'