找不到模块“./SLink.svelte”或其对应的类型声明

Dan*_*hts 2 typescript jestjs svelte

我正在尝试在现有项目中使用 Svelte 和 TypeScript 配置 Jest。

我已经按照本文设置了所有内容(使用 Babel 和ts-jest)。

当我运行测试时,我得到了这个:

FAIL src/tests/methods.test.ts
  ? Test suite failed to run

    src/router/components/index.ts:1:19 - error TS2307: Cannot find module './SLink.svelte' or its corresponding type declarations.

    1 import SLink from './SLink.svelte';
                        ~~~~~~~~~~~~~~~~

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.705 s
Ran all test suites.
npm ERR! Test failed.  See above for more details.
Run Code Online (Sandbox Code Playgroud)

我导入了其他不会引发此错误的组件,唯一的区别SLink是导入到常规.ts文件中,而我的其他组件导入到其他组件中(除了App.svelte)。

如果我将导出更改为空字符串,它会按原样通过测试(尽管如此,它仍会引发无法呈现此组件的错误)。

这个 SO post建议添加@tsconfig/svelte,我已经完成了。

如果我// @ts-ignore在导入上方添加,它会完全正常工作,但是,还有其他方法可以解决这个问题吗?


相关代码:

src/tests/methods.test.ts:

import { render } from '@testing-library/svelte';
import App from '../App.svelte';

test('should render', () => {
    const results = render(App);

    expect(() => results.getByText('Hello world!')).not.toThrow();
});
Run Code Online (Sandbox Code Playgroud)

src/路由器/组件/index.ts:

import SLink from './SLink.svelte';

export { SLink };
Run Code Online (Sandbox Code Playgroud)

SLink是一个使用 TypeScript的普通 Svelte 组件。

jest.config.js:

module.exports = {
    preset: 'ts-jest',
    clearMocks: true,
    coverageDirectory: 'coverage',
    transform: {
        '^.+\\.svelte$': [
            'svelte-jester',
            {
                preprocess: true,
            },
        ],
        '^.+\\.ts$': 'ts-jest',
        '^.+\\.js$': 'babel-jest',
    },
    moduleFileExtensions: ['js', 'ts', 'svelte'],
};
Run Code Online (Sandbox Code Playgroud)

babel.config.js:

module.exports = {
    presets: [
        ['@babel/preset-env', { targets: { node: 'current' } }],
        '@babel/preset-typescript',
    ],
};
Run Code Online (Sandbox Code Playgroud)

svelte.config.js:

import sveltePreprocess from 'svelte-preprocess';

module.exports = {
    preprocess: sveltePreprocess(),
};
Run Code Online (Sandbox Code Playgroud)

文件树:

src
|_ tests
  |_ methods.test.ts
|_ router
  |_ components
    |_ index.ts
    |_ SLink.svelte
Run Code Online (Sandbox Code Playgroud)

小智 8

我看到了类似的错误,多亏了这个对不同问题的回答,我意识到由于 tsconfig.json 文件中的覆盖,我意外地停止了引用 Svelte 类型。

我的 tsconfig 扩展了 Svelte 默认值,但我在compilerOptions下面添加了覆盖:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
  // ...
  "compilerOptions": {
    "types": ["node", "jest"]
  }
}
Run Code Online (Sandbox Code Playgroud)

在构建我的项目时,我收到此错误(类似地,关于在.ts文件中导入 Svelte 组件):

(!) Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)

似乎我不小心覆盖了一些 Svelte 默认值。根据上面链接的建议,我像这样修改了我的 tsconfig (添加"svelte"到 types 数组):

(!) Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)

就我而言,这修复了错误。