相关疑难解决方法(0)

嘲笑`文件`开玩笑

我正在尝试以开玩笑的方式为我的Web组件项目编写测试.我已经使用了带有es2015预设的babel.我在加载js文件时遇到问题.我已经跟踪了一段代码,其中document对象有一个currentScript对象.但在测试环境中它是null.所以我在想同样的嘲笑.但是jest.fn()并没有真正的帮助.我该如何处理这个问题?

一段代码,其中开玩笑失败.

var currentScriptElement = document._currentScript || document.currentScript;
var importDoc = currentScriptElement.ownerDocument;
Run Code Online (Sandbox Code Playgroud)

我写的测试用例. component.test.js

import * as Component from './sample-component.js';

describe('component test', function() {
  it('check instance', function() {
    console.log(Component);
    expect(Component).toBeDefined();
  });
});
Run Code Online (Sandbox Code Playgroud)

以下是jest抛出的错误

Test suite failed to run

    TypeError: Cannot read property 'ownerDocument' of null

      at src/components/sample-component/sample-component.js:4:39
Run Code Online (Sandbox Code Playgroud)

更新: 根据AndreasKöberle的建议,我添加了一些全球变量,并试图嘲笑如下

__DEV__.document.currentScript = document._currentScript = {
  ownerDocument: ''
};
__DEV__.window = {
  document: __DEV__.document
}
__DEV__.document.registerElement = jest.fn();

import * as Component from './arc-sample-component.js'; …
Run Code Online (Sandbox Code Playgroud)

jsdom jestjs babel-jest

32
推荐指数
6
解决办法
4万
查看次数

导入SASS文件的测试组件时出现语法错误

我正在尝试React使用Jest+ 测试我的组件Enzyme,但是当我的组件有SASSfile(scss)时,正在发生SyntaxError.

这是我的SASS文件内容:

.user-box {
  width: 50px;
  height: 50px;
}
Run Code Online (Sandbox Code Playgroud)

我只是在我的组件中导入它:

import React from 'react';

import './userBox.scss';

class MyComponent extends React.Component {
    render() {
        const style = {
            borderRadius: '99px'
        };
        return (
            <div>Hello World</div>
        );
    }
}

export default MyComponent;
Run Code Online (Sandbox Code Playgroud)

以下是我测试的错误消息:

Jest测试错误消息

如果我评论import './userBox.scss';,测试将是okey.

如果导入样式,如何ReactJest+`Enzyme` 测试组件

javascript unit-testing reactjs jestjs enzyme

8
推荐指数
4
解决办法
5413
查看次数

标签 统计

jestjs ×2

babel-jest ×1

enzyme ×1

javascript ×1

jsdom ×1

reactjs ×1

unit-testing ×1