有一个组件,它使用了一个第三方模块,我们需要测试它,这是可取的,不要做一个模拟的组件,是导入到它。在此示例中编写
// 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// 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
/// 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'],
};是否可以在不模拟其他组件折旧到该组件的情况下测试该组件?
发布于 2021-09-16 12:37:03
你当然可以。问题是clsx不是ESM模块,因此它不会导出默认值。由于没有default,您需要在tsconfig.json中启用synthetic default imports。
https://stackoverflow.com/questions/67301521
复制相似问题