diff --git a/packages/taro-cli-convertor/__tests__/index.test.ts b/packages/taro-cli-convertor/__tests__/index.test.ts index facdf990ea4..7ed468fe69a 100644 --- a/packages/taro-cli-convertor/__tests__/index.test.ts +++ b/packages/taro-cli-convertor/__tests__/index.test.ts @@ -3,11 +3,6 @@ import * as taroize from '@tarojs/taroize' import Convertor from '../src/index' import { generateMinimalEscapeCode } from './util' -jest.mock('../src/util/index.ts', () => ({ - ...jest.requireActual('../src/util/index.ts'), // 保留原始的其他函数 - printToLogFile: jest.fn(), -})) - jest.mock('fs', () => ({ ...jest.requireActual('fs'), // 保留原始的其他函数 appendFile: jest.fn(), diff --git a/packages/taro-cli-convertor/__tests__/script.test.ts b/packages/taro-cli-convertor/__tests__/script.test.ts index c44512087e7..18c19b81f1a 100644 --- a/packages/taro-cli-convertor/__tests__/script.test.ts +++ b/packages/taro-cli-convertor/__tests__/script.test.ts @@ -7,11 +7,6 @@ import { generateMinimalEscapeCode } from './util' const fs = require('fs') const path = require('path') -jest.mock('../src/util/index.ts', () => ({ - ...jest.requireActual('../src/util/index.ts'), // 保留原始的其他函数 - printToLogFile: jest.fn(), -})) - jest.mock('fs', () => ({ ...jest.requireActual('fs'), // 保留原始的其他函数 appendFile: jest.fn(), diff --git a/packages/taroize/__tests__/event.test.ts b/packages/taroize/__tests__/event.test.ts index 1614a5fcf40..9655a8c68bb 100644 --- a/packages/taroize/__tests__/event.test.ts +++ b/packages/taroize/__tests__/event.test.ts @@ -6,6 +6,11 @@ interface Option { path: string } +jest.mock('fs', () => ({ + ...jest.requireActual('fs'), // 保留原始的其他函数 + appendFile: jest.fn(), +})) + describe('event convertor', () => { const option: Option = { path: '', diff --git a/packages/taroize/__tests__/script.test.ts b/packages/taroize/__tests__/script.test.ts index d31ca3e9d64..cd816344a07 100644 --- a/packages/taroize/__tests__/script.test.ts +++ b/packages/taroize/__tests__/script.test.ts @@ -4,6 +4,10 @@ import { parseScript } from '../src/script' import { parseWXML, WXS } from '../src/wxml' import { generateMinimalEscapeCode } from './util' +jest.mock('fs', () => ({ + ...jest.requireActual('fs'), // 保留原始的其他函数 + appendFile: jest.fn(), +})) interface Option { script: string diff --git a/packages/taroize/__tests__/template.test.ts b/packages/taroize/__tests__/template.test.ts index 6d08d19f001..0adca695380 100644 --- a/packages/taroize/__tests__/template.test.ts +++ b/packages/taroize/__tests__/template.test.ts @@ -1,10 +1,14 @@ import { parseWXML } from '../src/wxml' import { generateMinimalEscapeCode } from './util' -jest.mock('fs') const fs = require('fs') const path = require('path') +jest.mock('fs', () => ({ + ...jest.requireActual('fs'), // 保留原始的其他函数 + appendFile: jest.fn(), +})) + describe('template.ts', () => { describe('import 正常情况', () => { // 还原模拟函数 diff --git a/packages/taroize/__tests__/wxml.test.ts b/packages/taroize/__tests__/wxml.test.ts index dd78b6c47c7..dac1b27864a 100644 --- a/packages/taroize/__tests__/wxml.test.ts +++ b/packages/taroize/__tests__/wxml.test.ts @@ -3,6 +3,11 @@ import * as t from '@babel/types' import { convertStyleUnit, parseContent, parseStyle, parseWXML } from '../src/wxml' import { generateMinimalEscapeCode } from './util' +jest.mock('fs', () => ({ + ...jest.requireActual('fs'), // 保留原始的其他函数 + appendFile: jest.fn(), +})) + interface Option { path: string rootPath: string diff --git a/packages/taroize/src/global.ts b/packages/taroize/src/global.ts index 4c041302dd0..6ae893db1b1 100644 --- a/packages/taroize/src/global.ts +++ b/packages/taroize/src/global.ts @@ -3,13 +3,15 @@ export const usedComponents = new Set() export const errors: string[] = [] export const globals = { + rootPath: '', hasCatchTrue: false, logFilePath: '', } export const THIRD_PARTY_COMPONENTS = new Set() -export const resetGlobals = (logFilePath) => { +export const resetGlobals = (rootPath, logFilePath) => { + globals.rootPath = rootPath globals.hasCatchTrue = false globals.logFilePath = logFilePath THIRD_PARTY_COMPONENTS.clear() diff --git a/packages/taroize/src/index.ts b/packages/taroize/src/index.ts index 12b435171f3..566decd86e8 100644 --- a/packages/taroize/src/index.ts +++ b/packages/taroize/src/index.ts @@ -19,7 +19,7 @@ interface Option { } export function parse (option: Option) { - resetGlobals(option.logFilePath) + resetGlobals(option.rootPath, option.logFilePath) setting.rootPath = option.rootPath if (option.json) { const config = JSON.parse(option.json)