Skip to content

Commit

Permalink
Add typescript support to tests with ts-jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Herover committed Jun 9, 2020
1 parent 8810c9e commit 9625417
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 13 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ module.exports = {
// notifyMode: "failure-change",

// A preset that is used as a base for Jest's configuration
// preset: undefined,
preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,
Expand Down
157 changes: 157 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build": "tsc",
"tsc": "tsc",
"jest": "jest",
"lint": "eslint 'lib/*/**/*.{js,ts}'",
"lint": "eslint '{lib,test}/*/**/*.{js,ts}'",
"test": "jest"
},
"keywords": [
Expand All @@ -34,6 +34,7 @@
},
"devDependencies": {
"@types/bignum": "0.0.29",
"@types/jest": "^25.2.3",
"@types/node": "^14.0.6",
"@types/xmldoc": "^1.1.4",
"@typescript-eslint/eslint-plugin": "^3.1.0",
Expand All @@ -45,6 +46,7 @@
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.20.0",
"jest": "^26.0.1",
"ts-jest": "^26.1.0",
"typescript": "^3.9.3"
}
}
22 changes: 11 additions & 11 deletions tests/asyncTimer.test.js → tests/asyncTimer.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* eslint-disable */
const asyncTimer = require('../dist/SteamCondenser/asyncTimer.js');
import * as asyncTimer from '../lib/SteamCondenser/asyncTimer';

test('resolving doWithin should return resolved value', async () => {
const expectedResult = 1;

const val = await asyncTimer.doWithin(new Promise(resolve => resolve(expectedResult)), 1);
const val = await asyncTimer.doWithin(new Promise((resolve) => resolve(expectedResult)), 1);

expect(val).toBe(expectedResult);
});
Expand All @@ -14,25 +13,26 @@ test('rejecting doWithin should throw error', async () => {

let err;
try {
const val = await asyncTimer.doWithin(new Promise((resolve, reject) => reject(new Error(rejectMessage))), 1)
}
catch (e) {
await asyncTimer.doWithin(
new Promise((resolve, reject) => reject(new Error(rejectMessage))),
1,
);
} catch (e) {
err = e;
}

expect(err).not.toBeUndefined();
expect(err.message).toBe(rejectMessage);
});

test('not resolving or rejecting promise should throw error', async () => {
let err;
try {
const val = await asyncTimer.doWithin(new Promise((resolve, reject) => () => {}), 1)
}
catch (e) {
await asyncTimer.doWithin(new Promise(() => () => {}), 1);
} catch (e) {
err = e;
}

expect(err).not.toBeUndefined();
expect(err.message).toBe('Timed out.');
});

0 comments on commit 9625417

Please sign in to comment.