19

我试图为我现有的项目创建测试文件,该项目由

https://github.com/facebookincubator/create-react-app

但是当我跑的时候我得到了这个错误npm test a

FAIL  src/__tests__/movie_single/MovieSingle-test.js
  ● Test suite failed to run

    matchMedia not present, legacy browsers require a polyfill

      at Object.MediaQueryDispatch (node_modules/enquire.js/dist/enquire.js:226:19)
      at node_modules/enquire.js/dist/enquire.js:291:9
      at Object.<anonymous>.i (node_modules/enquire.js/dist/enquire.js:11:20)
      at Object.<anonymous> (node_modules/enquire.js/dist/enquire.js:21:2)
      at Object.<anonymous> (node_modules/react-responsive-mixin/index.js:2:28)
      at Object.<anonymous> (node_modules/react-slick/lib/slider.js:19:29)
      at Object.<anonymous> (node_modules/react-slick/lib/index.js:3:18)
      at Object.<anonymous> (src/components/movie_single/MovieDetailsBox.js:4:45)
      at Object.<anonymous> (src/components/movie_single/MovieSingle.js:3:50)
      at Object.<anonymous> (src/__tests__/movie_single/MovieSingle-test.js:3:46)
      at process._tickCallback (internal/process/next_tick.js:103:7)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.642s
Ran all test suites matching "a".

所以这是我的package.json

{
  "name": "xxx",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "autoprefixer-stylus": "0.10.0",
    "concurrently": "3.0.0",
    "react-scripts": "0.6.1",
    "stylus": "0.54.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "watch": "concurrently --names 'webpack, stylus' --prefix name 'npm run start' 'npm run styles:watch'",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
  }
}

这是我的MovieSingle-test.js

import React from 'react';
import ReactDOM from 'react-dom';
import MoviesSingle from '../../components/movie_single/MovieSingle';

it('renders without crashing', () => {
    const div = document.createElement('div');
    ReactDOM.render(<MoviesSingle />, div);
});

那么我该如何解决这个问题并至少使通用反应组件测试通过?

谢谢!

4

7 回答 7

27

将 a 添加src/setupTests.js到您的项目(将在运行测试之前执行):

/**
 * fix: `matchMedia` not present, legacy browsers require a polyfill
 */
global.matchMedia = global.matchMedia || function() {
  return {
      matches : false,
      addListener : function() {},
      removeListener: function() {}
  }
}
于 2017-02-18T14:20:54.673 回答
6

我也刚刚遇到它,这对我有帮助:

test-setup.js这里获取并将其放置到src目录中

window.matchMedia = window.matchMedia || function() {
    return {
        matches : false,
        addListener : function() {},
        removeListener: function() {}
    };
};

将以下内容添加到package.json

"jest": {
    "setupFiles": ["<rootDir>\\src\\test-setup.js", "<rootDir>\\config\\polyfills.js"]
  },

看看是否有帮助。

于 2017-01-22T19:00:10.353 回答
4

添加这段代码:

window.matchMedia =
    window.matchMedia ||
    function() {
        return {
            matches: false,
            addListener: function() {},
            removeListener: function() {}
        };
    };

在 jest.setup.js 或 setupTests.js 中(您可以从 jest.config.js 中检查名称,例如 setupFiles: ["./jest.setup.js"] )

于 2019-11-23T12:34:36.013 回答
1

如果您使用的是 GatsbyJS,请将来自 @maow 的代码放在根目录下的 loadershim.js 文件中。

于 2018-10-16T12:44:22.540 回答
1

这就是我设法使它工作的方法:

// fix: `matchMedia` not present, legacy browsers require a polyfill
global.window.matchMedia = (query) => ({
  matches: false,
  media: query,
  onchange: null,
  addListener: () => {}, // deprecated
  removeListener: () => {}, // deprecated
  addEventListener: () => {},
  removeEventListener: () => {},
  dispatchEvent: () => {},
})
于 2021-05-07T16:54:27.670 回答
0

我按照以下步骤解决了我的问题:

  • 创建了一个文件setupTests.js
  • 将它作为src/setupTests.js放在src文件夹中
  • 将以下代码放入setupTests.js
window.matchMedia = window.matchMedia || function() {
    return {
        matches: false,
        addListener: function() {},
        removeListener: function() {}
    };
};

注意:再次运行npm 测试,它将解决问题(停止监视任务并重新启动它)。

于 2020-12-29T11:15:10.667 回答
0

从错误消息中获取线索:

matchMedia 不存在,旧版浏览器需要 polyfill

这表示某些功能在测试时不可用,需要以某种方式实现才能执行操作。

这种 polyfill找到合适的polyfill代码。 matchMedia

src然后在名为的文件夹中创建一个新文件,setupTests.js并将 polyfill 代码粘贴到文件中。React 脚本注意这个文件将在测试运行之前执行。

现在再次运行测试。它是固定的。 其他人也有同样的问题。

于 2017-08-19T16:22:47.543 回答