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
Support td.replace for Vitest #484
Comments
I've never heard of vitest before today, but FWIW, it says it includes tinyspy, which says in its readme:
And from a quick look at the vitest docs, it looks like they have a That error doesn't look like what I'd expect to see for a failure for ESM imports given the source of testdouble-jest. @nullpaper Can you share how you're calling the |
I don't know what import * as td from 'test-double' |
I've been using testdouble for a long time and was also recently introduced to import {vi, describe, it, afterEach} from 'vitest'
import * as td from 'testdouble'
import {validateOptions} from '../some-dependency'
import {calculate} from '../some-other-dependency'
import {doTheThing as subject} from '../some-code-under-test'
vi.mock('../some-dependency', () => td.object())
vi.mock('../some-other-dependency', () => td.object())
describe('some test subject', () => {
afterEach(() => {
td.reset()
})
it('should ensure options before creating something', () => {
const inputValue = 1234
const inputOptions = {"hello": "from"}
const validatedOptions = {"the other": "side"}
const expectedResult = 6789
td.when(validateOptions(inputOptions)).thenReturn(validatedOptions)
td.when(calculate(inputValue, validatedOptions)).thenReturn(expectedResult)
const result = subject(inputValue, inputOptions)
expect(result).to.equal(expectedResult)
})
}) |
@mcous yeahhh this gets me 95% of the way, thanks. |
@nullpaper I've continued to use testdouble together with vitest for a few months, and while the I've just published testdouble-vitest to fill that particular gap by creating versions of import { beforeEach, afterEach, describe, it, expect } from 'vitest'
import { replaceEsm, reset } from 'testdouble-vitest'
import * as td from 'testdouble'
describe('getting a report', () => {
let dataLoader: typeof import('./load-report-data')
let reportGenerator: typeof import('./generate-report')
let subject: typeof import('./get-report')
beforeEach(async () => {
dataLoader = await replaceEsm('./load-report-data')
reportGenerator = await replaceEsm('./generate-report')
subject = await import('./get-report')
})
afterEach(() => {
reset()
})
it('should load the data and generate the report', async () => {
const reportData = { id: 'abc123' }
const report = { id: 'abc123', summary: 'hello world' }
td.when(dataLoader.loadReportData('abc123')).thenResolve(reportData)
td.when(reportGenerator.generateReport(reportData)).thenReturn(report)
const result = await subject.getReport('abc123')
expect(result).to.eql(report)
})
}) If you're still using |
This is wonderful, @mcous! If you can take the time to document this in a PR on this file (and anywhere else that mentioning it makes sense) I'd be happy to promote your package! |
How can we get Testdouble
replace
working with Vitest https://vitest.devVitest and td almost work together smoothly.
td.replace
works fine when you're mocking a local object, but it doesn't work on an imported module:As Vitest is a 'drop-in' replacement for Jest, I tried using
testdouble-jest
but of course that doesn't work because Vitest is modules-based and Jest is commonjs so you end up with conflicts onrequireMock
rather thanimportMock
.But even tweaking these makes no difference as the import doesn't seem to get swapped out. I'm guessing this is a modules vs CJS issue somewhere in the deep. I'm hoping someone with more experience can point in the right direction.
Environment
The text was updated successfully, but these errors were encountered: