From 40e233af7307ba564d9cee0c6bf4766ddfe5868a Mon Sep 17 00:00:00 2001 From: hankei6km Date: Wed, 20 Oct 2021 02:19:04 +0900 Subject: [PATCH] Setup to native ESM --- jest.config.js | 22 +++++++++++++++++++--- package.json | 12 +++++++----- src/cli.ts | 2 +- src/index.ts | 2 +- src/main.ts | 3 +-- test/cli.spec.ts | 13 +++++++------ test/count.spec.ts | 2 +- tsconfig.json | 7 ++++--- 8 files changed, 41 insertions(+), 22 deletions(-) diff --git a/jest.config.js b/jest.config.js index d111c48..6d28d9b 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,10 +1,26 @@ -module.exports = { +export default { roots: [''], testMatch: [ '**/__tests__/**/*.+(ts|tsx|js)', '**/?(*.)+(spec|test).+(ts|tsx|js)' ], - transform: { - '^.+\\.(ts|tsx)$': 'ts-jest' + collectCoverage: true, + collectCoverageFrom: ['/src/**/*.ts'], + coveragePathIgnorePatterns: [ + '/node_modules/', + '/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' } } diff --git a/package.json b/package.json index 9be97c6..59350ce 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "license": "MIT", "keywords": [], "main": "dist/index.js", + "exports": "./dist/index.js", + "type": "module", "types": "dist/index.d.ts", "files": [ "dist" @@ -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" }, @@ -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" diff --git a/src/cli.ts b/src/cli.ts index 0268c27..30240cb 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,5 @@ import { Writable } from 'stream' -import countChars from './count' +import countChars from './count.js' type Opts = { filenames: string[] diff --git a/src/index.ts b/src/index.ts index 3f75c0b..eaa9b26 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -import countChars from './count' +import countChars from './count.js' export { countChars } diff --git a/src/main.ts b/src/main.ts index 31c6984..60d8a87 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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') diff --git a/test/cli.spec.ts b/test/cli.spec.ts index c372916..c36254d 100644 --- a/test/cli.spec.ts +++ b/test/cli.spec.ts @@ -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 () => { @@ -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) @@ -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') }) }) diff --git a/test/count.spec.ts b/test/count.spec.ts index b418eb5..4a1f8d7 100644 --- a/test/count.spec.ts +++ b/test/count.spec.ts @@ -1,4 +1,4 @@ -import countChars from '../src/count' +import countChars from '../src/count.js' describe('countChars()', () => { it('should return count of content', async () => { diff --git a/tsconfig.json b/tsconfig.json index bcab47a..731b732 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "target": "esnext", - "module": "commonjs", + "target": "es2020", + "module": "esnext", "moduleResolution": "node", "strict": true, "skipLibCheck": true, @@ -17,6 +17,7 @@ ], "include": [ "src/**/*.ts", + "test/**/*.spec.ts", "src/**/*.tsx" ] -} +} \ No newline at end of file