-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow generic when creating Piscina (#569)
- Loading branch information
Showing
2 changed files
with
49 additions
and
7 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { resolve } from 'path'; | ||
import Piscina from '..'; | ||
import { test } from 'tap'; | ||
|
||
test('Piscina<T , R> works', async ({ equal }) => { | ||
const worker = new Piscina<string, number>({ | ||
filename: resolve(__dirname, 'fixtures/eval.js') | ||
}); | ||
|
||
const result: number = await worker.run('Promise.resolve(42)'); | ||
equal(result, 42); | ||
}); | ||
|
||
test('Piscina with no generic works', async ({ equal }) => { | ||
const worker = new Piscina({ | ||
filename: resolve(__dirname, 'fixtures/eval.js') | ||
}); | ||
|
||
const result = await worker.run('Promise.resolve("Hello, world!")'); | ||
equal(result, 'Hello, world!'); | ||
}); | ||
|
||
test('Piscina<T, R> typescript complains when invalid Task is supplied as wrong type', async ({ equal }) => { | ||
const worker = new Piscina<string, number>({ | ||
filename: resolve(__dirname, 'fixtures/eval.js') | ||
}); | ||
|
||
// @ts-expect-error complains due to invalid Task being number when expecting string | ||
const result = await worker.run(42); | ||
|
||
equal(result, 42); | ||
}); | ||
|
||
test('Piscina<T, R> typescript complains when assigning Result to wrong type', async ({ equal }) => { | ||
const worker = new Piscina<string, number>({ | ||
filename: resolve(__dirname, 'fixtures/eval.js') | ||
}); | ||
|
||
// @ts-expect-error complains due to expecting a number but being assigned to a string | ||
const result: string = await worker.run('Promise.resolve(42)'); | ||
equal(result, 42); | ||
}); |