Giter VIP home page Giter VIP logo

Comments (4)

sheremet-va avatar sheremet-va commented on July 17, 2024 1

Inconveniently named restoreAllMocks is not related to vi.mock, so it doesn't mention the vi.mock behaviour in the documentation.

So is there no way to have different mocks for different test / suites?

There are several ways you can achieve it. vi.mock is called only once, but you can setup a hoisted spy that can be changed in different tests:

import { envSchema, fetchCdkConfig } from './cdk-config.js'

const getFromSsm = vi.hoisted(() => vi.fn())

vi.mock('./config-client.js', () => {
  const ConfigClient = vi.fn();
  ConfigClient.prototype.getFromSsm = getFromSsm
  return { ConfigClient };
})

test('returns a valid envSchema object', () => {
  getFromSssm.mockReturnValue(generateMock(envSchema))
  const actual = await fetchCdkConfig();
  expect(envSchema.safeParse(actual).success).toBe(true);
})

test('breaks if it gets an invalid envSchema object', async () => {
  getFromSssm.mockReturnValue({ name: 'home', catchphrase: 'doh' })
  const sut = async () => await fetchCdkConfig();
  await expect(sut).rejects.toThrow(); // make sure you await it
})

You can also just spy on methods without mocking the module:

import { envSchema, fetchCdkConfig } from './cdk-config.js'
import { ConfigClient } from './config-client.js'

const getFromSsm = vi.fn()
ConfigClient.prototype.getFromSsm = getFromSsm

// same code for tests

If you really want to mock several times, there is vi.doMock API: https://vitest.dev/api/vi.html#vi-domock
but make sure that you import from ./cdk-config.js inside of every test again as it only mocks the next import:

The next dynamic import of the module will be mocked.

from vitest.

fritz-trawa avatar fritz-trawa commented on July 17, 2024 1

Very helpful, thank you very much for the explanation!

from vitest.

sheremet-va avatar sheremet-va commented on July 17, 2024

vi.mock is hoisted, so your code is only actually called once. Please refer to the documentation: https://vitest.dev/api/vi.html#vi-mock

from vitest.

fritz-trawa avatar fritz-trawa commented on July 17, 2024

I see. I looked at the documentation for restAllMocks where nothing of the sort is mentioned

Will call .mockReset() on all spies. This will clear mock history and reset its implementation to an empty function (will return undefined).

So is there no way to have different mocks for different test / suites?

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.