React Native Realm 测试

问题描述 投票:0回答:1

如何测试使用 React Native 制作的 Realm 数据库?

我使用 Realm 制作了下表:

const exercises = {
    "name": "table",
    "properties": {
        "id": "int",
        "name": { "type": "string", "maxLength": 50 },
        "type": "string",
        "notes": { "type": "string", "maxLength": 500 },
        "video": "binary",
    },
    "primaryKey": "id",
};
const addRecord() => {}
const editRecord() => {}
const deleteRecord() => {}

我想编写测试以确保功能

addRecord()
editRecord()
deleteRecord()
按预期工作。

目前,我正在使用 Jest 来测试代码。如果可能的话,我想使用 Jest 来测试表函数,尽管我可以使用其他库。

我尝试在

realm.test.js
文件中再次制作表格。但是,我遇到了错误:

SyntaxError: Unexpected token 'export'
> 1 | import Realm from "realm";

我的 package.json 中有以下代码:

    "jest": {
        "preset": "jest-expo",
        "transformIgnorePatterns": [
            "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg)"
        ]
    },

我尝试将

realm
文件夹中的
node_modules
目录添加到
transformIgnorePatterns
数组中。这没有效果

database react-native realm
1个回答
0
投票

@realm
添加到
transformIgnorePatterns
数组的末尾。

新对象应该是:

    "jest": {
        "preset": "jest-expo",
        "transformIgnorePatterns": [
            "node_modules/(?!((jest-)?react-native|@react-native(-community)?)|expo(nent)?|@expo(nent)?/.*|@expo-google-fonts/.*|react-navigation|@react-navigation/.*|@sentry/react-native|native-base|react-native-svg|@realm)"
        ]
    },

像这样运行测试:

describe("exercises table", () => {
const exercises = {
    "name": "table",
    "properties": {
        "id": "int",
        "name": { "type": "string", "maxLength": 50 },
        "type": "string",
        "notes": { "type": "string", "maxLength": 500 },
        "video": "binary",
    },
    "primaryKey": "id",
};
test("should write to the table", () => {
    const realm = new Realm({ "schema": [exercises] });
    realm.write(() => { realm.deleteAll(); });
    realm.write(() => {
        realm.create("exercises", { id: 1, name: "Exercise 1", type: "type 1", notes: "Note 1", "video": "Binary" });
    });
    const exercise = realm.objects("exercises")[0];
    expect(exercise.name).toBe("Exercise 1");
    expect(exercise.type).toBe("Type 1");
    expect(exercise.notes).toBe("Note 1");
    expect(exercise.video).toBe("Binary");
});
});

定义

describe
块内的表格。将所有测试写入同一个
describe

© www.soinside.com 2019 - 2024. All rights reserved.