setup.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * Jest setup file for cfc-frontend mini-program tests.
  3. * Mocks the global `uni` API and other mini-program runtime globals.
  4. */
  5. // ---- Mock uni global ----
  6. global.uni = {
  7. // Storage mocks
  8. _storage: {},
  9. getStorageSync(key) {
  10. return this._storage[key] !== undefined ? this._storage[key] : ''
  11. },
  12. setStorageSync(key, value) {
  13. this._storage[key] = value
  14. },
  15. removeStorageSync(key) {
  16. delete this._storage[key]
  17. },
  18. // Request mock (override in individual tests)
  19. request: jest.fn(),
  20. // Toast mock
  21. showToast: jest.fn(),
  22. // Navigation mocks
  23. navigateTo: jest.fn(),
  24. redirectTo: jest.fn(),
  25. reLaunch: jest.fn(),
  26. switchTab: jest.fn(),
  27. navigateBack: jest.fn(),
  28. // Account info mock (set in tests as needed)
  29. getAccountInfoSync: jest.fn(() => ({
  30. miniProgram: { envVersion: 'develop' }
  31. })),
  32. // Other common uni APIs
  33. showLoading: jest.fn(),
  34. hideLoading: jest.fn(),
  35. showModal: jest.fn(),
  36. login: jest.fn(),
  37. getUserProfile: jest.fn(),
  38. getUserInfo: jest.fn(),
  39. getSystemInfoSync: jest.fn(() => ({
  40. platform: 'devtools',
  41. language: 'zh_CN',
  42. version: '8.0.0'
  43. }))
  44. }
  45. // ---- Mock Vue ----
  46. jest.mock('vue', () => {
  47. const Vue = jest.requireActual('vue')
  48. Vue.config.productionTip = false
  49. return Vue
  50. })