'Jest aliases not working with sveltekit/vite

Here is the error:

$ z front
➜  Catalyze-frontend git:(more-tests) ✗ 
$ npm run test:jest

> [email protected] test:jest
> jest --rootDir ./src

Error: Jest: Failed to parse the TypeScript config file /home/ettinger/src/oblivion/Catalyze-frontend/jest.config.ts
  Error: Must use import to load ES Module: /home/ettinger/src/oblivion/Catalyze-frontend/jest.config.ts
require() of ES modules is not supported.
require() of /home/ettinger/src/oblivion/Catalyze-frontend/jest.config.ts from /home/ettinger/src/oblivion/Catalyze-frontend/node_modules/@jest/core/node_modules/jest-config/build/readConfigFileAndSetRootDir.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/ettinger/src/oblivion/Catalyze-frontend/package.json.

    at readConfigFileAndSetRootDir (/home/ettinger/src/oblivion/Catalyze-frontend/node_modules/@jest/core/node_modules/jest-config/build/readConfigFileAndSetRootDir.js:136:13)
    at async readConfig (/home/ettinger/src/oblivion/Catalyze-frontend/node_modules/@jest/core/node_modules/jest-config/build/index.js:233:18)
    at async readConfigs (/home/ettinger/src/oblivion/Catalyze-frontend/node_modules/@jest/core/node_modules/jest-config/build/index.js:420:26)
    at async runCLI (/home/ettinger/src/oblivion/Catalyze-frontend/node_modules/@jest/core/build/cli/index.js:132:59)
    at async Object.run (/home/ettinger/src/oblivion/Catalyze-frontend/node_modules/jest-cli/build/cli/index.js:155:37)
➜  Catalyze-frontend git:(more-tests) ✗ 

Here is my jest.config.ts:

export default {
    verbose: true,
    testEnvironment: 'jsdom',
    transform: {
        '^.+\\.svelte$': [
            'svelte-jester',
            {
                preprocess: true
            }
        ],
        '^.+\\.js$': 'babel-jest',
        '^.+\\.ts$': 'ts-jest'
    },
    moduleFileExtensions: ['js', 'ts', 'svelte'],
    setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
    moduleNameMapper: {
        '^$components(.*)$': '<root-dir>/src/components$1'
    }
};

I believe package.json has type: "module" which is causing the issue however sveltekit/vite require this. So I don't know how to fix.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source