有一个组件使用了第三方模块我们需要对其进行测试,最好不要制作一个导入的模拟组件。写在这个例子上
// 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'],
};
是否可以在不模拟其他人贬值到该组件的情况下测试组件?