【发布时间】:2021-03-17 15:32:01
【问题描述】:
我正在尝试在现有项目中使用 Svelte 和 TypeScript 配置 Jest。
我已经按照 this article 设置了所有内容(使用 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.
我导入了其他不会引发此错误的组件,唯一的区别是 SLink 被导入到常规 .ts 文件中,而我的其他组件被导入到其他组件中(@987654327 除外@)。
如果我将导出更改为空字符串,它会按照应有的方式通过测试(尽管它仍然会引发无法呈现此组件的错误)。
This 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();
});
src/router/components/index.ts:
import SLink from './SLink.svelte';
export { SLink };
SLink 是一个普通的 Svelte 组件,不使用 TypeScript。
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'],
};
babel.config.js:
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }],
'@babel/preset-typescript',
],
};
svelte.config.js:
import sveltePreprocess from 'svelte-preprocess';
module.exports = {
preprocess: sveltePreprocess(),
};
文件树:
src
|_ tests
|_ methods.test.ts
|_ router
|_ components
|_ index.ts
|_ SLink.svelte
【问题讨论】:
标签: typescript jestjs svelte