Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

集成jest,报配置文件错误,找不到@tarojs/component #15502

Open
zmm2tysu opened this issue Apr 12, 2024 · 0 comments
Open

集成jest,报配置文件错误,找不到@tarojs/component #15502

zmm2tysu opened this issue Apr 12, 2024 · 0 comments
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@zmm2tysu
Copy link

相关平台

微信小程序

小程序基础库: 3.3.6
使用框架: React

复现步骤

npm run test后

Test suite failed to run

    Configuration error:

    Could not locate module @tarojs/components mapped as:
    @tarojs/components/dist-h5/react.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/@tarojs\/components/": "@tarojs/components/dist-h5/react"
      },
      "resolver": undefined
    }

期望结果

运行test pass

实际结果

Test suite failed to run

    Configuration error:

    Could not locate module @tarojs/components mapped as:
    @tarojs/components/dist-h5/react.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/@tarojs\/components/": "@tarojs/components/dist-h5/react"
      },
      "resolver": undefined
    }

环境信息

Taro CLI 3.6.25 environment info:
    System:
      OS: Windows 10 10.0.19045
    Binaries:
      Node: 16.14.2 - D:\Programs\nodejs\node.EXE
      Yarn: 1.22.21 - D:\Programs\nvm\node_global\node_modules\yarn\bin\yarn.CMD
      npm: 8.5.0 - D:\Programs\nodejs\npm.CMD
    npmPackages:
      @tarojs/components: 3.6.25 => 3.6.25
      @tarojs/helper: 3.6.25 => 3.6.25
      @tarojs/plugin-framework-react: 3.6.25 => 3.6.25
      @tarojs/plugin-html: 3.6.25 => 3.6.25
      @tarojs/plugin-platform-weapp: 3.6.25 => 3.6.25
      @tarojs/react: 3.6.25 => 3.6.25
      @tarojs/runtime: 3.6.25 => 3.6.25
      @tarojs/shared: 3.6.25 => 3.6.25
      @tarojs/taro: 3.6.25 => 3.6.25
      @tarojs/webpack5-runner: 3.6.25 => 3.6.25
      babel-preset-taro: 3.6.25 => 3.6.25
      eslint-config-taro: 3.6.25 => 3.6.25
      react: ^18.0.0 => 18.2.0

补充信息

jest.config.js

module.exports = {
  verbose: true,
  globals: {
    // ...
    window: true,
    ENABLE_INNER_HTML: true,
    ENABLE_ADJACENT_HTML: true,
    ENABLE_SIZE_APIS: true,
    ENABLE_TEMPLATE_CONTENT: true,
    ENABLE_CLONE_NODE: true,
    ENABLE_CONTAINS: true,
    ENABLE_MUTATION_OBSERVER: true,
  },
  moduleNameMapper: {
    '@tarojs/components': '@tarojs/components/dist-h5/react',
    // '^. \\.(css|scss|less)$': '<rootDir>/style-mock.js',
    // '@tarojs/taro': '@tarojs/taro-h5',
    // "@/config": '<rootDir>/src/config.ts',
    // "@/storage": '<rootDir>/src/storage/index.ts',
    // "^@/store(.*)$": '<rootDir>/src/store$1',
  },
  transform: {
    '^. \\.(js|jsx|ts|tsx)$': '<rootDir>/node_modules/babel-jest',
    '^. \\.esm.js?$': 'ts-jest',
  },
  rootDir: __dirname,
  setupFiles: ['<rootDir>/test/setup'],
  moduleFileExtensions: ['js', 'jsx', 'ts', 'tsx', 'json'],
  transformIgnorePatterns: ['<rootDir>/node_modules/(?!@taro)', '^. \\.(css|sass|scss|less)$'],
};

test.tsx

import { View, Button } from '@tarojs/components';

export default () => {
  return (
    <View>
      <Button>测试按钮点击</Button>
    </View>
  );
};

index.test.js

import '@testing-library/jest-dom'
import { render } from '@testing-library/react'
import TestPage from '../src/pages/test'

describe('测试', () => {
  it('测试', () => {
    const wrapper = render(<TestPage />);
    const btn = wrapper.find('Button');
    
    expect(btn).not.toBeNull();
  });
});
@taro-bot2 taro-bot2 bot added F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-react Framework - React T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
Status: Todo
Development

No branches or pull requests

1 participant