本文介绍了为什么 expo-file-system 在 Jest 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的小项目编写一些测试.我们使用 expo-file-system 来允许我们访问 IOS 内部存储.这是我对保存功能的测试.

I am trying to write some tests for my little project. We used expo-file-system to allow us access IOS internal storage. And here is my test for our saving function.

import SaveData from "../app/resources/SaveData/SaveData"
import * as FileSystem from 'expo-file-system';

var promiseHolder = 0;

describe('
saveNewFile() is tested.
', ()=>{
it("A file is going to be created.",async ()=>{
    await FileSystem.writeAsStringAsync("sometext","test.txt");
    promiseHolder = await FileSystem.getInfoAsync("test.txt");
    testHolder = await FileSystem.readAsStringAsync("test.txt");
    console.log(promiseHolder)
    console.log(testHolder)
  });
});

这是输出.命令行输出

看起来 FileSystem.documentDirectory 在测试中不起作用,但我们的应用运行良好.我该怎么办?

It looks like FileSystem.documentDirectory does not work in test, but our app works well. What should I do?

推荐答案

你没有像 promiseHolder 那样声明 gan 变量.

You didn't declare gan variable like promiseHolder.

 import SaveData from "../app/resources/SaveData/SaveData";
 import * as FileSystem from 'expo-file-system';

 var promiseHolder = 0;
 var testHolder=0;

describe('
saveNewFile() is tested.
', ()=>{
it("A file is going to be created.",async ()=>{
   let fileUri = FileSystem.documentDirectory + "text.txt";
   await FileSystem.writeAsStringAsync(flileUri, "cao");
   promiseHolder = await
   FileSystem.getInfoAsync(fileUri);
   testHolder = await FileSystem.readAsStringAsync(fileUri);
   console.log(promiseHolder)
   console.log(testHolder)
    });
 });

更新:expo-jest 为 FileSystem 等导入制作模型,测试中收到的结果旨在预定义模型预设.

Updates:expo-jest makes mockups for imports like FileSystem and results that were received in test intend to predefined mockup presets.

这篇关于为什么 expo-file-system 在 Jest 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:43