alibaba / ice

🚀 ice.js: The Progressive App Framework Based On React(基于 React 的渐进式应用框架)
https://ice.work
MIT License
17.84k stars 2.09k forks source link

单元测试引用request后报错 #4793

Open vision-yip opened 3 years ago

vision-yip commented 3 years ago

What is the current behavior? 发生了什么?

写单元测试的时候引用了

import { request } from '../../.ice';

发现堆栈会到iconfont当中. 在1.x版本中不会出现, 2.x后貌似就没按照预计的单元测试进行

image

What is the expected behavior? 期望的结果是什么?

调用了request就仅仅是调用了request, 不要检测其它的

Any additional comments? 相关环境信息?

imsobear commented 3 years ago

package.json 和 build.json 内容提供下,用哪个命令跑的测试任务?

vision-yip commented 3 years ago

build.config.js

import webpackConfig from './build.plugins';

const config = require('./project.config');

const proxys = {};
(config.proxys || []).forEach((e) => {
  proxys[`${e.path}`] = {
    enable: true,
    target: e.target,
    pathRewrite: {
      [`^${e.path}`]: e.rewrite,
    },
  };
});

module.exports = {
  mpa: {
    openPage: config.openPage || 'auth',
  },
  devPublicPath: config.devPublicPath,
  publicPath: config.proPublicPath,
  hash: config.hash,
  alias: {
    '@public': './public',
    '@config': './project.config.js',
  },
  plugins: [
    [
      'build-plugin-moment-locales',
      {
        locales: ['zh-cn'],
      },
    ],
    webpackConfig,
  ],
  sassLoaderOptions: {
    prependData: `
      @import "./src/assets/sass/theme.scss";
    `,
  },
  proxy: proxys,
};

package.json

{
  "name": "banber",
  "version": "3.0.0",
  "description": "banber v3",
  "dependencies": {
    "@testing-library/jest-dom": "^5.12.0",
    "ahooks": "^2.10.4",
    "antd": "^4.13.0",
    "antd-theme-generator": "^1.2.8",
    "i18next": "^19.8.4",
    "jsencrypt": "^3.1.0",
    "jsonwebtoken": "^8.5.1",
    "moment": "^2.24.0",
    "react": "17.0.1",
    "react-dom": "^16.4.1",
    "react-i18next": "^11.8.5",
    "react-relay": "^11.0.0",
    "react-router": "^5.2.0",
    "swiper": "^6.4.15",
    "typescript": "^4.1.2"
  },
  "devDependencies": {
    "@babel/core": "^7.15.8",
    "@babel/preset-env": "^7.15.8",
    "@babel/preset-react": "^7.14.5",
    "@babel/preset-typescript": "^7.15.0",
    "@iceworks/spec": "^1.1.0",
    "@testing-library/react-hooks": "^5.1.2",
    "@types/echarts": "^4.9.10",
    "@types/jsonwebtoken": "^8.5.1",
    "@types/lodash": "^4.14.168",
    "@types/qs": "^6.9.6",
    "@types/react": "^17.0.2",
    "@types/react-relay": "^7.0.17",
    "babel-jest": "^27.2.5",
    "babel-plugin-import": "^1.13.3",
    "babel-plugin-relay": "^10.1.3",
    "buffer": "^6.0.3",
    "build-plugin-moment-locales": "^0.1.0",
    "crypto-browserify": "^3.12.0",
    "echarts": "^5.1.2",
    "eslint": "^6.8.0",
    "graphql": "^15.5.0",
    "husky": "^7.0.2",
    "ice.js": "^2.0.0",
    "jest": "^27.2.5",
    "lint-staged": "^11.1.2",
    "msw": "^0.28.2",
    "prettier": "^2.2.1",
    "react-bus": "^3.0.0",
    "react-test-renderer": "^17.0.1",
    "relay-compiler": "^10.1.3",
    "relay-config": "^10.1.3",
    "size-sensor": "^1.0.1",
    "stream-browserify": "^3.0.0",
    "stylelint": "^13.12.0",
    "tinycolor2": "^1.4.2",
    "ts-babel": "^6.1.7",
    "ts-jest": "^27.0.5",
    "ts-node": "^10.3.0",
    "velocity-animate": "^1.5.2"
  },
  "lint-staged": {
    "*.{ts,jsx,js}": [
      "eslint --fix"
    ]
  },
  "scripts": {
    "start": "node color && icejs start --config build.config.js --mode local",
    "build": "node color && icejs build --config build.config.js --mode prod",
    "v27test": "jest --jest-config=./jest.config.js",
    "clearTestCache": "jest --version --clearCache",
    "test": "icejs test --jest-config=./jest.config.js",
    "lint": "npm run eslint && npm run stylelint",
    "eslint": "eslint --fix --cache --ext .js,.jsx,.ts,.tsx ./src",
    "eslint:fix": "npm run eslint -- --fix",
    "stylelint": "stylelint \"**/*.{css,scss,less}\"",
    "prettier": "prettier ./src/* --write",
    "relay": "relay-compiler --src ./src --schema ../data/schema.graphql --extensions js jsx",
    "config": "if [ ! -f dev.config.js ];then echo 'module.exports = {};' > dev.config.js;fi",
    "prepare": "cd .. && husky install client/.husky",
    "lint-staged": "lint-staged"
  },
  "engines": {
    "node": ">=14.0.0"
  },
  "private": true
}
vision-yip commented 3 years ago

运行命令: npm run test

imsobear commented 3 years ago

看起来是自定义的 alias 没配置到 jest 里:

image

vision-yip commented 3 years ago

jest.config.js

module.exports = {
  preset: 'ts-jest/presets/js-with-babel-esm',
  // testEnvironment: 'node',
  setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
  moduleNameMapper: {
    '@config': '<rootDir>/project.config.js',
    '@/(.*)$': '<rootDir>/src/$1',
    '@public/$': '<rootDir>/public/$1',
    // https://stackoverflow.com/questions/54627028/jest-unexpected-token-when-importing-css
    // https://github.com/eddyerburgh/jest-transform-stub#readme
    '^.+\\.(css|less|scss)$': '<rootDir>/jest.config.js',
  },
  transform: {
    '^.+\\.tsx?$': 'babel-jest',
    '^.+\\.ts?$': 'ts-jest',
  },
  globals: {
    'ts-jest': {
      compiler: 'typescript',
      tsConfig: {
        // allow js in typescript
        allowJs: true,
      },
      useESM: true,
    },
  },
  // extensionsToTreatAsEsm: ['.ts'],
  transformIgnorePatterns: ['<rootDir>node_modules/(?!(_zrender@5.1.1@zrender|_echarts@5.1.2@echarts)/.*)'],
  modulePaths: ['<rootDir>'],
};

另外, 仅仅引用了request为什么会跟iconfont有交集🤔

imsobear commented 3 years ago

@vision-yip 要不提供个重现 demo,放到 GitHub 上,我试试。

vision-yip commented 3 years ago

因为跟项目一起, 无法提供最小复现demo😢😞@imsobear

imsobear commented 3 years ago

@vision-yip 根据基础模板改一下?因为你这个配置还挺复杂的,很可能是你的某个配置引起的问题。