Skip to content

Commit

Permalink
Add runtime test that TypeScript imports are working (#1473)
Browse files Browse the repository at this point in the history
* Add runtime test that TypeScript imports are working

* Add test of default export, have not deleted it yet!
  • Loading branch information
shadowspawn authored Feb 15, 2021
1 parent 4aaaa9d commit c119028
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
48 changes: 48 additions & 0 deletions package-lock.json

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

14 changes: 11 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
},
"scripts": {
"lint": "eslint index.js esm.mjs \"tests/**/*.js\"",
"typescript-lint": "eslint typings/*.ts",
"typescript-lint": "eslint typings/*.ts tests/*.ts",
"test": "jest && npm run test-typings",
"test-esm": "node --experimental-modules ./tests/esm-test.mjs",
"test-esm": "node --experimental-modules ./tests/esm-imports-test.mjs",
"test-typings": "tsd",
"typescript-checkJS": "tsc --allowJS --checkJS index.js --noEmit",
"test-all": "npm run test && npm run lint && npm run typescript-lint && npm run typescript-checkJS && npm run test-esm"
Expand All @@ -45,12 +45,20 @@
"eslint-plugin-jest": "^24.1.3",
"jest": "^26.6.3",
"standard": "^16.0.3",
"ts-jest": "^26.5.1",
"tsd": "^0.14.0",
"typescript": "^4.1.2"
},
"types": "typings/index.d.ts",
"jest": {
"collectCoverage": true
"testEnvironment": "node",
"collectCoverage": true,
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testPathIgnorePatterns": [
"/node_modules/"
]
},
"engines": {
"node": ">= 10"
Expand Down
3 changes: 2 additions & 1 deletion tests/esm-test.mjs → tests/esm-imports-test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../esm.mjs';

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

function check(condition, explanation) {
if (!condition) {
Expand Down
44 changes: 44 additions & 0 deletions tests/ts-imports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { program, Command, Option, CommanderError, InvalidOptionArgumentError, Help, createCommand } from '../';

import * as commander from '../';

// Do some simple checks that expected imports are available at runtime.
// Similar tests to esm-imports-test.js

// eslint-disable-next-line @typescript-eslint/ban-types
function checkClass(obj: object, name: string) {
expect(typeof obj).toEqual('object');
expect(obj.constructor.name).toEqual(name);
}

test('legacy default export of global Command', () => {
checkClass(commander, 'Command');
});

test('program', () => {
checkClass(program, 'Command');
});

test('createCommand', () => {
checkClass(createCommand(), 'Command');
});

test('Command', () => {
checkClass(new Command('name'), 'Command');
});

test('Option', () => {
checkClass(new Option('-e, --example', 'description'), 'Option');
});

test('CommanderError', () => {
checkClass(new CommanderError(1, 'code', 'failed'), 'CommanderError');
});

test('InvalidOptionArgumentError', () => {
checkClass(new InvalidOptionArgumentError('failed'), 'InvalidOptionArgumentError');
});

test('Help', () => {
checkClass(new Help(), 'Help');
});
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"noImplicitThis": true,
"strictNullChecks": true,
"types": [
"node"
"node",
"jest"
],
"noEmit": true,
"forceConsistentCasingInFileNames": true
Expand Down

0 comments on commit c119028

Please sign in to comment.