This file contains hidden or 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 { jest } from "@jest/globals" | |
let mockedDependencyValue | |
jest.unstable_mockModule("./dependency.js", () => ({ | |
dependency: () => mockedDependencyValue | |
})); | |
describe("functionToTest", () => { | |
afterEach(() => { |
This file contains hidden or 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 { jest } from "@jest/globals" | |
import * as Dependency from "./dependency.js" | |
import { functionToTest } from "./functionToTest.js" | |
describe("functionToTest", () => { | |
afterEach(() => { | |
jest.restoreAllMocks() | |
}) | |
it("should return the correct value 1", () => { |
This file contains hidden or 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
export const dependency = () => "the real dependency" |
This file contains hidden or 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 { jest } from '@jest/globals'; | |
import * as FetchUtils from "../fetchMessage.js"; | |
import { handleFetchedMessage } from "../handleFetchedMessage.js"; | |
describe("handleFetchedMessage", () => { | |
beforeEach(() => { | |
// 1 | |
jest.useFakeTimers() | |
}) |
This file contains hidden or 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
export const fetchMessage = () => { | |
const messages = ["Message 1", "Message 2", "Message 3"] | |
const randomIndex = Math.floor(Math.random() * messages.length); | |
return new Promise(resolve => { | |
setTimeout(() => { | |
resolve(messages[randomIndex]) | |
}, 2000) | |
}) | |
} |
This file contains hidden or 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
const abortableSearchItems = ( | |
searchString: string, | |
signal: AbortSignal | |
): Promise<Item[]> => { | |
if (signal.aborted) { | |
return Promise.reject(new Error("searchAborted")); | |
} | |
return new Promise((resolve, reject) => { | |
const abortSearchHandler = () => { |
This file contains hidden or 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
// This is a redux action creator | |
const searchItems = | |
(searchString: string, signal: AbortSignal) => async (dispatch) => { | |
dispatch(searchStarted()); | |
try { | |
const items = await abortableSearchItems(searchString, signal); | |
dispatch(searchSucceeded(items)); | |
} catch (error) { | |
if ((error as any).message === 'searchAborted') { |
This file contains hidden or 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
const abortSearchHandler = () => { | |
throw new Error('searchAborted'); | |
}; | |
const abortableSearchItems = ( | |
searchString: string, | |
signal: AbortSignal | |
): Promise<Item[]> => { | |
if (signal.aborted) { | |
throw new Error('searchAborted'); |
This file contains hidden or 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
const mockedRequest = jest.fn() | |
const callback1 = mockedRequest.mock.calls[0][1] | |
const callback2 = mockedRequest.mock.calls[1][1] | |
const callback3 = mockedRequest.mock.calls[2][1] | |
it('should return and send error if count is 3', () => { | |
const mockedSend = jest.fn() | |
const mockedRes = { | |
send: mockedSend, |
This file contains hidden or 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
name: Dev Deploy | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
name: Deploy |
NewerOlder