Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image imports that use typescript alias returns undefined #88

Open
ritchieanesco opened this issue Nov 5, 2021 · 1 comment
Open

Image imports that use typescript alias returns undefined #88

ritchieanesco opened this issue Nov 5, 2021 · 1 comment
Labels
question Further information is requested

Comments

@ritchieanesco
Copy link

Given I have the following typescript configuration.

  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/images*": ["./src/images/*"]
    },
    ...
  },

And jest configuration:

{
  rootDir: "src/",
  moduleDirectories: ["node_modules", "src"],
  moduleFileExtensions: ["svelte", "ts", "js"],
  testPathIgnorePatterns: ["/node_modules/", "/dist/"],
  transform: {
    "^.+\\.svelte$": [
      "svelte-jester",
      {
        preprocess: "./svelte.config.js",
      },
    ],
    "^.+\\.js$": "./babel.config.js",
    "^.+\\.ts$": "ts-jest"
  },
  testEnvironment: "jsdom",
  moduleNameMapper: {
      ".*\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/file-mock.js"
    },
}

And I import an image in svelte file

<script lang="ts">
  import airplane from "@/images/airplane.png"
</script>

{#if airplane}
  <img src={airplane} />
{/if}

When I run a test to validate an image exists the test fails because image import returns undefined.
However, If I import the image to a typescript file and export it out as part of an object so it can be consumed in the svelte file, then it works.

Any suggestions what this could be?

@sebastianrothe
Copy link
Collaborator

Do you use Sveltekit/Sapper as well? If so, then you need to add an alias to your sveltekit-config as well.

Am I rright, that all image imports are resolved to file-mock.js?

@sebastianrothe sebastianrothe added the question Further information is requested label Feb 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants