npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

testdouble-vitest

v0.2.0

Published

Use testdouble.js with vitest for a happier, more productive TDD experience.

Downloads

101

Readme

testdouble-vitest

npm package CI Status

Use testdouble.js with vitest for a happier, more productive TDD experience!

This module ties vitest's import mocking system together with td.imitate to give you a version of td.replaceEsm that works in vitest.

Setup

Install vitest, testdouble, and testdouble-vitest using your package manager of choice...

npm install --save-dev vitest testdouble testdouble-vitest

...then configure vitest to inline the testdouble-vitest dependency...

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    deps: {
      inline: ['testdouble-vitest'],
    },
  },
})

..and you should be ready to go!

Recommended usage

Instead of using td.replaceEsm() and td.reset(), use the replaceEsm() and reset() functions from testdouble-vitest.

From there, follow recommended testdouble.js usage. Use your beforeEach() hook to mock out your test subject's dependencies and import your subject using a dynamic import(), then reset in afterEach(). See example for more details.

import { vi, describe, beforeEach, afterEach, it } from 'vitest'
import { replaceEsm, reset } from 'testdouble-vitest'
import * as td from 'testdouble'

import type * as dependencyModule from '../dependency'
import type * as subjectModule from '../subject'

describe('collaborator subject', () => {
  let dependency: typeof dependencyModule
  let subject: typeof subjectModule

  beforeEach(async () => {
    dependency = await replaceEsm('../dependency')
    subject = await import('../subject')
  })

  afterEach(() => {
    reset()
  })

  it('should replace the dependency with a testdouble imitation', async () => {
    td.when(dependency.load('abc123')).thenResolve({ id: 'abc123' })

    const result = await subject.getReport('abc123')

    expect(result).to.eql({ id: 'abc123' })
  })
})

Like vanilla td.replaceEsm(), this module's replaceEsm() also allows you to specify the replacement explicitly.

dependency = await replaceEsm(
  '../dependency',
  { doSomething: td.func('doSomething') }, // named exports object
  td.func('defaultExport') // default export
)

Usage with vi.mock

If you prefer to use vi.mock() at the top level of your test files, but you would still like to use testdouble.js fakes, you can use this module's imitateEsm() function. Compared to the recommended usage, using vi.mock:

  • Slightly increases the risk of cross-test module state pollution
  • Does not differentiate import statements for mocked modules and real imports

These tradeoffs might be worth it if you or your team is more comfortable with the typical jest.mock() / vi.mock() style.

import { vi, describe, beforeEach, afterEach, it } from 'vitest'
import { imitateEsm, reset } from 'testdouble-vitest'
import * as td from 'testdouble'

import * as dependency from '../dependency'
import * as subject from '../subject'

vi.mock('../dependency', () => imitateEsm('../dependency'))

describe('collaborator subject', () => {
  afterEach(() => {
    reset()
  })

  it('should replace the dependency with a testdouble imitation', async () => {
    td.when(dependency.load('abc123')).thenResolve({ id: 'abc123' })

    const result = await subject.getReport('abc123')

    expect(result).to.eql({ id: 'abc123' })
  })
})