Cannot find module 'x' or its corresponding type declarations when running Jest Github Actions

119 views Asked by At

When I run my Jest test suite locally, the tests have no problem finding my imported modules and tests complete successfully. However, on Github Actions. The module seems not able to be found, and tests fail due to this.

Does anyone know what is wrong with my config that may be causing this?

Error message:

src/ui/components/Icon.tsx:3:19 - error TS2307: Cannot find module '@/../public/icons/fa-bars-light.svg' or its corresponding type declarations.

3 import Svgs1 from "@/../public/icons/fa-bars-light.svg";

jest.config.js

/** @type {import('ts-jest').JestConfigWithTsJest} */
const { pathsToModuleNameMapper } = require('ts-jest');
const { compilerOptions } = require('./tsconfig');

module.exports = {
  testEnvironment: 'jsdom',
  transform: {
    ".(ts|tsx)": "ts-jest",
    "^.+\.svg$": "jest-transformer-svg" // required to enable svg imports during test runs
  },
  roots: ["<rootDir>"],
  moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
  modulePaths: [compilerOptions.baseUrl],
  moduleDirectories: ['node_modules', 'src', 'public']
};

tsconfig.js (relevant keys)

{
  "compilerOptions": {
     ...,
    "baseUrl": "./",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    }
  },
  "include": [
    .../
    "src/**/*.ts",
    "src/**/*.tsx",
    "public/icons/*.svg"
  ],
  "exclude": [
    "node_modules"
  ]
}

Relevant directory structure

public
   - icons
src
  - ui
    - components
      - Icon.tsx
tsconfig.json
jest.config.js

Github Action Workflow

name: Node.js CI
run-name: "Run Unit Tests"
on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18.17.1'
        cache: 'npm'
    - run: npm i
    - run: npm test

Relevant package.json packages:

{
  ...,
  "jest-transformer-svg": "^2.0.2",
  "jest": "^29.7.0",
  "jest-environment-jsdom": "^29.7.0",
  "ts-jest": "^29.1.2",
}
0

There are 0 answers