Skip to content

Commit e8a0897

Browse files
committed
Setup to native ESM
1 parent 303101b commit e8a0897

File tree

8 files changed

+41
-22
lines changed

8 files changed

+41
-22
lines changed

jest.config.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
module.exports = {
1+
export default {
22
roots: ['<rootDir>'],
33
testMatch: [
44
'**/__tests__/**/*.+(ts|tsx|js)',
55
'**/?(*.)+(spec|test).+(ts|tsx|js)'
66
],
7-
transform: {
8-
'^.+\\.(ts|tsx)$': 'ts-jest'
7+
collectCoverage: true,
8+
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
9+
coveragePathIgnorePatterns: [
10+
'<rootDir>/node_modules/',
11+
'<rootDir>/src/types/'
12+
],
13+
transform: {},
14+
testEnvironment: 'jest-environment-node',
15+
// https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/
16+
preset: 'ts-jest/presets/default-esm',
17+
extensionsToTreatAsEsm: ['.ts'],
18+
globals: {
19+
'ts-jest': {
20+
useESM: true
21+
}
22+
},
23+
moduleNameMapper: {
24+
'^(\\.{1,2}/.*)\\.js$': '$1'
925
}
1026
}

package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"license": "MIT",
66
"keywords": [],
77
"main": "dist/index.js",
8+
"exports": "./dist/index.js",
9+
"type": "module",
810
"types": "dist/index.d.ts",
911
"files": [
1012
"dist"
@@ -13,11 +15,10 @@
1315
"count": "dist/main.js"
1416
},
1517
"scripts": {
16-
"start": "tsc && node dist/main.js",
17-
"start:watch": "nodemon --watch src --ext ts --exec ts-node src/main.ts",
18-
"build": "tsc",
19-
"test": "jest",
20-
"clean": "rm dist/*",
18+
"start": "npm run build && node dist/main.js",
19+
"build": "npm run clean && tsc && rimraf dist/test && mv dist/src/* dist/ && rimraf dist/src",
20+
"test": "node --experimental-vm-modules node_modules/.bin/jest",
21+
"clean": "rimraf \"dist/*\"",
2122
"upgrade-interactive": "npm-check --update",
2223
"csb:test": "npm test -- --runInBand --watchAll"
2324
},
@@ -31,6 +32,7 @@
3132
"jest": "^27.3.1",
3233
"nodemon": "^2.0.14",
3334
"npm-check": "^5.9.2",
35+
"rimraf": "^3.0.2",
3436
"ts-jest": "^27.0.7",
3537
"ts-node": "^10.3.0",
3638
"typescript": "^4.4.4"

src/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Writable } from 'stream'
2-
import countChars from './count'
2+
import countChars from './count.js'
33

44
type Opts = {
55
filenames: string[]

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
import countChars from './count'
1+
import countChars from './count.js'
22
export { countChars }

src/main.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env node
22
import yargs from 'yargs'
33
import { hideBin } from 'yargs/helpers'
4-
import cli from './cli'
5-
4+
import cli from './cli.js'
65
;(async () => {
76
const argv = await yargs(hideBin(process.argv))
87
.scriptName('count')

test/cli.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PassThrough } from 'stream'
2-
import cli from '../src/cli'
2+
import { jest } from '@jest/globals'
3+
import cli from '../src/cli.js'
34

45
describe('cli()', () => {
56
it('should return stdout with exitcode=0', async () => {
@@ -17,10 +18,10 @@ describe('cli()', () => {
1718
})
1819
).toEqual(0)
1920
expect(outData.mock.calls.length).toEqual(2)
20-
expect(outData.mock.calls[0][0].toString('utf8')).toEqual(
21+
expect((outData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
2122
'test/assets/test1.txt: 15 chars\n'
2223
)
23-
expect(outData.mock.calls[1][0].toString('utf8')).toEqual(
24+
expect((outData.mock.calls[1][0] as Buffer).toString('utf8')).toEqual(
2425
'test/assets/test2.txt: 17 chars\n'
2526
)
2627
expect(errData.mock.calls.length).toEqual(0)
@@ -40,13 +41,13 @@ describe('cli()', () => {
4041
})
4142
).toEqual(1)
4243
expect(outData.mock.calls.length).toEqual(1)
43-
expect(outData.mock.calls[0][0].toString('utf8')).toEqual(
44+
expect((outData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
4445
'test/assets/test1.txt: 15 chars\n'
4546
)
4647
expect(errData.mock.calls.length).toEqual(2)
47-
expect(errData.mock.calls[0][0].toString('utf8')).toEqual(
48+
expect((errData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
4849
"Error: ENOENT: no such file or directory, open 'test/assets/fail.txt'"
4950
)
50-
expect(errData.mock.calls[1][0].toString('utf8')).toEqual('\n')
51+
expect((errData.mock.calls[1][0] as Buffer).toString('utf8')).toEqual('\n')
5152
})
5253
})

test/count.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import countChars from '../src/count'
1+
import countChars from '../src/count.js'
22

33
describe('countChars()', () => {
44
it('should return count of content', async () => {

tsconfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "esnext",
4-
"module": "commonjs",
3+
"target": "es2020",
4+
"module": "esnext",
55
"moduleResolution": "node",
66
"strict": true,
77
"skipLibCheck": true,
@@ -17,6 +17,7 @@
1717
],
1818
"include": [
1919
"src/**/*.ts",
20+
"test/**/*.spec.ts",
2021
"src/**/*.tsx"
2122
]
22-
}
23+
}

0 commit comments

Comments
 (0)