Please provide the following:
- SDK Version: 45
- Platforms(Android/iOS/web/all): Android and iOS
- Add the appropriate “Tag” based on what Expo library you have a question on.
I’m trying to test a hook using @testing-library/react-hooks
and Jest libraries.
But I’m getting this error that I don’t know if it’s about @expo
library or jest-runtime
library:
C:\Users\ana.melo\Documents\wallet-app\node_modules\@expo\config\build\Config.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){
SyntaxError: Invalid or unexpected token
at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1350:14)
at Object.<anonymous> (node_modules/@expo/config/src/index.ts:3:1)
My test.tsx
file:
import { useCep } from "../index";
import { renderHook } from "@testing-library/react-hooks"
import { getCep } from "../../services/api/getCep";
jest.mock("../../services/api/getCep")
const mockCep = {
"cep": "11740-000",
"logradouro": "",
"complemento": "",
"bairro": "",
"localidade": "Itanhaém",
"uf": "SP",
"ibge": "3522109",
"gia": "3694",
"ddd": "13",
"siafi": "6543"
}
describe("hooks/useCep", () => {
it("retornar cep", async () => {
const { result, waitForNextUpdate } = renderHook(() => useCep())
console.log(String(result.current[0]))
await waitForNextUpdate()
console.log(String(result.current[0]))
})
})
File where hook implementation is:
import { AxiosResponse } from "axios";
import { useState, useEffect } from "react";
import { getCep } from "services/api/getCep";
import { CepResponse } from "services/api/global";
export default function useCEP(){
const [cep, setCep] = useState<AxiosResponse<CepResponse> | {}>()
const showCep = async () => {
const cep = await getCep("11740000")
setCep(cep)
}
useEffect(() => {
showCep()
}, [])
return [cep, showCep]
}
File where getCep()
(does get axios
requisition) implementation is:
import { cepAPI } from "services/apiConfig"
import { CepResponse } from "./global"
export async function getCep(cep: string){
try {
const response = await cepAPI.get<CepResponse>(`/${cep}/json`)
return response.data
} catch(error) {
return {}
}
}