I am trying to import an JPEG Image in ContactListItem.tsx.
import React from "react";
import { render, screen, RenderResult } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import ContactListItem from "./index";
import Avatar from "src/assets/icons/avatar_1.jpeg";
describe("Testing ContactListItem component", () => {...});
I've got an error
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
But I've already created fileMock for transforming in jest.config.ts
export default {
roots: ["<rootDir>/src"],
moduleFileExtensions: ["ts", "tsx", "js", "json", "node"],
moduleDirectories: ["node_modules", "src"],
moduleNameMapper: {
"src/(.*)": "<rootDir>/src/$1",
"^.+\\.(png|jpg|jpeg|gif|webp|ico|bmp|svg)$/i":
"<rootDir>/src/tests/__mocks__/fileMock.js",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy",
},
testEnvironment: "jsdom",
transform: {
"^.+\\.tsx?$": "ts-jest",
},
transformIgnorePatterns: ["<rootDir>/node_modules/"],
};
file.mock.js
module.exports = `test-file-stub`;
My project hierarchy:
-public
-src
-components
-ContactListItem
-index.tsx
-ContactListItem.test.tsx
-tests
-__mocks__
-file.mock.js
-jest.config.ts