Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mockReturnValue does not override class methods #645

Closed
6 tasks done
cexbrayat opened this issue Jan 27, 2022 · 0 comments · Fixed by #646
Closed
6 tasks done

mockReturnValue does not override class methods #645

cexbrayat opened this issue Jan 27, 2022 · 0 comments · Fixed by #646

Comments

@cexbrayat
Copy link
Contributor

Describe the bug

It looks like mockReturnValue does not always work as expected (at least when used with classes).

Let's say you have an App class, that calls a method run on each of its deps:

  class Dep {
    run(): boolean {
      // does something random
    }
  }

  class App {
    constructor(private deps: Array<Dep>) {}

    start(): Array<Dep> {
      return (
        this.deps
          // run every dep
          // and only keeps the ones that started
          .filter((dep) => dep.run())
      );
    }
  }

The following simple test fails:

describe('App', () => {
    let dep1: Dep;
    let dep2: Dep;
    let app: App;

    beforeEach(() => {
      dep1 = new Dep();
      // first dep starts
      vi.spyOn(dep1, 'run').mockReturnValue(true);

      dep2 = new Dep();
      // second dep does not
      vi.spyOn(dep2, 'run').mockReturnValue(false);

      app = new App([dep1, dep2]);
    });

    test('should return only the deps that started', () => {
      const runningDeps: Array<Dep> = app.start();
      expect(dep1.run).toHaveBeenCalled();
      expect(dep2.run).toHaveBeenCalled();
      // fails because `runningDeps` is empty
      expect(runningDeps).toEqual([dep1]);
    });
  });

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-iucjaa?file=test/suite.test.ts

Note that the same test in Jest does succeed

System Info

System:
    OS: macOS 12.1
    CPU: (10) arm64 Apple M1 Max
    Memory: 12.57 GB / 64.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.13.2 - ~/.volta/tools/image/node/16.13.2/bin/node
    Yarn: 1.22.17 - ~/.volta/tools/image/yarn/1.22.17/bin/yarn
    npm: 8.3.2 - ~/.volta/tools/image/npm/8.3.2/bin/npm
  Browsers:
    Chrome: 97.0.4692.99
    Firefox: 96.0.2
    Safari: 15.2
  npmPackages:
    @vitejs/plugin-vue: 2.1.0 => 2.1.0
    vite: 2.7.13 => 2.7.13
    vitest: 0.2.4 => 0.2.4

Used Package Manager

yarn

Validations

chaii3 pushed a commit to chaii3/vitest that referenced this issue May 13, 2022
Co-authored-by: aaronz <aaron.bjym1011@outlook.com>
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
@github-actions github-actions bot locked and limited conversation to collaborators Jun 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant