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

Commit

Permalink
feat: use esModuleInterop when defined (fixes #179) (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfellner authored and jdx committed Nov 7, 2018
1 parent 8bf2287 commit 5778618
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
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)
})
})

0 comments on commit 5778618

Please sign in to comment.