Mocha在导入到非规范文件中时不会忽略node_modules

时间:2020-03-01 04:17:28

标签: typescript mocha

我正在尝试在我的打字稿项目中运行Mocha测试。

我有一个user.spec.ts文件,其中包含@firebase/testingchai之类的模块,并且我可以毫无问题地运行该文件。

当我包括从本地文件导入时 import {randomTest} from './user';我仍然可以运行测试,一切都很好。

我的tsconfig.json是

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "jsx": "react-native",
    "lib": ["dom", "esnext"],
    "module": "CommonJS",
    "noEmit": true,
    "skipLibCheck": true,
    "resolveJsonModule": true
  }
}

当我在导入的文件中包含node_module的任何内容时,会出现错误Unexpected token {

据我所知,node_modules文件中的.spec.ts被忽略,而.ts文件中的import * as firebase from '@firebase/testing'; import { assert } from 'chai'; import 'mocha'; import * as appConfig from '../../app-dev.json'; import { setUser, getUserRemote } from './user'; // this file breaks when including node_modules const projectId = appConfig.expo.extra.firebase.projectId; firebase.initializeAdminApp({ projectId }); beforeEach(async () => await firebase.clearFirestoreData({ projectId })); describe('testing user', () => { it('should run tests', async () => { await setUser( { email: 'test@email.com', firstName: 'first', lastName: 'last', id: 'test_user_1', accessToken: 'accessToken1', firebaseUid: 'fireBaseUid1', }, 'remote', ); const user = await getUserRemote('test_user_1'); assert(user.email === 'test@email.com'); }); }); 被忽略。

这是一个ts文件示例

.ts

还有失败的import * as Google from 'expo-google-app-auth'; import { FirebaseService } from '../services/firebaseService'; import AsyncStorage from '@react-native-community/async-storage'; const fb = FirebaseService.getInstance(); export interface BlissUser { email: string; firstName: string; lastName: string; id: string; accessToken: string; firebaseUid: string; } type StoreLocation = 'local' | 'remote' | 'both'; export const setUser = async (user: BlissUser, location: StoreLocation) => { switch (location) { case 'local': await setUserLocal(user); break; case 'remote': await setUserRemote(user); case 'both': await setUserLocal(user); await setUserRemote(user); default: await setUserLocal(user); } }; 文件

mocha -r ts-node/register src/**/*.spec.ts

使用-r @babel/register

运行测试

尽管有许多类似的问题,但所有问题的回答都没有奏效。我已经尝试了所有--ignore node_modules tsconfig.modules.commonjs @ECHO OFF setlocal enabledelayedexpansion SET "S=Foobar" SET "num=0" SET "logs=C:\some path" for /R "%logs%" %%F IN (%S%*.txt) DO ( set /a "num+=1" echo !num!. %%~nxF set "file!num!=%%F" ) set "choice=" set /p choice="Select File Number, or no to exit: " if /I "%choice%"=="no" ( goto cleanup ) else ( start "" "!file%choice%!" pause ) :cleanup 等。

0 个答案:

没有答案