jest.mock('./targetJSFile') is not importing the right functions

Viewed 16

I'm trying to understand why Jest is not allowing me to import functions from _mocks_/http.js.

This is my catchAnalysis.js file

import { fetchNewsData } from './http'

async function catchAnalysis(params) {
        console.log("::: Form Submitted :::")
        fetchNewsData(params)
            .then(response => /* Blablabla */)  
            .catch(error => console.log('error', error));
    }

This works great. Below is my catchAnalysis.test.js file:

jest.mock('./http')
import { fetchNewsData } from './http'


test("Mock API works", () =>{
    let testing = fetchNewsData()
    console.log(`fetchNewsData value is ${testing}`)
    /* Blablabla */     
})

fetchNewsData() is undefined when I run JEST tests with npm.

Am I using jest.mock('') incorrectly? I don't know where to start to debug.

For context, this is the _mocks_/http.js file. The real file is a simple fetch API call.

function fetchNewsData(){
    console.log("::: MOCK API :::")
    let responseData = {
        "agreement": "DISAGREEMENT",
        "confidence": "86",
        "irony": "NONIRONIC",
        "model": "general_en",
        "score_tag": "P",
        "status": {
            "code": "0",
            "msg": "OK",
            "credits": "3",
            "remaining_credits": "19849"
        },
        "subjectivity": "SUBJECTIVE"
    }
    console.log(`response Text is ${response.text()}`)
    return Promise.resolve(response)
}

export {
    fetchNewsData
}

Thank you!

1 Answers

I found the answer...

Misread document -> the mock folder has to be in the same directory as the file being mocked. I had put it in src.

I didn't expect folder location to be the issue!

Related