Created
July 23, 2022 21:38
-
-
Save tim-rohrer/a3e731dc368b545c98825b4a049d35ac to your computer and use it in GitHub Desktop.
Broken test file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ParsedData } from "quicken-investment-parser/dist/QuickenInvestmentParser" | |
import { OkImpl } from "ts-results-es" | |
import { getMockReq, getMockRes } from "@jest-mock/express" | |
import { jest } from "@jest/globals" | |
beforeEach(() => { | |
jest.resetAllMocks() | |
}) | |
test("should succeed", async () => { | |
const expectedResponse: ParsedData = [ | |
JSON.stringify({ | |
name: "Tim", | |
}), | |
] | |
const req = getMockReq() | |
const { res, next } = getMockRes() | |
jest.unstable_mockModule("../services/quicken.service.js", () => ({ | |
fetchQuickenInvestmentData: jest.fn().mockImplementation( | |
async () => | |
<OkImpl<typeof expectedResponse>>{ | |
ok: true, | |
err: false, | |
val: expectedResponse, | |
}, | |
), | |
})) | |
const quickenControllerModule = await import( | |
"../controller/quicken.controller.js" | |
) | |
await quickenControllerModule.getData(req, res, next) | |
expect(res.status).toHaveBeenCalledTimes(1) | |
expect(res.status).toHaveBeenCalledWith(200) | |
expect(res.json).toHaveBeenCalledTimes(1) | |
expect(res.json).toHaveBeenCalledWith(expectedResponse) | |
}) | |
test("should handle an error from the parser", async () => { | |
const expectedResponse: Error = new Error("Something went wrong") | |
const req = getMockReq() | |
const { res, next } = getMockRes() | |
jest.unstable_mockModule("../services/quicken.service.js", () => ({ | |
fetchQuickenInvestmentData: jest.fn().mockImplementation(async () => ({ | |
ok: false, | |
err: true, | |
val: expectedResponse, | |
})), | |
})) | |
const quickenControllerModule = await import( | |
"../controller/quicken.controller.js" | |
) | |
await quickenControllerModule.getData(req, res, next) | |
expect(res.status).not.toHaveBeenCalled() | |
expect(next).toHaveBeenCalledTimes(1) | |
expect(next).toHaveBeenCalledWith(expectedResponse) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment