Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
module.exports = {
export default {
roots: ['<rootDir>'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)'
],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest'
collectCoverage: true,
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
coveragePathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/src/types/'
],
transform: {},
testEnvironment: 'jest-environment-node',
// https://kulshekhar.github.io/ts-jest/docs/next/guides/esm-support/
preset: 'ts-jest/presets/default-esm',
extensionsToTreatAsEsm: ['.ts'],
globals: {
'ts-jest': {
useESM: true
}
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1'
}
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"license": "MIT",
"keywords": [],
"main": "dist/index.js",
"exports": "./dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"files": [
"dist"
Expand All @@ -13,11 +15,10 @@
"count": "dist/main.js"
},
"scripts": {
"start": "tsc && node dist/main.js",
"start:watch": "nodemon --watch src --ext ts --exec ts-node src/main.ts",
"build": "tsc",
"test": "jest",
"clean": "rm dist/*",
"start": "npm run build && node dist/main.js",
"build": "npm run clean && tsc && rimraf dist/test && mv dist/src/* dist/ && rimraf dist/src",
"test": "node --experimental-vm-modules node_modules/.bin/jest",
"clean": "rimraf \"dist/*\"",
"upgrade-interactive": "npm-check --update",
"csb:test": "npm test -- --runInBand --watchAll"
},
Expand All @@ -31,6 +32,7 @@
"jest": "^27.3.1",
"nodemon": "^2.0.14",
"npm-check": "^5.9.2",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.7",
"ts-node": "^10.3.0",
"typescript": "^4.4.4"
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Writable } from 'stream'
import countChars from './count'
import countChars from './count.js'

type Opts = {
filenames: string[]
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import countChars from './count'
import countChars from './count.js'
export { countChars }
3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import cli from './cli'

import cli from './cli.js'
;(async () => {
const argv = await yargs(hideBin(process.argv))
.scriptName('count')
Expand Down
13 changes: 7 additions & 6 deletions test/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PassThrough } from 'stream'
import cli from '../src/cli'
import { jest } from '@jest/globals'
import cli from '../src/cli.js'

describe('cli()', () => {
it('should return stdout with exitcode=0', async () => {
Expand All @@ -17,10 +18,10 @@ describe('cli()', () => {
})
).toEqual(0)
expect(outData.mock.calls.length).toEqual(2)
expect(outData.mock.calls[0][0].toString('utf8')).toEqual(
expect((outData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
'test/assets/test1.txt: 15 chars\n'
)
expect(outData.mock.calls[1][0].toString('utf8')).toEqual(
expect((outData.mock.calls[1][0] as Buffer).toString('utf8')).toEqual(
'test/assets/test2.txt: 17 chars\n'
)
expect(errData.mock.calls.length).toEqual(0)
Expand All @@ -40,13 +41,13 @@ describe('cli()', () => {
})
).toEqual(1)
expect(outData.mock.calls.length).toEqual(1)
expect(outData.mock.calls[0][0].toString('utf8')).toEqual(
expect((outData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
'test/assets/test1.txt: 15 chars\n'
)
expect(errData.mock.calls.length).toEqual(2)
expect(errData.mock.calls[0][0].toString('utf8')).toEqual(
expect((errData.mock.calls[0][0] as Buffer).toString('utf8')).toEqual(
"Error: ENOENT: no such file or directory, open 'test/assets/fail.txt'"
)
expect(errData.mock.calls[1][0].toString('utf8')).toEqual('\n')
expect((errData.mock.calls[1][0] as Buffer).toString('utf8')).toEqual('\n')
})
})
2 changes: 1 addition & 1 deletion test/count.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import countChars from '../src/count'
import countChars from '../src/count.js'

describe('countChars()', () => {
it('should return count of content', async () => {
Expand Down
7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "esnext",
"module": "commonjs",
"target": "es2020",
"module": "esnext",
"moduleResolution": "node",
"strict": true,
"skipLibCheck": true,
Expand All @@ -17,6 +17,7 @@
],
"include": [
"src/**/*.ts",
"test/**/*.spec.ts",
"src/**/*.tsx"
]
}
}