https://github.com/kfirfitousi/contentlayer-mock
Generate typed mocks for Contentlayer documents
https://github.com/kfirfitousi/contentlayer-mock
contentlayer markdown mdx mock typescript
Last synced: 3 months ago
JSON representation
Generate typed mocks for Contentlayer documents
- Host: GitHub
- URL: https://github.com/kfirfitousi/contentlayer-mock
- Owner: kfirfitousi
- License: mit
- Created: 2023-01-11T15:10:09.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-15T14:11:35.000Z (over 2 years ago)
- Last Synced: 2025-03-07T15:17:32.996Z (4 months ago)
- Topics: contentlayer, markdown, mdx, mock, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/package/contentlayer-mock
- Size: 80.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# contentlayer-mock
Generate typed mocks for [Contentlayer](https://github.com/contentlayerdev/contentlayer) documents.

## Usage
### Generate a mocked document
`dummy` generates a single mocked document. The argument `properties` must contain all required fields of the document type.
```ts
import { type Post } from "contentlayer/generated";
import { dummy } from "contentlayer-mock";const dummyPost = dummy({
title: "Dummy Post",
description: "This is a dummy post",
});// equivalent to
const dummyPost: Post = {
_id: "dummy",
_raw: {
contentType: "mdx",
flattenedPath: "posts/dummy",
sourceFileDir: "posts",
sourceFileName: "dummy-post.mdx",
sourceFilePath: "posts/dummy-post.mdx",
},
body: {
code: "",
raw: "dummy document raw content",
},
type: "Post",
title: "Dummy Post",
description: "This is a dummy post",
};
````properties` can also be used to overwrite the default properties of the document.
```ts
const dummyPost = dummy({
title: "Dummy Post",
description: "This is a dummy post",
_id: "custom-id",
body: {
raw: "custom raw content",
},
});dummyPost._id; // "custom-id"
dummyPost.body.raw; // "custom raw content"
```### Generate an array of mocked documents
`dummyArray` generates an array of mocked documents. The first argument, `length`, is the number of documents to generate. The second argument, `properties`, must contain all required fields of the document type.
```ts
import { type Post } from "contentlayer/generated";
import { dummyArray } from "contentlayer-mock";const dummyPosts = dummyArray(10, {
title: "Dummy Post",
description: "This is a dummy post",
});
````properties` can also be used to overwrite the default properties of the documents.
```ts
const dummyPosts = dummyArray(10, {
title: "Dummy Post",
description: "This is a dummy post",
_id: "custom-id",
_raw: {
flattenedPath: "posts/custom-id",
},
});
```You can also pass a function to `properties` to generate different values for each document.
```ts
const dummyPosts = dummyArray(10, (index) => ({
title: `Dummy Post ${index}`,
description: `This is a dummy post ${index}`,
_id: `custom-id-${index}`,
}));
```## Limitations
Currently only supports `mdx` documents.