Skip to content

Instantly share code, notes, and snippets.

View spencerfeng's full-sized avatar

Spencer Feng spencerfeng

  • Sydney, Australia
View GitHub Profile
@spencerfeng
spencerfeng / functionToTest_correct.spec.js
Created December 13, 2023 11:32
For tutorial: Mastering the Art of Mocking ES Modules in Jest
import { jest } from "@jest/globals"
let mockedDependencyValue
jest.unstable_mockModule("./dependency.js", () => ({
dependency: () => mockedDependencyValue
}));
describe("functionToTest", () => {
afterEach(() => {
@spencerfeng
spencerfeng / functionToTest_wrong.spec.js
Created December 13, 2023 11:17
For tutorial: Mastering the Art of Mocking ES Modules in Jest
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", () => {
@spencerfeng
spencerfeng / dependency.js
Created December 13, 2023 11:05
For tutorial: Mastering the Art of Mocking ES Modules in Jest
export const dependency = () => "the real dependency"
@spencerfeng
spencerfeng / handleFetchedMessage.spec.js
Created July 1, 2023 03:48
For tutorial: Testing JavaScript Code with Timeouts and Promises
import { jest } from '@jest/globals';
import * as FetchUtils from "../fetchMessage.js";
import { handleFetchedMessage } from "../handleFetchedMessage.js";
describe("handleFetchedMessage", () => {
beforeEach(() => {
// 1
jest.useFakeTimers()
})
@spencerfeng
spencerfeng / fetchMessage.js
Created July 1, 2023 03:30
For tutorial: Testing JavaScript Code with Timeouts and Promises
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)
})
}
@spencerfeng
spencerfeng / abortableSearchItems_after.ts
Created July 25, 2022 12:16
For tutorial: Create an abortable API using AbortController and AbortSignal
const abortableSearchItems = (
searchString: string,
signal: AbortSignal
): Promise<Item[]> => {
if (signal.aborted) {
return Promise.reject(new Error("searchAborted"));
}
return new Promise((resolve, reject) => {
const abortSearchHandler = () => {
@spencerfeng
spencerfeng / searchItems.ts
Last active July 25, 2022 12:17
For tutorial: Create an abortable API using AbortController and AbortSignal
// 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') {
@spencerfeng
spencerfeng / abortableSearchItems_before.ts
Last active July 25, 2022 11:49
For tutorial: Create an abortable API using AbortController and AbortSignal
const abortSearchHandler = () => {
throw new Error('searchAborted');
};
const abortableSearchItems = (
searchString: string,
signal: AbortSignal
): Promise<Item[]> => {
if (signal.aborted) {
throw new Error('searchAborted');
@spencerfeng
spencerfeng / sudo_test_for_edward.js
Created June 18, 2022 22:04
Sudo Test For Edward
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,
@spencerfeng
spencerfeng / dev-deploy.yml
Created April 30, 2022 11:59
For tutorial: Create a serverless service to extract the HTML of a web page and store it in an S3 bucket using Serverless Framework
name: Dev Deploy
on:
push:
branches:
- main
jobs:
deploy:
name: Deploy