首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何测试svelte组件

如何测试svelte组件
EN

Stack Overflow用户
提问于 2021-04-28 21:48:00
回答 1查看 106关注 0票数 0

有一个组件,它使用了一个第三方模块,我们需要测试它,这是可取的,不要做一个模拟的组件,是导入到它。在此示例中编写

代码语言:javascript
复制
// test.spec.ts
import Component from "Component";

describe('Component', () => {
    test('should render component correctly', () => {
        const { container } = render(Component);
        expect(container).toBeTruthy();
    });
});

// error: TypeError: cn is not a function
代码语言:javascript
复制
// ChildComponent.svelte

<script type="ts">
  import cn from 'clsx';
  const class = cn('
    'awesome-class': true
  ');
</script>

<div {class} ></div>



// Component.svelte

<script type="ts">
  import ChildComponent from './ChildComponen';
</script>

<ChildComponent />

jest.config.js looks like this

代码语言:javascript
复制
/// jest.config.js
module.exports = {
    displayName: { name: 'web', color: 'magentaBright' },
    preset: 'ts-jest',
    testEnvironment: 'jsdom',
    testRegex: '\\.spec\\.ts?$',
    coverageDirectory: 'src',
    moduleDirectories: ['node_modules', 'src', '<rootDir>'],

    moduleNameMapper: {
        '^src(.*)$': '<rootDir>/src$1',
        clxs: '<rootDir>/node_modules/clxs',
    },
    transform: {
        '^.+\\.svelte$': ['svelte-jester', { preprocess: true }],
        '^.+\\.js$': 'babel-jest'
    },
    moduleFileExtensions: ['js', 'ts', 'svelte'],
    bail: false,
    verbose: true,
    testTimeout: 3000,
    setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
};

是否可以在不模拟其他组件折旧到该组件的情况下测试该组件?

EN

回答 1

Stack Overflow用户

发布于 2021-09-16 12:37:03

你当然可以。问题是clsx不是ESM模块,因此它不会导出默认值。由于没有default,您需要在tsconfig.json中启用synthetic default imports

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67301521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档