refactor: move hoppscotch-common tests to vitest (#3154)

This commit is contained in:
Andrew Bastin
2023-06-22 00:36:25 +05:30
committed by GitHub
parent fc3e3aeaec
commit 6f35574d68
21 changed files with 698 additions and 281 deletions

View File

@@ -1,8 +1,9 @@
import { vi, describe, expect, test } from "vitest"
import axios from "axios"
import axiosStrategy from "../AxiosStrategy"
jest.mock("axios")
jest.mock("~/newstore/settings", () => {
vi.mock("axios")
vi.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {

View File

@@ -1,11 +1,12 @@
import { describe, test, expect, vi } from "vitest"
import axios from "axios"
import axiosStrategy from "../AxiosStrategy"
jest.mock("../../utils/b64", () => ({
vi.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
decodeB64StringToArrayBuffer: vi.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
vi.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
@@ -22,7 +23,7 @@ describe("axiosStrategy", () => {
test("sends POST request to proxy if proxy is enabled", async () => {
let passedURL
jest.spyOn(axios, "post").mockImplementation((url) => {
vi.spyOn(axios, "post").mockImplementation((url) => {
passedURL = url
return Promise.resolve({ data: { success: true, isBinary: false } })
})
@@ -41,7 +42,7 @@ describe("axiosStrategy", () => {
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
vi.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
@@ -54,7 +55,7 @@ describe("axiosStrategy", () => {
test("passes wantsBinary field", async () => {
let passedFields
jest.spyOn(axios, "post").mockImplementation((_url, req) => {
vi.spyOn(axios, "post").mockImplementation((_url, req) => {
passedFields = req
return Promise.resolve({ data: { success: true, isBinary: false } })
})
@@ -65,7 +66,7 @@ describe("axiosStrategy", () => {
})
test("checks for proxy response success field and throws error message for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
vi.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {
@@ -78,7 +79,7 @@ describe("axiosStrategy", () => {
})
test("checks for proxy response success field and throws error 'Proxy Error' for non-success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
vi.spyOn(axios, "post").mockResolvedValue({
data: {
success: false,
data: {},
@@ -89,7 +90,7 @@ describe("axiosStrategy", () => {
})
test("checks for proxy response success and doesn't left for success", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
vi.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
data: {},
@@ -100,7 +101,7 @@ describe("axiosStrategy", () => {
})
test("checks isBinary response field and right with the converted value if so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
vi.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: true,
@@ -114,7 +115,7 @@ describe("axiosStrategy", () => {
})
test("checks isBinary response field and right with the actual value if not so", async () => {
jest.spyOn(axios, "post").mockResolvedValue({
vi.spyOn(axios, "post").mockResolvedValue({
data: {
success: true,
isBinary: false,
@@ -128,15 +129,15 @@ describe("axiosStrategy", () => {
})
test("cancel errors are returned a left with the string 'cancellation'", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(true)
vi.spyOn(axios, "post").mockRejectedValue("errr")
vi.spyOn(axios, "isCancel").mockReturnValueOnce(true)
expect(await axiosStrategy({})()).toEqualLeft("cancellation")
})
test("non-cancellation errors return a left", async () => {
jest.spyOn(axios, "post").mockRejectedValue("errr")
jest.spyOn(axios, "isCancel").mockReturnValueOnce(false)
vi.spyOn(axios, "post").mockRejectedValue("errr")
vi.spyOn(axios, "isCancel").mockReturnValueOnce(false)
expect(await axiosStrategy({})()).toEqualLeft("errr")
})

View File

@@ -1,3 +1,4 @@
import { vi, describe, expect, test, beforeEach } from "vitest"
import extensionStrategy, {
hasExtensionInstalled,
hasChromeExtensionInstalled,
@@ -5,12 +6,12 @@ import extensionStrategy, {
cancelRunningExtensionRequest,
} from "../ExtensionStrategy"
jest.mock("../../utils/b64", () => ({
vi.mock("../../utils/b64", () => ({
__esModule: true,
decodeB64StringToArrayBuffer: jest.fn((data) => `${data}-converted`),
decodeB64StringToArrayBuffer: vi.fn((data) => `${data}-converted`),
}))
jest.mock("~/newstore/settings", () => {
vi.mock("~/newstore/settings", () => {
return {
__esModule: true,
settingsStore: {
@@ -39,32 +40,32 @@ describe("hasExtensionInstalled", () => {
describe("hasChromeExtensionInstalled", () => {
test("returns true if extension is hooked and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
vi.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
vi.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
vi.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not chrome", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
jest.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
vi.spyOn(navigator, "vendor", "get").mockReturnValue("Google")
expect(hasChromeExtensionInstalled()).toEqual(false)
})
@@ -73,35 +74,35 @@ describe("hasChromeExtensionInstalled", () => {
describe("hasFirefoxExtensionInstalled", () => {
test("returns true if extension is hooked and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(true)
})
test("returns false if extension is hooked and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {}
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Firefox")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
test("returns false if extension not installed and browser is not firefox", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = undefined
jest.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
vi.spyOn(navigator, "userAgent", "get").mockReturnValue("Chrome")
expect(hasFirefoxExtensionInstalled()).toEqual(false)
})
})
describe("cancelRunningExtensionRequest", () => {
const cancelFunc = jest.fn()
const cancelFunc = vi.fn()
beforeEach(() => {
cancelFunc.mockClear()
@@ -109,7 +110,7 @@ describe("cancelRunningExtensionRequest", () => {
test("cancels request if extension installed and function present in hook", () => {
global.__POSTWOMAN_EXTENSION_HOOK__ = {
cancelRunningRequest: cancelFunc,
cancelRequest: cancelFunc,
}
cancelRunningExtensionRequest()
@@ -125,7 +126,7 @@ describe("cancelRunningExtensionRequest", () => {
})
describe("extensionStrategy", () => {
const sendReqFunc = jest.fn()
const sendReqFunc = vi.fn()
beforeEach(() => {
sendReqFunc.mockClear()