@testing-initializer/data
v0.1.1
Published
Create a mock type-safe database for integration tests.
Downloads
1
Maintainers
Readme
Data Testing Initializer
This package allows you to create a @msw/data type-safe database. It will read nested
arrays as a many of
relationship and objects as a one of
relationship.
Installation
npm i --save-dev @testing-initializer/data
or with Yarn:
yarn add -D @testing-initializer/data
Usage
First make you have your types available:
interface User {
id: number
name: string
}
interface ToDo {
id: number
name: string
}
interface Project {
id: number
name: string
date: string
user: User
toDos: ToDo[]
}
interface APITypes {
user: User
toDo: ToDo
project: Project
}
Proceed to create your mock database based on this types.
import { createDatabase, generateId } from "@testing-initializer/data"
const db = createDatabase<APITypes>({
user: {
id: () => generateId("user-pk"),
name: () => `User ${generateId("user-name")}`,
},
toDo: {
id: () => generateId("toDo-pk"),
name: () => `Todo ${generateId("toDo-name")}`,
},
project: {
id: () => generateId("project-pk"),
date: () => new Date().toISOString(),
name: () => `Project ${generateId("project-name")}`,
user: oneOf("user"),
toDos: manyOf("toDo"),
},
})
Now you can use all database methods available in @msw/data:
db.user.create()
db.user.findFirst({ ... })
db.project.findMany({ ... })
License
This project is licensed under the terms of the MIT license