How to mock a third party react-native component with jest?

后端 未结 1 1580
长发绾君心
长发绾君心 2020-12-18 12:43

I am using the NumericInput and it works fine when I run the application on my device.

However, when I run jest, I get all kind of errors:



        
1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-18 13:01

    This was a problem with react-native's official jest preprocessor.

    This was my jest config file:

    const { defaults } = require('jest-config');
    
    module.exports = {
        preset: 'react-native',
        transform: {
            '^.+\\.js$': '/node_modules/react-native/jest/preprocessor.js',
            '^.+\\.tsx?$': 'ts-jest'
        },
        moduleFileExtensions: [
            'tsx',
            ...defaults.moduleFileExtensions
        ],
    };
    

    To solve the problem, this is my new jest config file:

    const { defaults } = require('jest-config');
    
    module.exports = {
        preset: 'react-native',
        transform: {
            '^.+\\.tsx?$': 'ts-jest'
        },
        moduleFileExtensions: [
            'tsx',
            ...defaults.moduleFileExtensions
        ],
    };
    

    You do not need the jest preprocessor transform item when using the 'react-native' preset. For more info.

    0 讨论(0)
提交回复
热议问题