//test let gameStorageServiceStub: SinonStubbedInstance<GameStorageService>; let…
- User
//test let gameStorageServiceStub: SinonStubbedInstance; let socketManagerServiceStub: SinonStubbedInstance; let sandbox: SinonSandbox; let expressApp: Express.Application; beforeEach(async () => { sandbox = createSandbox(); gameStorageServiceStub = createStubInstance(GameStorageService); socketManagerServiceStub = createStubInstance(SocketManager);
codeconst app = Container.get(Application); Object.defineProperty(app['gamesController'], 'gameStorageService', { value: gameStorageServiceStub }); Object.defineProperty(app['gamesController'], 'socketManagerService', { value: socketManagerServiceStub }); expressApp = app.app; });//games controller : this.router.post('/saveGame', async (req: Request, res: Response) => {
codethis.gameStorageService.storeGameImages(receivedNameForm.gameId, buffer1, buffer2); const newGameToAdd: GameData = { }; this.gameStorageService .storeGameResult(newGameToAdd) .then(() => { // we need to send a socket to refresh the game list this.socketManagerService.sendRefreshAvailableGames(); res.status(StatusCodes.CREATED).send({ body: receivedNameForm.gameName }); }) .catch((error: Error) => { res.status(StatusCodes.NOT_FOUND).send(error.message); }); });//error :
- GamesController "before each" hook for "fetchGame should catch error": TypeError: Cannot redefine property: socketManagerService
Fix the error
- Assistant
It looks like the error is caused by trying to redefine
socketManagerServicewhen its property descriptor is non-configurable. To fix this error, you can make the property descriptor configurable while creating it.Here's how you can do this, inside the
beforeEachblock:typescriptbeforeEach(async () => { sandbox = createSandbox(); gameStorageServiceStub = createStubInstance(GameStorageService); socketManagerServiceStub = createStubInstance(SocketManager); const app = Container.get(Application); Object.defineProperty(app['gamesController'], 'gameStorageService', { value: gameStorageServiceStub, configurable: true // add this line }); Object.defineProperty(app['gamesController'], 'socketManagerService', { value: socketManagerServiceStub, configurable: true // add this line }); expressApp = app.app; });