15

I am trying to migrate from jest to vitest. at some point I get this error : Syntax Error: Invalid or unexpected token

It seems one of the packages we have in project has this line that causes the issue:

require("./lib/somefont.woff")

I checked jest and jest has this line which resolved the issue:

"moduleNameMapper": {
      "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
      "\\.(css|less|scss|sass)$": "identity-obj-proxy"
    },

I was wondering how I can resolve the issue in vitest? is there an equivalent of moduleNameMapper in vitest?

meisam
  • 465
  • 6
  • 18

1 Answers1

1

Use import to import the font

import "./lib/somefont.woff"

Try setting alias config in vitest.config.js

import { defineConfig } from 'vite'
export default defineConfig({
  test: {
    globals: true,
    environment: "jsdom",
    alias: {
      ".*\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
    },
  },
});
Arjun
  • 1,181
  • 1
  • 1
  • 7