0

There is a component that uses a third-party module We need to test it, it is desirable not to make a mock component that is imported into it. Wrote on 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'],
};

Is it possible to test a component without mock others depreciating into that component?

1 Answers1

1

Of course you can. The issue is that clsx is not an ESM module, and therefore, it does not exports a default. As there is no default, you need to enable synthetic default imports in your tsconfig.json.

enjoythelive1
  • 111
  • 1
  • 5