fix: prevent ts-node to register twice (#5699)

This commit is contained in:
Kevin Marrec 2019-05-11 18:51:19 +02:00 committed by Pooya Parsa
parent 732be3288e
commit 0c7e500699
2 changed files with 21 additions and 3 deletions

View File

@ -2,7 +2,14 @@ import path from 'path'
import fs from 'fs-extra'
import * as imports from '../imports'
let _guard = false
export const setGuard = (val) => { _guard = val }
async function registerTSNode({ tsConfigPath, options }) {
if (_guard) {
return
}
const { register } = await imports.tsNode()
// https://github.com/TypeStrong/ts-node
@ -13,6 +20,8 @@ async function registerTSNode({ tsConfigPath, options }) {
},
...options
})
_guard = true
}
async function getNuxtTypeScript() {

View File

@ -1,7 +1,7 @@
import { resolve } from 'path'
import { mkdirp, writeFile, remove } from 'fs-extra'
import { register } from 'ts-node'
import { detectTypeScript } from '../../src/utils/typescript'
import { detectTypeScript, setGuard } from '../../src/utils/typescript'
jest.mock('ts-node')
@ -16,8 +16,12 @@ describe('Typescript Support', () => {
await writeFile(tsConfigPath, '{}', 'utf-8')
})
test('detectTypeScript detects and registers runtime', async () => {
beforeEach(() => {
register.mockReset()
setGuard(false)
})
test('detectTypeScript detects and registers runtime', async () => {
await detectTypeScript(rootDir)
expect(register).toHaveBeenCalledTimes(1)
expect(register).toHaveBeenCalledWith({
@ -28,8 +32,13 @@ describe('Typescript Support', () => {
})
})
test('multiple detectTypeScript calls registers runtime only once', async () => {
await detectTypeScript(rootDir)
await detectTypeScript(rootDir)
expect(register).toHaveBeenCalledTimes(1)
})
test('detectTypeScript skips rootDir without tsconfig.json', async () => {
register.mockReset()
await detectTypeScript(rootDir2)
expect(register).toHaveBeenCalledTimes(0)
})