Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor test-suite to use typescript #652

Merged
merged 7 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"packages/*"
],
"scripts": {
"test": "c8 --reporter=lcov yarn workspaces run test:ci",
"build": "yarn workspace @keyv/test-suite run build",
"test": " yarn build && c8 --reporter=lcov yarn workspaces run test:ci",
"test:services:start": "docker-compose -f ./docker-compose.yaml up -d",
"test:services:stop": "docker-compose -f ./docker-compose.yaml down -v",
"clean": "rm -rf node_modules && rm -rf yarn.lock && yarn workspaces run clean"
Expand Down
29 changes: 24 additions & 5 deletions packages/test-suite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
"name": "@keyv/test-suite",
"version": "1.8.9",
"description": "Test suite for Keyv API compliancy",
"main": "src/index.js",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"type": "commonjs",
"scripts": {
"test": "xo && c8 ava --serial",
"test:ci": "xo && ava --serial",
"build": "tsc",
"prepare": "yarn build",
"test": "yarn build && xo && c8 ava --serial",
"test:ci": "yarn build && xo && ava --serial",
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf dist && rm -rf ./test/testdb.sqlite"
},
"xo": {
"rules": {
"unicorn/prefer-module": 0,
"unicorn/prefer-node-protocol": 0
"unicorn/prefer-node-protocol": 0,
"@typescript-eslint/no-unsafe-assignment": 0,
"@typescript-eslint/no-confusing-void-expression": 0,
"import/extensions": 0
}
},
"repository": {
Expand All @@ -39,13 +45,26 @@
"bignumber.js": "^9.1.1",
"delay": "^5.0.0",
"json-bigint": "^1.0.0",
"sqlite3": "^5.1.4",
"timekeeper": "^2.2.0"
},
"devDependencies": {
"@ava/typescript": "^3.0.1",
"ts-node": "^10.9.1",
"@keyv/compress-brotli": "*",
"@types/json-bigint": "^1.0.1",
"@types/json-buffer": "^3.0.0",
"ava": "^5.1.0",
"c8": "^7.12.0",
"keyv": "*",
"xo": "^0.53.1"
"typescript": "^4.9.5"
},
"ava": {
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const tk = require('timekeeper');
import tk from 'timekeeper';

const keyvApiTests = (test, Keyv, store) => {
import type {TestFn} from 'ava';
import type KeyvModule from 'keyv';
import type {KeyvStoreFn} from './types';

const keyvApiTests = (test: TestFn<any>, Keyv: typeof KeyvModule, store: KeyvStoreFn) => {
test.beforeEach(async () => {
const keyv = new Keyv({store: store()});
await keyv.clear();
Expand Down Expand Up @@ -67,7 +71,7 @@ const keyvApiTests = (test, Keyv, store) => {
await keyv.set('foo', 'bar');
await keyv.set('foo1', 'bar1', 1);
await keyv.set('foo2', 'bar2');
await new Promise(resolve => {
await new Promise<void>(resolve => {
setTimeout(() => {
// Simulate database latency
resolve();
Expand Down Expand Up @@ -179,4 +183,4 @@ const keyvApiTests = (test, Keyv, store) => {
});
};

module.exports = keyvApiTests;
export default keyvApiTests;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Keyv = require('keyv');
import Keyv, {type CompressionAdapter} from 'keyv';

const keyvCompressionTests = (test, compression) => {
import type {TestFn} from 'ava';

const keyvCompressionTests = (test: TestFn, compression: CompressionAdapter) => {
let keyv;
test.beforeEach(async () => {
keyv = new Keyv({
Expand Down Expand Up @@ -52,4 +54,4 @@ const keyvCompressionTests = (test, compression) => {
});
};

module.exports = keyvCompressionTests;
export default keyvCompressionTests;
21 changes: 0 additions & 21 deletions packages/test-suite/src/index.js

This file was deleted.

24 changes: 24 additions & 0 deletions packages/test-suite/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type KeyvModule from 'keyv';
import type {TestFn} from 'ava';
import type {KeyvStoreFn} from './types';

import keyvApiTests from './api';
import keyvValueTests from './values';
import keyvNamepsaceTests from './namespace';

const keyvTestSuite = (test: TestFn, Keyv: typeof KeyvModule, store: KeyvStoreFn) => {
keyvApiTests(test, Keyv, store);
keyvValueTests(test, Keyv, store);
keyvNamepsaceTests(test, Keyv, store);
};

export {
keyvTestSuite as default,
};
export {default as keyvOfficialTests} from './official';
export {default as keyvIteratorTests} from './iterator';
export {default as keyvCompresstionTests} from './compression';

export {default as keyvApiTests} from './api';
export {default as keyvValueTests} from './values';
export {default as keyvNamepsaceTests} from './namespace';
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict';
const delay = require('delay');
import delay from 'delay';

const keyvIteratorTests = (test, Keyv, store) => {
import type {TestFn} from 'ava';
import type KeyvModule from 'keyv';
import type {KeyvStoreFn} from './types';

const keyvIteratorTests = (test: TestFn, Keyv: typeof KeyvModule, store: KeyvStoreFn) => {
test.beforeEach(async () => {
const keyv = new Keyv({store: store()});
await keyv.clear();
Expand All @@ -17,7 +20,7 @@ const keyvIteratorTests = (test, Keyv, store) => {
const map = new Map(
Array.from({length: 5})
.fill(0)
.map((x, i) => [String(i), String(i + 10)]),
.map((_x, i) => [String(i), String(i + 10)]),
);
const toResolve = [];
for (const [key, value] of map) {
Expand All @@ -36,13 +39,13 @@ const keyvIteratorTests = (test, Keyv, store) => {
test.serial(
'iterator() doesn\'t yield values from other namespaces',
async t => {
const KeyvStore = store();
const keyvStore = store();

const keyv1 = new Keyv({store: KeyvStore, namespace: 'keyv1'});
const keyv1 = new Keyv({store: keyvStore, namespace: 'keyv1'});
const map1 = new Map(
Array.from({length: 5})
.fill(0)
.map((x, i) => [String(i), String(i + 10)]),
.map((_x, i) => [String(i), String(i + 10)]),
);
const toResolve = [];
for (const [key, value] of map1) {
Expand All @@ -51,11 +54,11 @@ const keyvIteratorTests = (test, Keyv, store) => {

await Promise.all(toResolve);

const keyv2 = new Keyv({store: KeyvStore, namespace: 'keyv2'});
const keyv2 = new Keyv({store: keyvStore, namespace: 'keyv2'});
const map2 = new Map(
Array.from({length: 5})
.fill(0)
.map((x, i) => [String(i), String(i + 11)]),
.map((_x, i) => [String(i), String(i + 11)]),
);
toResolve.length = 0;
for (const [key, value] of map2) {
Expand All @@ -80,7 +83,7 @@ const keyvIteratorTests = (test, Keyv, store) => {
const map = new Map(
Array.from({length: 5})
.fill(0)
.map((x, i) => [String(i), String(i + 10)]),
.map((_x, i) => [String(i), String(i + 10)]),
);
const toResolve = [];
for (const [key, value] of map) {
Expand All @@ -102,4 +105,4 @@ const keyvIteratorTests = (test, Keyv, store) => {
);
};

module.exports = keyvIteratorTests;
export default keyvIteratorTests;
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const keyvNamepsaceTests = (test, Keyv, store) => {
import type {TestFn} from 'ava';
import type KeyvModule from 'keyv';
import type {KeyvStoreFn} from './types';

const keyvNamepsaceTests = (test: TestFn, Keyv: typeof KeyvModule, store: KeyvStoreFn) => {
test.beforeEach(async () => {
const keyv1 = new Keyv({store: store(), namespace: 'keyv1'});
const keyv2 = new Keyv({store: store(), namespace: 'keyv2'});
Expand Down Expand Up @@ -47,4 +51,4 @@ const keyvNamepsaceTests = (test, Keyv, store) => {
});
};

module.exports = keyvNamepsaceTests;
export default keyvNamepsaceTests;
26 changes: 0 additions & 26 deletions packages/test-suite/src/official.js

This file was deleted.

28 changes: 28 additions & 0 deletions packages/test-suite/src/official.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {promisify} from 'util';
import type {ExecutionContext, TestFn} from 'ava';
import type KeyvModule from 'keyv';

const keyvOfficialTests = (test: TestFn, Keyv: typeof KeyvModule, goodUri: string, badUri: string, options = {}) => { // eslint-disable-line max-params
test.serial('connection string automatically requires storage adapter', async t => {
const keyv = new Keyv(goodUri, options);
await keyv.clear();
t.is(await keyv.get('foo'), undefined);
await keyv.set('foo', 'bar');
t.is(await keyv.get('foo'), 'bar');
await keyv.clear();
});

const withCallback = (fn: (t: ExecutionContext, end: () => void) => void) => async (t: ExecutionContext) => {
await promisify(fn)(t);
};

test.serial('connection errors are emitted', withCallback((t: ExecutionContext, end) => {
const keyv = new Keyv(badUri, options);
keyv.on('error', () => {
t.pass();
end();
});
}));
};

export default keyvOfficialTests;
3 changes: 3 additions & 0 deletions packages/test-suite/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type {Store} from 'keyv';

export type KeyvStoreFn = () => Store<string | undefined>;
Loading