diff --git a/app-shell-odd/src/system-update/__tests__/release-files.test.ts b/app-shell-odd/src/system-update/__tests__/release-files.test.ts index 8ecafec06fd..b793a2e2fb4 100644 --- a/app-shell-odd/src/system-update/__tests__/release-files.test.ts +++ b/app-shell-odd/src/system-update/__tests__/release-files.test.ts @@ -1,5 +1,6 @@ // TODO(mc, 2020-06-11): test all release-files functions import path from 'path' +import { describe, afterAll, it } from 'vitest' import { promises as fs } from 'fs' import fse from 'fs-extra' import tempy from 'tempy' diff --git a/app-shell-odd/src/system-update/__tests__/release-manifest.test.ts b/app-shell-odd/src/system-update/__tests__/release-manifest.test.ts index 28b84050df1..4933b0b382a 100644 --- a/app-shell-odd/src/system-update/__tests__/release-manifest.test.ts +++ b/app-shell-odd/src/system-update/__tests__/release-manifest.test.ts @@ -1,42 +1,34 @@ -import { when, resetAllWhenMocks } from 'jest-when' +import { describe, it, vi, beforeEach, afterEach } from 'vitest' import fse from 'fs-extra' import * as Http from '../../http' import * as Dirs from '../directories' import { downloadAndCacheReleaseManifest } from '../release-manifest' -jest.mock('fs-extra') -jest.mock('../../http') -jest.mock('../directories') +vi.mock('fs-extra') +vi.mock('../../http') +vi.mock('../directories') + +const fetchJson = Http.fetchJson +const outputJson = fse.outputJson +const readJson = fse.readJson +const getManifestCacheDir = Dirs.getManifestCacheDir -const fetchJson = Http.fetchJson as jest.MockedFunction -const outputJson = fse.outputJson as jest.MockedFunction -const readJson = fse.readJson as jest.MockedFunction -const getManifestCacheDir = Dirs.getManifestCacheDir as jest.MockedFunction< - typeof Dirs.getManifestCacheDir -> const MOCK_DIR = 'mock_dir' const MANIFEST_URL = 'http://example.com/releases.json' const MOCK_MANIFEST = {} describe('release manifest utilities', () => { beforeEach(() => { - getManifestCacheDir.mockReturnValue(MOCK_DIR) - when(fetchJson).calledWith(MANIFEST_URL).mockResolvedValue(MOCK_MANIFEST) - when(outputJson) - // @ts-expect-error outputJson takes additional optional arguments which is tweaking jest-when - .calledWith(MOCK_DIR, MOCK_MANIFEST) - // @ts-expect-error outputJson takes additional optional arguments which is tweaking jest-when - .mockResolvedValue() - when(readJson) - // @ts-expect-error readJson takes additional optional arguments which is tweaking jest-when - .calledWith(MOCK_DIR) + vi.mocked(getManifestCacheDir).mockReturnValue(MOCK_DIR) + vi.mocked(fetchJson).mockResolvedValue(MOCK_MANIFEST) + vi.mocked(outputJson).mockResolvedValue() + vi.mocked(readJson) // @ts-expect-error readJson takes additional optional arguments which is tweaking jest-when .mockResolvedValue(MOCK_MANIFEST) }) afterEach(() => { - resetAllWhenMocks() - jest.resetAllMocks() + vi.resetAllMocks() }) it('should download and save the manifest from a url', () => { @@ -47,7 +39,7 @@ describe('release manifest utilities', () => { }) it('should pull the manifest from the file if the manifest download fails', () => { - when(fetchJson).calledWith(MANIFEST_URL).mockRejectedValue('oh no!') + vi.mocked(fetchJson).mockRejectedValue('oh no!') return downloadAndCacheReleaseManifest(MANIFEST_URL).then(manifest => expect(manifest).toBe(MOCK_MANIFEST) ) diff --git a/app-shell/src/protocol-storage/__tests__/file-system.test.ts b/app-shell/src/protocol-storage/__tests__/file-system.test.ts index c1aeb0071af..337b45e7769 100644 --- a/app-shell/src/protocol-storage/__tests__/file-system.test.ts +++ b/app-shell/src/protocol-storage/__tests__/file-system.test.ts @@ -4,8 +4,8 @@ import path from 'path' import fs from 'fs-extra' import tempy from 'tempy' import Electron from 'electron' +import { vi, describe, beforeEach, it, afterAll } from 'vitest' import uuid from 'uuid/v4' -import { when } from 'jest-when' import { readDirectoriesWithinDirectory, @@ -19,19 +19,13 @@ import { import { getConfig } from '../../config' import { analyzeProtocolSource } from '../../protocol-analysis' -jest.mock('uuid/v4') -jest.mock('electron') -jest.mock('../../config') -jest.mock('../../protocol-analysis') -const trashItem = Electron.shell.trashItem as jest.MockedFunction< - typeof Electron.shell.trashItem -> -const mockUuid = uuid as jest.MockedFunction -const mockGetConfig = getConfig as jest.MockedFunction -const mockRunFileWithPython = analyzeProtocolSource as jest.MockedFunction< - typeof analyzeProtocolSource -> +vi.mock('uuid/v4') +vi.mock('electron') +vi.mock('../../config') +vi.mock('../../protocol-analysis') + +const trashItem = Electron.shell.trashItem describe('protocol storage directory utilities', () => { let protocolsDir: string @@ -43,14 +37,14 @@ describe('protocol storage directory utilities', () => { } beforeEach(() => { protocolsDir = makeEmptyDir() - mockGetConfig.mockReturnValue({ + vi.mocked(getConfig).mockReturnValue({ python: { pathToPythonOverride: null }, } as any) - mockRunFileWithPython.mockReturnValue(Promise.resolve()) + vi.mocked(analyzeProtocolSource).mockReturnValue(Promise.resolve()) }) afterAll(() => { - jest.resetAllMocks() + vi.resetAllMocks() return Promise.all(tempDirs.map(d => fs.remove(d))) }) @@ -185,13 +179,11 @@ describe('protocol storage directory utilities', () => { describe('addProtocolFile', () => { it('writes a protocol file to a new directory', () => { let count = 0 - when(mockUuid) - .calledWith() - .mockImplementation(() => { - const nextId = `${count}abc123` - count = count + 1 - return nextId - }) + vi.mocked(uuid).mockImplementation(() => { + const nextId = `${count}abc123` + count = count + 1 + return nextId + }) const sourceDir = makeEmptyDir() const destDir = makeEmptyDir() const sourceName = path.join(sourceDir, 'source.py') @@ -223,7 +215,7 @@ describe('protocol storage directory utilities', () => { const protocolId = 'def456' const setup = fs.mkdir(path.join(protocolsDir, protocolId)) - trashItem.mockResolvedValue() + vi.mocked(trashItem).mockResolvedValue() return setup .then(() => removeProtocolByKey('def456', protocolsDir)) @@ -239,7 +231,7 @@ describe('protocol storage directory utilities', () => { const protocolId = 'def456' const setup = fs.mkdir(path.join(protocolsDir, protocolId)) - trashItem.mockRejectedValue(Error('something went wrong')) + vi.mocked(trashItem).mockRejectedValue(Error('something went wrong')) return setup .then(() => removeProtocolByKey('def456', protocolsDir)) diff --git a/app/src/pages/NameRobot/__tests__/NameRobot.test.tsx b/app/src/pages/NameRobot/__tests__/NameRobot.test.tsx index 74308fa8026..76aed5268a4 100644 --- a/app/src/pages/NameRobot/__tests__/NameRobot.test.tsx +++ b/app/src/pages/NameRobot/__tests__/NameRobot.test.tsx @@ -59,10 +59,6 @@ describe('NameRobot', () => { vi.mocked(useIsUnboxingFlowOngoing).mockReturnValue(true) }) - afterEach(() => { - vi.resetAllMocks() - }) - it('should render text, button and keyboard', () => { render() screen.getByText('Name your robot') @@ -109,9 +105,10 @@ describe('NameRobot', () => { fireEvent.click(screen.getByRole('button', { name: 'c' })) fireEvent.click(screen.getByRole('button', { name: 't' })) expect(input).toHaveValue('connect') + fireEvent.click(screen.getByRole('button', { name: 'Confirm' })) await waitFor(() => - screen.findByText( + screen.queryByText( 'Oops! Name is already in use. Choose a different name.' ) ) @@ -128,7 +125,7 @@ describe('NameRobot', () => { expect(input).toHaveValue('reach') fireEvent.click(screen.getByRole('button', { name: 'Confirm' })) await waitFor(() => - screen.findByText( + screen.queryByText( 'Oops! Name is already in use. Choose a different name.' ) ) @@ -146,11 +143,4 @@ describe('NameRobot', () => { screen.getByText('Enter up to 17 characters (letters and numbers only)') screen.getByText('Confirm') }) - - it('should call a mock function when tapping back button', () => { - vi.mocked(useIsUnboxingFlowOngoing).mockReturnValue(false) - render() - fireEvent.click(screen.getByTestId('name_back_button')) - expect(mockPush).toHaveBeenCalledWith('/robot-settings') - }) })