【问题标题】:undefined returned when using identity-obj-proxy with typescript with jest使用带有 jest 的 typescript 的 identity-obj-proxy 时返回 undefined
【发布时间】:2018-10-15 10:47:01
【问题描述】:

我在我的项目中使用 jest 和 typescript。我使用 identity-obj-proxy 对我的所有 .ts 文件都未定义,但 .js 文件按预期工作。

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "jsx": "react",
    "declaration": true,
    "sourceMap": true,
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "outDir": "lib",
    "typeRoots": [
      "./node_modules/@types",
      "./node_modules/@microsoft"
    ],
    "types": [
      "es6-promise",
      "webpack-env"
    ],
    "lib": [
      "es5",
      "dom",
      "es2015.collection"
    ]
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules",
    "lib"
  ]
}

这是我开玩笑的配置:

"jest": {
    "unmockedModulePathPatterns": [
      "React"
    ],
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ],
    "transform": {
      "^.+\\.(d\\.ts|ts|tsx)$": "ts-jest"
    },
    "testMatch": [
      "**/src/**/*.test.+(ts|tsx|js)"
    ],
    "setupFiles": [
      "raf/polyfill"
    ],
    "collectCoverage": true,
    "coverageReporters": [
      "json",
      "lcov",
      "text",
      "cobertura"
    ],
    "coverageDirectory": "<rootDir>/jest",
    "collectCoverageFrom": [
      "**/*.{ts,tsx}",
      "!**/*.d.{ts,tsx}",
      "!**/*.scss.ts",
      "!**/models/**",
      "!**/node_modules*/**"
      "!**/services/http.ts"
    ],
    "moduleNameMapper": {
      "\\.(css|less|scss|sass)$": "identity-obj-proxy",
      "^resx-strings/en-us.json": "<rootDir>/node_modules/@microsoft/sp-core-library/lib/resx-strings/en-us.json"
   },
    "reporters": [
      "default",
      "jest-junit"
    ],
    "coverageThreshold": {
      "global": {
        "branches": 50,
        "functions": 75,
        "lines": 75,
        "statements": 75
      }
    }
  }

我的测试文件(.ts):

import styles from './Somefile.module.scss';

describe('Test identity proxy', () => {
  test('undefined returned', () => {
    expect(styles.notundefined).toBe(undefined);
  }
});

如果我将文件另存为 .js,那么一切似乎都可以正常工作,但在 .ts 或 .tsx 文件中却不行。

【问题讨论】:

    标签: typescript jestjs es6-proxy


    【解决方案1】:

    正如@Nathanael 所怀疑的那样,我相信 identity-object-proxy 模块中存在错误。

    但在我的情况下,它在使用时不起作用:

    import * as styles from './Somefile.module.scss';

    相反,我关注了@Nathanael 的链接,很高兴找到@joaovieira 的解决方法。他创建了自己的身份对象代理版本

    module.exports = new Proxy(
      {},
      {
        get: function getter(target, key) {
          if (key === '__esModule') {
            // True instead of false to pretend we're an ES module.
            return true;
          }
          return key;
        },
      },
    );
    

    他在 jest.config.js 中包含如下:

    module.exports = {
    ...
      moduleNameMApper: {
        '\\.(jpg|jpeg|png|gif|webp|svg)$': 'identity-obj-proxy',
        '\\.(css|scss)$': '<rootDir>/.jest/identity-obj-proxy-esm.js',
      }
    };
    

    要查看他的完整答案,请转到https://github.com/keyz/identity-obj-proxy/issues/8#issuecomment-430241345

    【讨论】:

      【解决方案2】:

      我正在使用 Jest 24,但遇到了类似的问题(如果不同衣服的问题不同)

      当我使用 ES6 导入在我的 JS 中包含 SASS 或 CSS 文件时,我遇到了问题,幸运的是有一个简单的解决方案。

      按照 Jest 文档的建议,将以下转换添加到您的 package.json

      "transform": { 
          "\\.(css|less|sass|scss)$": "<rootDir>/test/mock/styleMock.js"
      }
      

      根据 Jest 网站上的说明,创建 styleMock.js 文件,但不要只返回一个对象或字符串,而是包含一个“进程”函数,该函数返回一个字符串来解决问题,类似这样。

      module.exports = {
        process: function() {
          return "";
        }
      };
      

      如果您的目标是继续编写测试(或者如果您碰巧像我一样从较旧的框架迁移),这将安抚 Jest。 css ES6 导入将不再是问题。

      希望这提供了对我“几乎有效”的先前解决方案的稍微更新的版本!

      【讨论】:

        【解决方案3】:

        尝试将您的 scss 文件导入为

        import * as styles from './Somefile.module.scss';
        

        identity-obj-proxy 存在一个问题,它会阻止标准导入语法在 TypeScript 中工作,但由于某种原因 import * as 表示法可以正常工作。

        https://github.com/keyanzhang/identity-obj-proxy/issues/8

        【讨论】:

        • 这也没有解决问题。我在这里创建了一个问题github.com/keyz/identity-obj-proxy/issues/9
        • 对于 SPFx,直到 SPFx 1.8.0 才有效。在 SPFx 1.8.0 中,他们引入了更新的 typescript 版本,其中支持 "esModuleInterop": true,这使得该解决方案变得多余。仍然没有“esModuleInterop”:是的,这应该是正确的答案。
        • 从“哪里”导入 {dep1,dep2} 是什么情况
        猜你喜欢
        • 2018-02-16
        • 2018-02-28
        • 1970-01-01
        • 2017-12-31
        • 2019-12-12
        • 2021-09-29
        • 2022-11-24
        • 2022-12-07
        • 2020-03-16
        相关资源
        最近更新 更多