This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |