Api*_*oud 44 javascript unit-testing mocking jestjs
我不知道是否有更好的方法来禁用错误控制台 里面一个特定的玩笑测试(即,恢复原来的控制台前/每次测试后).
这是我目前的做法:
describe("Some description", () => {
let consoleSpy;
beforeEach(() => {
if (typeof consoleSpy === "function") {
consoleSpy.mockRestore();
}
});
test("Some test that should not output errors to jest console", () => {
expect.assertions(2);
consoleSpy = jest.spyOn(console, "error").mockImplementation();
// some function that uses console error
expect(someFunction).toBe("X");
expect(consoleSpy).toHaveBeenCalled();
});
test("Test that has console available", () => {
// shows up during jest watch test, just as intended
console.error("test");
});
});
Run Code Online (Sandbox Code Playgroud)
是否有更简洁的方法来完成同样的事情?我想避免spyOn
,但mockRestore
似乎只能使用它.
谢谢!
Con*_*tin 88
如果你只想做一个特定的测试:
beforeEach(() => {
jest.spyOn(console, 'warn').mockImplementation(() => {});
});
Run Code Online (Sandbox Code Playgroud)
Raj*_*han 54
对于特定的spec文件,Andreas的足够好.以下设置将禁止console.log
所有测试套件的语句,
jest --silent
Run Code Online (Sandbox Code Playgroud)
(要么)
要自定义,warn, info and debug
您可以使用以下设置
__tests __/setup.js
global.console = {
log: jest.fn(), // console.log are ignored in tests
// Keep native behaviour for other methods, use those to print out things in your own tests, not `console.log`
error: console.error,
warn: console.warn,
info: console.info,
debug: console.debug,
};
Run Code Online (Sandbox Code Playgroud)
jest.config.js
module.exports = {
verbose: true,
setupTestFrameworkScriptFile: "<rootDir>/__tests__/setup.js",
};
Run Code Online (Sandbox Code Playgroud)
And*_*rle 35
由于每个测试文件都在自己的线程中运行,因此如果要在一个文件中为所有测试禁用它,则无需还原它.出于同样的原因你也可以写
console.log = jest.fn()
expect(console.log).toHaveBeenCalled();
Run Code Online (Sandbox Code Playgroud)
nic*_*ckb 14
我发现上面的答案是:console.log
在调用任何其他console
方法(例如)时,所有测试套件中的压缩都会引发错误warn
,error
因为它正在替换整个全局console
对象.
这种有点类似的方法适用于Jest 22+:
"jest": {
"setupFiles": [...],
"setupTestFrameworkScriptFile": "<rootDir>/jest/setup.js",
...
}
Run Code Online (Sandbox Code Playgroud)
jest.spyOn(global.console, 'log').mockImplementation(() => jest.fn());
Run Code Online (Sandbox Code Playgroud)
使用此方法,仅console.log
模拟并且其他console
方法不受影响.
beforeAll(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
jest.spyOn(console, 'error').mockImplementation(() => {});
jest.spyOn(console, 'warn').mockImplementation(() => {});
jest.spyOn(console, 'info').mockImplementation(() => {});
jest.spyOn(console, 'debug').mockImplementation(() => {});
});
Run Code Online (Sandbox Code Playgroud)
对我来说,一个更清晰/干净的方法(读者需要很少的 jest API 知识来理解正在发生的事情),就是手动执行 mockRestore 所做的:
// at start of test you want to suppress
const consoleLog = console.log;
console.log = jest.fn();
// at end of test
console.log = consoleLog;
Run Code Online (Sandbox Code Playgroud)
如果您使用命令npm test
来运行测试,请更改package.jsontest
script
中的内容,如下所示
{
....
"name": "....",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest --silent", // add --silent to jest in script like this
"lint": "eslint ."
},
...
}
Run Code Online (Sandbox Code Playgroud)
或者您可以直接运行命令npx jest --silent
来清除测试时的所有日志和错误
这是您可能想要使用的所有行。您可以将它们直接放入测试中:
jest.spyOn(console, 'warn').mockImplementation(() => {});
console.warn("You won't see me!")
expect(console.warn).toHaveBeenCalled();
console.warn.mockRestore();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21383 次 |
最近记录: |