Шутка: «Контейнер не существует» - PullRequest
0 голосов
/ 24 апреля 2019

У меня очень простой тест

    const div = document.createElement("div");
    ReactDOM.render(<App />, div);
    ReactDOM.unmountComponentAtNode(div);

не проходит по этому коду:

componentDidMount() {
    const ProgressBar = require('progressbar.js');
    /* istanbul ignore next */
    const bar = new ProgressBar.Line('#progressDiv', {
        strokeWidth: 2,
        easing: 'easeInOut',

ошибка:

● renders without crashing

Container does not exist: #progressDiv

   6 |         const ProgressBar = require('progressbar.js');
   7 |         /* istanbul ignore next */
>  8 |         const bar = new ProgressBar.Line('#progressDiv', {
     |                     ^
   9 |             strokeWidth: 2,

контейнер существует.

Я предполагаю, что это распространенная проблема, потому что ComponentDidMount выполняется перед рендерингом?

команда запуска:

npm test => "test": "react-scripts test --watchAll=false"

версии:

"react-scripts": {
  "version": "2.1.5",
  "jest": "23.6.0",

1 Ответ

0 голосов
/ 24 апреля 2019

после большой боли я решил это с помощью насмешки.

jest.mock('../components/app/App');

it("renders without crashing", () => {

    const spy = jest.fn()
    App.prototype.componentDidMount.mockImplementation(() => spy())

    const div = document.createElement("div");
    ReactDOM.render(<App />, div);
    ReactDOM.unmountComponentAtNode(div);
});

это проходит мои тесты, хотя vs code все еще жалуется

mockImplementation не существует для type () => void

...