Skip to content

Commit d8c03bc

Browse files
committed
add AssertEqual & enable ESM
1 parent da3018d commit d8c03bc

14 files changed

+120
-30
lines changed

.eslintrc.js .eslintrc.cjs

File renamed without changes.

.github/workflows/npm.yml

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,16 @@ jobs:
1111
- ubuntu-latest
1212
# - macos-latest
1313
# - windows-latest
14-
# node: [10, 11, 12, 13, 14, 15, 16]
15-
node: [16]
14+
node-version:
15+
- 16
1616
runs-on: ${{ matrix.os }}
1717
steps:
1818
- uses: actions/checkout@v2
1919
- name: Use Node.js ${{ matrix.node }}
2020
uses: actions/setup-node@v2
2121
with:
2222
node-version: ${{ matrix.node-version }}
23+
cache: npm
2324
cache-dependency-path: package.json
2425

2526
- name: Install Dependencies
@@ -44,6 +45,7 @@ jobs:
4445
- uses: actions/setup-node@v2
4546
with:
4647
node-version: ${{ matrix.node-version }}
48+
cache: npm
4749
cache-dependency-path: package.json
4850

4951
- name: Install Dependencies
@@ -66,6 +68,7 @@ jobs:
6668
with:
6769
node-version: 16
6870
registry-url: https://registry.npmjs.org/
71+
cache: npm
6972
cache-dependency-path: package.json
7073

7174
- name: Install Dependencies

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ This module is highly inspired by [pytest](https://pytest.org/)
6767

6868
1. Upgrade to [tap](https://github.com/tapjs/node-tap) to replace [blue-tape](https://github.com/spion/blue-tape) ([wechayt/wechaty#2223](https://github.com/wechaty/wechaty/pull/2223))
6969
1. Remove `sinon-test`
70+
1. Enable ES Modules
71+
1. Add `AssertEqual` for typing tests
7072

7173
### v0.4 June 07, 2019
7274

package.json

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
{
22
"name": "tstest",
3-
"version": "0.5.16",
3+
"version": "0.7.0",
44
"description": "Testing Utilities for Helping You to Write Better TypeScript Programs",
5-
"main": "dist/src/mod.js",
6-
"typings": "dist/src/mod.d.ts",
5+
"type": "module",
6+
"exports": {
7+
".": {
8+
"import": "./dist/esm/src/mod.js",
9+
"require": "./dist/cjs/src/mod.js"
10+
}
11+
},
12+
"typings": "./dist/esm/src/mod.d.ts",
13+
"engines": {
14+
"node": ">=16",
15+
"npm": ">=7"
16+
},
717
"scripts": {
8-
"build": "tsc",
918
"clean": "shx rm -fr dist/*",
10-
"dist": "npm run clean && npm run build",
11-
"pack": "npm pack",
12-
"lint": "npm run lint:es && npm run lint:ts",
19+
"dist": "npm-run-all clean build dist:commonjs",
20+
"build": "tsc && tsc -p tsconfig.cjs.json",
21+
"dist:commonjs": "jq -n \"{ type: \\\"commonjs\\\" }\" > dist/cjs/package.json",
22+
"lint": "npm-run-all lint:es lint:ts",
1323
"lint:es": "eslint --ignore-pattern tests/fixtures/ '{bin,examples,scripts,src,tests}/**/*.ts'",
14-
"lint:ts": "tsc --noEmit",
24+
"lint:ts": "tsc --isolatedModules --noEmit",
1525
"test:pack": "bash -x scripts/npm-pack-testing.sh",
16-
"test": "tap --node-arg=--require=ts-node/register \"src/**/*.spec.ts\" \"src/*.spec.ts\" \"tests/*.spec.ts\" \"tests/**/*.spec.ts\""
26+
"test": "cross-env NODE_OPTIONS=\"--no-warnings --loader=ts-node/esm\" tap \"src/**/*.spec.ts\" \"tests/**/*.spec.ts\""
1727
},
1828
"repository": {
1929
"type": "git",
@@ -32,10 +42,12 @@
3242
},
3343
"homepage": "https://github.com/huan/tstest#readme",
3444
"devDependencies": {
35-
"@chatie/eslint-config": "^0.12.4",
45+
"@chatie/eslint-config": "^0.14.1",
3646
"@chatie/git-scripts": "^0.6.2",
3747
"@chatie/semver": "^0.4.7",
38-
"@chatie/tsconfig": "^0.16.2",
48+
"@chatie/tsconfig": "^0.20.2",
49+
"cross-env": "^7.0.3",
50+
"npm-run-all": "^4.1.5",
3951
"read-pkg-up": "^7",
4052
"shx": "^0.3.3"
4153
},
@@ -51,6 +63,10 @@
5163
"pre-push": "npx git-scripts-pre-push"
5264
}
5365
},
66+
"files": [
67+
"dist",
68+
"src"
69+
],
5470
"publishConfig": {
5571
"tag": "next"
5672
}

scripts/npm-pack-testing.sh

+35-5
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,59 @@
22
set -e
33

44
npm run dist
5-
npm run pack
5+
npm pack
66

77
TMPDIR="/tmp/npm-pack-testing.$$"
88
mkdir "$TMPDIR"
99
mv *-*.*.*.tgz "$TMPDIR"
1010
cp tests/fixtures/smoke-testing.ts "$TMPDIR"
1111

1212
cd $TMPDIR
13+
1314
npm init -y
14-
npm install --production \
15-
*-*.*.*.tgz \
15+
npm install *-*.*.*.tgz \
1616
@types/node \
1717
is-pr \
18-
typescript
18+
typescript@latest
1919

20+
#
21+
# CommonJS
22+
#
2023
./node_modules/.bin/tsc \
24+
--esModuleInterop \
2125
--lib esnext \
22-
--strict \
26+
--noEmitOnError \
27+
--noImplicitAny \
28+
--skipLibCheck \
29+
--target es5 \
30+
--module CommonJS \
31+
--moduleResolution node \
32+
smoke-testing.ts
33+
34+
echo
35+
echo "CommonJS: pack testing..."
36+
node smoke-testing.js
37+
38+
#
39+
# ES Modules
40+
#
41+
42+
# https://stackoverflow.com/a/59203952/1123955
43+
echo "`jq '.type="module"' package.json`" > package.json
44+
45+
./node_modules/.bin/tsc \
2346
--esModuleInterop \
47+
--lib esnext \
2448
--noEmitOnError \
2549
--noImplicitAny \
2650
--skipLibCheck \
51+
--target es2020 \
52+
--module es2020 \
53+
--moduleResolution node \
2754
smoke-testing.ts
2855

56+
echo
57+
echo "ES Module: pack testing..."
2958
node smoke-testing.js
59+
3060
# (for i in {1..3}; do node smoke-testing.js && break || sleep 1; done)

src/assert-equal.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
2+
3+
import { test } from './mod.js'
4+
5+
import type { AssertEqual } from './assert-equal.js'
6+
7+
test('AssertEqual smoke testing', async (t) => {
8+
type T = string
9+
type EXPECTED_TYPE = string
10+
const typeTest: AssertEqual<T, EXPECTED_TYPE> = true
11+
12+
t.ok(typeTest, 'should pass the typing test')
13+
})

src/assert-equal.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Huan(202109): #18 - `dts` package conflict node_modules/.bin/tsc version
3+
* https://github.com/huan/sidecar/issues/18
4+
*
5+
* Testing static types in TypeScript
6+
* https://2ality.com/2019/07/testing-static-types.html
7+
*/
8+
type AssertEqual<T, Expected> =
9+
T extends Expected
10+
? (Expected extends T ? true : never)
11+
: never
12+
13+
export type {
14+
AssertEqual,
15+
}

src/mod.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import sinon from 'sinon'
22

3-
import { test } from './tap'
4-
import { TsTest } from './tstest'
5-
import { VERSION } from './version'
3+
import { test } from './tap.js'
4+
import { TsTest } from './tstest.js'
5+
import { VERSION } from './version.js'
6+
import type { AssertEqual } from './assert-equal.js'
67

8+
export type {
9+
AssertEqual,
10+
}
711
export {
812
sinon,
913
test,

src/tap.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

3-
import { test } from './tap'
3+
import { test } from './tap.js'
44

55
test('test smoke testing', async t => {
66
t.ok('test is ok')

src/version.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

3-
import { test } from './mod'
3+
import { test } from './mod.js'
44

5-
import { VERSION } from './version'
5+
import { VERSION } from './version.js'
66

77
test('Make sure the VERSION is fresh in source code', async (t) => {
88
t.equal(VERSION, '0.0.0', 'version should be 0.0.0 in source code, only updated before publish to NPM')

tests/fixtures/smoke-testing.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

33
import {
44
test,

tests/integration.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#!/usr/bin/env ts-node
1+
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
22

33
import {
44
test,
55
VERSION,
66
TsTest,
77
// sinon,
8-
} from '../src/mod'
8+
} from '../src/mod.js'
99

1010
test('test', async t => {
1111
t.ok(VERSION, 'ok')

tsconfig.cjs.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"module": "CommonJS",
5+
"outDir": "dist/cjs",
6+
},
7+
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"extends": "@chatie/tsconfig",
33
"compilerOptions": {
4-
"outDir": "dist",
4+
"outDir": "dist/esm",
55
},
66
"exclude": [
77
"node_modules/",

0 commit comments

Comments
 (0)