Skip to content

Use Django + Faker to generate mock data for MSW without duplicating logic #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
MenasheKoren opened this issue May 18, 2025 · 4 comments
Open
1 task done
Labels
Frontend/JavaScript Testing Third Party Package This idea is suitable for the third party package ecosystem

Comments

@MenasheKoren
Copy link

Code of Conduct

  • I agree to follow Django's Code of Conduct

Feature Description

Allow Django to generate mock data for MSW in the frontend by reusing existing FactoryBoy + Faker factories. The mock data can be exported as a JSON file or JavaScript handler file that MSW can directly use.

Problem

Right now, we're writing mock data twice: once in Django (with factories) and again manually in MSW. This causes duplication and makes it easy for frontend mocks to fall out of sync with backend models.

Request or proposal

proposal

Additional Details

  • The goal is for Django to remain the single source of truth for mock data
  • Could support output as .json or .js (with MSW handlers)
  • Faker could be seeded for consistent results
  • Ideal output lives in or near the Django project, not separately in the frontend

Implementation Suggestions

# Django command (pseudo)
from myapp.factories import PersonFactory
data = PersonFactory.create_batch(10)

# Save to JSON for MSW
with open('frontend/mocks/people.json', 'w') as f:
    json.dump(data, f)
// MSW in React
import people from './mocks/people.json'

export const handlers = [
  rest.get('/api/people', (req, res, ctx) => {
    return res(ctx.status(200), ctx.json(people))
  })
]
Copy link

Thank you MenasheKoren for sharing your idea! We have a lot of them so please be patient. You can see the current queue here.

Community instructions

For commenters, please use the emoji reactions on the issue to express support, and/or concern easily. Please use the comments to ask questions or contribute knowledge about the idea. It is unhelpful to post comments of "I'd love this" or "What's the state of this?"

Reaction Guide

  • 👍 This is something I would use
  • 👎 This is something that would cause problems for me or Django
  • 😕 I’m indifferent to this
  • 🎉 This is an easy win

@LilyAcorn
Copy link
Contributor

What is MSW?

@MenasheKoren
Copy link
Author

What is MSW?

https://mswjs.io/ It's a mock service worker for JavaScript, React, etc.

@LilyAcorn LilyAcorn added the Third Party Package This idea is suitable for the third party package ecosystem label May 18, 2025
@LilyAcorn
Copy link
Contributor

Since Django doesn't have any support for test factories in Python, I think this idea only makes sense as a third party package, so I've added that label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Frontend/JavaScript Testing Third Party Package This idea is suitable for the third party package ecosystem
Projects
Status: Idea
Development

No branches or pull requests

2 participants