Looking to test a component that utilizes a third-party module without mocking the imported components? Check out this example:
// 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'],
};
Wondering if it's possible to test a component without mocking other components it imports?