Creating an application using NestJS
and utilizing ts-jest
for e2e testing.
For the code repository, visit:
https://github.com/redplane/jest-issue
A controller is set up with the following structure:
@Controller({
path: 'api/shape',
scope: Scope.REQUEST,
})
export class ShapeController {
public constructor(
@Inject(AppInjectors.SERVICE__JOI)
private readonly __joiService: IJoiService
) {}
@Get()
public async processAsync(@Query() query: ProcessShapeQuery): Promise<void> {
// Validate parameters
console.log(ShapeDimensions);
await this.__joiService.validateAsync(query, processImageQueryJoiSchema);
}
}
Postman API requests work seamlessly, displaying:
{ WIDTH: 'WIDTH', HEIGHT: 'HEIGHT' }
However, during e2e tests using the command:
npm run e2e:api
An error surfaced:
TypeError: Cannot read properties of undefined (reading 'PIXEL')
5 | export const imageFrameOptionsJoiSchema = Joi.object<ShapeFrameOptions>({
6 | size: Joi.number().min(1),
> 7 | unit: Joi.any().valid(ShapeUnits.PIXEL),
| ^
8 | dimension: Joi.any().valid(ShapeDimensions.HEIGHT, ShapeDimensions.WIDTH)
9 | });
...more errors displayed...
Commenting out:
await this.__joiService.validateAsync(query, processImageQueryJoiSchema);
Causes console.log(ShapeDimensions);
to return undefined. Enum printouts still function normally.
Referencing my jest.config.ts
:
/* eslint-disable */
export default {
displayName: 'api-e2e',
preset: '../../jest.preset.js',
globalSetup: '<rootDir>/src/support/global-setup.ts',
globalTeardown: '<rootDir>/src/support/global-teardown.ts',
setupFiles: ['<rootDir>/src/support/test-setup.ts'],
testEnvironment: 'node',
transform: {
'^.+\\.[tj]s$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
},
],
},
moduleNameMapper: {
"@image-service/api": "<rootDir>/../api/src/index.ts",
"@image-service/api-business": "<rootDir>/../../libs/api-business/src"
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/api-e2e',
};
Tried solutions from:
Typescript runtime error: cannot read property of undefined (enum)
-> No circular dependency detected upon checking
Any suggestions are appreciated.
Thank you,