need some advice on creating a TS class with private/protected readonly members.
asset.ts
STOCK,
CRYPTO,
}
export abstract class Asset {
protected readonly _type: AssetType;
protected readonly _symbol: string;
protected readonly _name: string;
protected _amount: number;
protected readonly _initialPrice: number;
constructor(
type: AssetType,
symbol: string,
name: string,
amount: number,
initialPrice: number,
) {
this._type = type;
this._symbol = symbol;
this._name = name;
this._amount = amount;
this._initialPrice = initialPrice;
}
get type(): AssetType {
return this._type;
}
get symbol(): string {
return this._symbol;
}
get name(): string {
return this._name;
}
get amount(): number {
return this._amount;
}
set amount(amount: number) {
this._amount = amount;
}
get initialPrice(): number {
return this._initialPrice;
}
}
stock.ts:
import { Asset, AssetType } from '@core/asset';
export class Stock extends Asset {
constructor(
symbol: string,
name: string,
amount: number,
initialPrice: number,
) {
super(AssetType.STOCK, symbol, name, amount, initialPrice);
}
}
broker.test.ts
import { describe, it, expect, beforeAll } from 'vitest';
import { Broker } from '@core/broker';
import { Position } from '@core/position';
import { Stock } from '@core/stock';
let stock: Stock;
beforeAll(() => {
stock = new Stock('XYZ', 'XYZ Co.', 1000, 1.0);
});
describe('addPosition', () => {
it("should add position to the broker's positions", () => {
const positions: Position[] = [];
const broker: Broker = new Broker('John Smith', positions);
const newPosition: Position = new Position(stock, 1);
expect(broker.positions).toEqual([newPosition]);
});
});
Result after running vitest:
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL src/core/broker/broker.test.ts > addPosition
TypeError: Cannot set property asset of #<Position> which has only a getter
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
FAIL src/core/broker/broker.test.ts > addPosition > should add position to the broker's positions
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
TypeError: Cannot read properties of undefined (reading 'stacks')
❯ parseStacktrace file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-utils-timers.8a5e7cd5.js:5678:9
❯ printError file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:10045:18
❯ Vitest.printError file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:10623:12
❯ VerboseReporter.printTaskErrors file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:8640:22
❯ VerboseReporter.reportSummary file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:8591:18
❯ processTicksAndRejections node:internal/process/task_queues:96:5
❯ async VerboseReporter.onFinished file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:8486:5
❯ async VerboseReporter.onFinished file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:9266:5
❯ async Vitest.report file:/home/ffknob/Entwicklung/workspace/ffknob/x-udemy-js-unit-testing/node_modules/vitest/dist/chunk-vite-node-externalize.92c54acc.js:10574:5
So, since I’m not the one setting the property explicit with a setter, I guess that’s something vitest is doing somehow…
Any thoughts?
Thank you