Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

feat: use esModuleInterop when defined (fixes #179) #61

Merged
merged 1 commit into from
Nov 7, 2018
Merged
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
2 changes: 2 additions & 0 deletions src/ts-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface TSConfig {
rootDirs?: string[]
outDir?: string
target?: string
esModuleInterop?: boolean;
}
}

Expand All @@ -43,6 +44,7 @@ function registerTSNode(root: string) {
// cache: false,
// typeCheck: true,
compilerOptions: {
esModuleInterop: tsconfig.compilerOptions.esModuleInterop,
target: tsconfig.compilerOptions.target || 'es2017',
module: 'commonjs',
sourceMap: true,
Expand Down
70 changes: 70 additions & 0 deletions test/ts-node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as path from 'path'
import * as tsNode from 'ts-node'

import {TSConfig} from '../src/ts-node'
import * as util from '../src/util'

import {expect, fancy} from './test'

const root = path.resolve(__dirname, 'fixtures/typescript')
const orig = 'src/hooks/init.ts'
let tsNodeRegisterCallArguments: any[] = []

/**
* Delete a module from the require cache before requiring it.
*/
export default function freshRequire(name: string) {
delete require.cache[require.resolve(name)]
return require(name)
}

const DEFAULT_TS_CONFIG: TSConfig = {
compilerOptions: {}
}

const withMockTsConfig = (config: TSConfig = DEFAULT_TS_CONFIG) =>
fancy
.stub(tsNode, 'register', (arg: any) => {
tsNodeRegisterCallArguments.push(arg)
})
.stub(util, 'loadJSONSync', (arg: string) => {
if (arg.endsWith('tsconfig.json')) {
return config
}
})
.finally(() => {
tsNodeRegisterCallArguments = []
})

describe('tsPath', () => {
withMockTsConfig()
.it('should resolve a .ts file', () => {
const {tsPath} = freshRequire('../src/ts-node')
const result = tsPath(root, orig)
expect(result).to.equal(path.join(root, orig))
})

withMockTsConfig()
.it('should leave esModuleInterop undefined by default', () => {
const {tsPath} = freshRequire('../src/ts-node')
tsPath(root, orig)
expect(tsNodeRegisterCallArguments.length).is.equal(1)
expect(tsNodeRegisterCallArguments[0])
.to.have.nested.property('compilerOptions.esModuleInterop')
.equal(undefined)
})

withMockTsConfig({
compilerOptions: {
esModuleInterop: true
}
})
.it('should use the provided esModuleInterop option', () => {
const {tsPath} = freshRequire('../src/ts-node')
tsPath(root, orig)
expect(tsNodeRegisterCallArguments.length).is.equal(1)
expect(tsNodeRegisterCallArguments[0])
.to.have.nested.property('compilerOptions.esModuleInterop')
.equal(true)
})
})