Skip to content

Commit c119028

Browse files
authored
Add runtime test that TypeScript imports are working (#1473)
* Add runtime test that TypeScript imports are working * Add test of default export, have not deleted it yet!
1 parent 4aaaa9d commit c119028

File tree

5 files changed

+107
-5
lines changed

5 files changed

+107
-5
lines changed

package-lock.json

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
},
2121
"scripts": {
2222
"lint": "eslint index.js esm.mjs \"tests/**/*.js\"",
23-
"typescript-lint": "eslint typings/*.ts",
23+
"typescript-lint": "eslint typings/*.ts tests/*.ts",
2424
"test": "jest && npm run test-typings",
25-
"test-esm": "node --experimental-modules ./tests/esm-test.mjs",
25+
"test-esm": "node --experimental-modules ./tests/esm-imports-test.mjs",
2626
"test-typings": "tsd",
2727
"typescript-checkJS": "tsc --allowJS --checkJS index.js --noEmit",
2828
"test-all": "npm run test && npm run lint && npm run typescript-lint && npm run typescript-checkJS && npm run test-esm"
@@ -45,12 +45,20 @@
4545
"eslint-plugin-jest": "^24.1.3",
4646
"jest": "^26.6.3",
4747
"standard": "^16.0.3",
48+
"ts-jest": "^26.5.1",
4849
"tsd": "^0.14.0",
4950
"typescript": "^4.1.2"
5051
},
5152
"types": "typings/index.d.ts",
5253
"jest": {
53-
"collectCoverage": true
54+
"testEnvironment": "node",
55+
"collectCoverage": true,
56+
"transform": {
57+
"^.+\\.tsx?$": "ts-jest"
58+
},
59+
"testPathIgnorePatterns": [
60+
"/node_modules/"
61+
]
5462
},
5563
"engines": {
5664
"node": ">= 10"

tests/esm-test.mjs renamed to tests/esm-imports-test.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../esm.mjs';
22

3-
// Do some simple checks that expected imports are available.
3+
// Do some simple checks that expected imports are available at runtime.
44
// Run using `npm run test-esm`.
5+
// Similar tests to test-imports.test.ts
56

67
function check(condition, explanation) {
78
if (!condition) {

tests/ts-imports.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../';
2+
3+
import * as commander from '../';
4+
5+
// Do some simple checks that expected imports are available at runtime.
6+
// Similar tests to esm-imports-test.js
7+
8+
// eslint-disable-next-line @typescript-eslint/ban-types
9+
function checkClass(obj: object, name: string) {
10+
expect(typeof obj).toEqual('object');
11+
expect(obj.constructor.name).toEqual(name);
12+
}
13+
14+
test('legacy default export of global Command', () => {
15+
checkClass(commander, 'Command');
16+
});
17+
18+
test('program', () => {
19+
checkClass(program, 'Command');
20+
});
21+
22+
test('createCommand', () => {
23+
checkClass(createCommand(), 'Command');
24+
});
25+
26+
test('Command', () => {
27+
checkClass(new Command('name'), 'Command');
28+
});
29+
30+
test('Option', () => {
31+
checkClass(new Option('-e, --example', 'description'), 'Option');
32+
});
33+
34+
test('CommanderError', () => {
35+
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError');
36+
});
37+
38+
test('InvalidOptionArgumentError', () => {
39+
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidOptionArgumentError');
40+
});
41+
42+
test('Help', () => {
43+
checkClass(new Help(), 'Help');
44+
});

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"noImplicitThis": true,
99
"strictNullChecks": true,
1010
"types": [
11-
"node"
11+
"node",
12+
"jest"
1213
],
1314
"noEmit": true,
1415
"forceConsistentCasingInFileNames": true

0 commit comments

Comments
 (0)