When running a Jest test, a variable outside the function is undefined

Viewed 217

This is my dev code below.

import { proxyManager } from '../../proxy'
const proxy = proxyManager.get() 
// proxy is expected to be an object like this:
// { post: () => {}, get: () => {} }

export const updateNamePrefixToAll = ({ origPrefix, prefix, primDBId }) => {
  return proxy?.post(
    `/model/tables/physicalTables/tablePrefixes/batch?primaryDataSourceId=${primDBId}&oldTablePrefix=${encodeURL(
      origPrefix
    )}&newTablePrefix=${encodeURL(prefix)}`
  )
}

The test code is shown below

import {
  updateNamePrefixToAll,
} from '@tableList/service'
describe('service.ts', () => {
   it('', () => {
      updateNamePrefixToAll({
         origPrefix: 'origPrefix1',
         prefix: 'prefix1',
         primDBId: 'primDBId1',
       })
   })   
})

After running the test, there is an error.

ReferenceError: proxy is not defined

I have solved this error by mocking the module below

import { proxyManager } from '../../proxy'

Beside this, how can I solve this issue?

0 Answers
Related