Skip to content

Instantly share code, notes, and snippets.

@nazmulidris
Created January 28, 2022 00:33
Show Gist options
  • Save nazmulidris/05fd9ab0a73043aff6d6e4e117245c15 to your computer and use it in GitHub Desktop.
Save nazmulidris/05fd9ab0a73043aff6d6e4e117245c15 to your computer and use it in GitHub Desktop.
deep dive into jest & promise creation
/*
* Copyright (c) 2022 R3BL LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
test("simple sync function call", () => {
function get(key: string): string {
return key + "_value"
}
expect(get("foo")).toBe("foo_value")
})
type ResolveFn<T> = (value: T) => void
type RejectFn = (reason?: any) => void
type PromiseExecutorFn<T> = (resolveFn: ResolveFn<T>, rejectFn: RejectFn) => void
test("simple promise that resolves", () => {
function getFutureResult(key: string): Promise<string> {
const generateResult: PromiseExecutorFn<string> = (resolve, _reject) => {
resolve(key + "_value")
}
return new Promise<string>(generateResult)
}
const future: Promise<string> = getFutureResult("foo")
expect(future).resolves.toBe("foo_value")
})
test("simple promise that rejects", () => {
function getFutureError(_key: string): Promise<string> {
const generateResult: PromiseExecutorFn<string> = (_resolve, reject) => {
reject(new Error("reason for this error"))
}
return new Promise<string>(generateResult)
}
const future: Promise<string> = getFutureError("foo")
expect(future).rejects.toThrow("reason for this error")
})
test("simple promise that resolves after delay", () => {
function getFutureResult(key: string): Promise<string> {
const generateResultAfterDelay: PromiseExecutorFn<string> = (resolve, _reject) => {
setTimeout(() => {
resolve(key + "_value")
}, 100)
}
return new Promise<string>(generateResultAfterDelay)
}
const future: Promise<string> = getFutureResult("foo")
expect(future).resolves.toBe("foo_value")
})
test("use manual-promise-unwrap syntax to unwrap a result", (done) => {
const getFutureResult = (key: string) => new Promise<string>(
(resolve) => setTimeout(() => { resolve(key + "_value") }, 100)
)
const onOk: (value: string) => void = (value) => {
expect(value).toBe("foo_value")
done()
}
const onError: (error: Error) => void = (error) => expect(error).toBeUndefined()
const result: Promise<string> = getFutureResult("foo")
result
.then(onOk)
.catch(onError)
})
test("use async, await syntax to unwrap a result", async () => {
const getFutureResult = (key: string) => new Promise<string>(
(resolve) => setTimeout(() => { resolve(key + "_value") }, 100)
)
const value: string = await getFutureResult("foo")
expect(value).toBe("foo_value")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment