Skip to content

Commit 7bffbed

Browse files
committed
refactor: convert tests to typescript
1 parent 2f76b8a commit 7bffbed

File tree

12 files changed

+165
-99
lines changed

12 files changed

+165
-99
lines changed

.eslintrc.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
11
module.exports = {
2-
extends: ['plugin:prettier/recommended'],
32
env: {
43
node: true,
54
es6: true,
65
},
7-
overrides: [
8-
{
9-
files: ['**/*.ts'],
10-
extends: [
11-
'plugin:prettier/recommended',
12-
'plugin:@typescript-eslint/eslint-recommended',
13-
'plugin:@typescript-eslint/recommended',
14-
],
15-
globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly' },
16-
parser: '@typescript-eslint/parser',
17-
parserOptions: {
18-
ecmaVersion: 2020,
19-
sourceType: 'module',
20-
project: './tsconfig.json',
21-
},
22-
plugins: ['@typescript-eslint'],
23-
rules: {
24-
'@typescript-eslint/ban-types': 'off',
25-
},
26-
},
6+
extends: [
7+
'plugin:prettier/recommended',
8+
'plugin:@typescript-eslint/eslint-recommended',
9+
'plugin:@typescript-eslint/recommended',
2710
],
11+
globals: { Atomics: 'readonly', SharedArrayBuffer: 'readonly' },
12+
parser: '@typescript-eslint/parser',
13+
plugins: ['@typescript-eslint'],
14+
rules: {
15+
'@typescript-eslint/ban-types': 'off',
16+
},
2817
};

package-lock.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"types"
2020
],
2121
"types": "types",
22-
"dependencies": {},
2322
"devDependencies": {
2423
"@types/node": "^10.17.60",
24+
"@types/tap": "^15.0.7",
2525
"@typescript-eslint/eslint-plugin": "^5.30.7",
2626
"@typescript-eslint/parser": "^5.30.7",
2727
"eslint": "^8.20.0",
@@ -37,12 +37,13 @@
3737
"node": ">= 10.0.0"
3838
},
3939
"scripts": {
40-
"build": "tsc",
40+
"build": "tsc -b tsconfig-build.json",
41+
"types": "tsc",
4142
"test": "npm run lint && npm run test:raw",
42-
"lint": "eslint --ext .js,.ts src/**/* test/**/*",
43+
"lint": "eslint --ext .ts src/**/* test/**/*",
4344
"pretty": "prettier src test --write",
4445
"docs": "typedoc",
45-
"test:raw": "tap test/**/*-test.js"
46+
"test:raw": "tap --ts test/**/*-test.ts"
4647
},
4748
"prettier": {
4849
"singleQuote": true

src/Pool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Deferred } from './Deferred';
22
import { AggregateError } from './AggregateError';
33
import { defaultTo } from './helpers/defaultTo';
44

5-
export type LogLevel = 'verbose' | 'info' | 'error';
5+
export type LogLevel = 'verbose' | 'info' | 'warn' | 'error';
66
export type FactoryLogger = (message: string, level: LogLevel) => void;
77

88
// Various resources
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
const tap = require('tap');
2-
const Pool = require('../..').Pool;
1+
import * as tap from 'tap';
2+
import { Pool } from '../../src';
33

44
tap.test('fail for max < min', (t) => {
55
const factory = {
66
name: 'test-config',
7-
create: () => {},
8-
destroy: () => {},
9-
validate: () => {},
7+
create: async () => Math.random(),
8+
destroy: () => {
9+
//noop
10+
},
11+
validate: () => true,
1012
max: 1,
1113
min: 12,
1214
};
@@ -20,13 +22,16 @@ tap.test('fail for max < min', (t) => {
2022
tap.test('fail without factory.create', (t) => {
2123
const factory = {
2224
name: 'test-config',
23-
destroy: () => {},
24-
validate: () => {},
25+
destroy: () => {
26+
//noop
27+
},
28+
validate: () => true,
2529
max: 1,
2630
min: 0,
2731
};
2832

2933
t.throws(() => {
34+
// @ts-expect-error validate is required
3035
new Pool(factory);
3136
}, 'create function is required');
3237
t.end();
@@ -35,13 +40,14 @@ tap.test('fail without factory.create', (t) => {
3540
tap.test('fail without factory.destroy', (t) => {
3641
const factory = {
3742
name: 'test-config',
38-
create: () => {},
39-
validate: () => {},
43+
create: async () => Math.random(),
44+
validate: () => true,
4045
max: 1,
4146
min: 0,
4247
};
4348

4449
t.throws(() => {
50+
// @ts-expect-error destroy is required
4551
new Pool(factory);
4652
}, 'destroy function is required');
4753
t.end();
@@ -50,13 +56,16 @@ tap.test('fail without factory.destroy', (t) => {
5056
tap.test('fail without factory.validate', (t) => {
5157
const factory = {
5258
name: 'test-config',
53-
create: () => {},
54-
destroy: () => {},
59+
create: async () => Math.random(),
60+
destroy: () => {
61+
//noop
62+
},
5563
max: 1,
5664
min: 0,
5765
};
5866

5967
t.throws(() => {
68+
// @ts-expect-error validate is required
6069
new Pool(factory);
6170
}, 'validate function is required');
6271
t.end();
@@ -65,20 +74,32 @@ tap.test('fail without factory.validate', (t) => {
6574
tap.test('correctly defaults', (t) => {
6675
const factory = {
6776
name: 'test-config-defaults',
68-
create: () => {},
69-
destroy: () => {},
77+
create: async () => Math.random(),
78+
destroy: () => {
79+
//noop
80+
},
7081
validate: () => true,
7182
max: 1,
7283
min: 0,
7384
idleTimeoutMillis: 0,
85+
// @ts-expect-error need to pass undefined
7486
acquireTimeoutMillis: undefined,
87+
// @ts-expect-error need to pass null
7588
reapIntervalMillis: null,
7689
};
7790

7891
const pool = new Pool(factory);
7992

93+
// eslint-disable-next-line
94+
// @ts-ignore
8095
t.equal(pool.idleTimeoutMillis, 0);
96+
97+
// eslint-disable-next-line
98+
// @ts-ignore
8199
t.equal(pool.acquireTimeoutMillis, 30000);
100+
101+
// eslint-disable-next-line
102+
// @ts-ignore
82103
t.equal(pool.reapIntervalMillis, 1000);
83104

84105
t.end();
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const tap = require('tap');
2-
const Pool = require('../..').Pool;
3-
const { delay } = require('../utils');
1+
import * as tap from 'tap';
2+
import { Pool } from '../../src';
3+
import { delay } from '../utils';
44

55
tap.test('factory.create', (t) => {
66
tap.test('handle creation errors', (t) => {
@@ -14,8 +14,10 @@ tap.test('factory.create', (t) => {
1414
return Promise.resolve({ id: created });
1515
}
1616
},
17-
destroy: () => {},
18-
validate: () => {},
17+
destroy: () => {
18+
//noop
19+
},
20+
validate: () => false,
1921
max: 1,
2022
min: 0,
2123
idleTimeoutMillis: 1000,
@@ -51,8 +53,10 @@ tap.test('factory.create', (t) => {
5153
return delay(10).then(() => Promise.resolve({ id: created }));
5254
}
5355
},
54-
destroy: () => {},
55-
validate: () => {},
56+
destroy: () => {
57+
//noop
58+
},
59+
validate: () => false,
5660
max: 1,
5761
min: 0,
5862
idleTimeoutMillis: 1000,
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
const tap = require('tap');
2-
const Pool = require('../..').Pool;
1+
import * as tap from 'tap';
2+
import { Pool } from '../../src';
3+
34
const random = () => Math.floor(Math.random() * 1000);
45

56
tap.test('pool.name', (t) => {
67
const pool = new Pool({
78
name: 'test-pool.name',
89
create: () => Promise.resolve({ id: random() }),
9-
destroy: () => {},
10+
destroy: () => {
11+
//noop
12+
},
1013
validate: () => true,
1114
max: 2,
1215
min: 0,
@@ -20,7 +23,9 @@ tap.test('pool.size', (t) => {
2023
const pool = new Pool({
2124
name: 'test-pool.size',
2225
create: () => Promise.resolve({ id: random() }),
23-
destroy: () => {},
26+
destroy: () => {
27+
//noop
28+
},
2429
validate: () => true,
2530
max: 2,
2631
min: 0,
@@ -55,7 +60,9 @@ tap.test('pool.available', (t) => {
5560
const pool = new Pool({
5661
name: 'test-pool.available',
5762
create: () => Promise.resolve({ id: random() }),
58-
destroy: () => {},
63+
destroy: () => {
64+
//noop
65+
},
5966
validate: () => true,
6067
max: 2,
6168
min: 0,

0 commit comments

Comments
 (0)