显然,mock.mockRestore() 不会恢复使用 jest.mock() 创建的模拟的原始实现
// a.js
export default class A {}
// b.js
import A from './a';
export default class B extends A {}
// test.js
import A from './a';
import B from './b';
jest.mock('./a');
jest.mock('./b');
const b = new B();
test('instanceOf', () => {
A.mockRestore();
B.mockRestore();
expect(b).toBeInstanceOf(A); // fails
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的 React Native 项目设置酶测试。我在各种情况下都遇到了各种错误。
当我尝试为我的一个组件设置测试时,出现以下错误:
/Users/TuzMacbookPro2017/Development/QMG-local/APPS/ELECTRO/node_modules/@expo/vector-icons/Zocial.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import glyphMap from './vendor/react-native-vector-icons/glyphmaps/Zocial.json';
^^^^^^^^
SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
一些相关文件
/Users/TuzMacbookPro2017/Development/QMG-local/APPS/ELECTRO/node_modules/@expo/vector-icons/Zocial.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import glyphMap from './vendor/react-native-vector-icons/glyphmaps/Zocial.json';
^^^^^^^^
SyntaxError: Unexpected identifier
Run Code Online (Sandbox Code Playgroud)
import React from "react";
import renderer from "react-test-renderer";
import { mount, ReactWrapper } from "enzyme";
import LoginView from "../src/screens/LoginView";
describe("LoginView", () => {
const wrapper = mount(<LoginView />);
it("can get through the damn test file", () => {
expect(true).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
module.exports = {
setupFilesAfterEnv: ["<rootDir>setup-tests.js"],
transformIgnorePatterns: [
"node_modules/(?!(jest-)?react-native|@react-native-community|react-native-elements)"
],
preset: "react-native" …Run Code Online (Sandbox Code Playgroud) 我正在使用 selenium 开发端到端测试套件,其中测试用例是使用 jest 测试运行程序用 JavaScript 编写的。
我的问题是,当某些东西无法正常工作时,硒通常会毫不客气地失败,而对其失败的原因几乎没有解释。不用说,像这样调试测试可能非常困难。
我正在寻找一种方法来登录每个测试用例,所以我知道测试失败的地方,但是如果测试实际失败,只在测试输出中显示这些日志(为了不污染测试的控制台输出,有很多不必要的日志)。
所以我想做一些类似的事情:
describe(() => {
it('case 1', async () => {
// this log should only be printed on the console if this test fails
logger.info('message from case 1');
// ...
});
it('case 2', () => {
logger.info('message from case 2');
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
因此,如果测试case 1失败并且case 2没有失败,我会message from case 1在控制台输出中看到(理想情况下就在该测试用例的错误之前)而不是 message from case 2.
这可能是开玩笑的吗?我可以为此自由使用任何日志库。