question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Vitest+Typescript: TypeError: Cannot set property X of #<Y> which has only a getter

See original GitHub issue

Describe the bug

I’m having some trouble and need some advice on creating a TS class with private/protected readonly members and have it pass my vitest tests.

asset.ts

export enum AssetType {
  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…

I can manage it to work if I remove the readonly modifier and create a setter for the propertuy, but I wonder if I can pass this error without having to make changes to my code.

Any thoughts?

Thank you

Reproduction

(see example code above)

System Info

Need to install the following packages:
  envinfo
Ok to proceed? (y) 

  System:
    OS: Linux 5.17 openSUSE Tumbleweed 20220506
    CPU: (8) x64 Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
    Memory: 3.84 GB / 15.41 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.14.0 - ~/.nvm/versions/node/v16.14.0/bin/node
    Yarn: 1.22.18 - /usr/bin/yarn
    npm: 8.3.1 - ~/.nvm/versions/node/v16.14.0/bin/npm
  Browsers:
    Chrome: 101.0.4951.54
    Firefox: 100.0
  npmPackages:
    vitest: ^0.10.4 => 0.10.4 

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
hnrqcommented, Aug 29, 2022

@sheremet-va any guess on when the next release will be out? 🥺

1reaction
sheremet-vacommented, Aug 27, 2022

Fixed with #1921

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vitest+Typescript: TypeError: Cannot set property X of #<Y ...
Vitest +Typescript: TypeError: Cannot set property X of #<Y> which has only a getter ; from '@core/asset'; export ; Stock extends Asset {...
Read more >
TypeError: setting getter-only property "x" - JavaScript | MDN
The JavaScript strict mode-only exception "setting getter-only property" occurs when there is an attempt to set a new value to a property for...
Read more >
TypeError: Cannot set Property which has only a Getter in JS
The "Cannot set property which has only a getter" error occurs when trying to set a new value to a property, for which...
Read more >
API Reference | Vitest
You can also access object properties with $ prefix, ... import { bench } from 'vitest' bench.only('normal sorting', () => { const x...
Read more >
Getting Started with Vitest | Vue Mastery
But this has led to a new problem: writing unit tests for applications that run on Vite. Using frameworks like Jest with Vite...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found