如何配置笑话测试以失败警告?
console.warn('stuff');
// fail test
您可以使用这个简单的覆盖:
let error = console.error
console.error = function (message) {
error.apply(console, arguments) // keep default behaviour
throw (message instanceof Error ? message : new Error(message))
}
您可以使用 Jest setupFiles 使其在所有测试中可用。
在package.json
:
"jest": {
"setupFiles": [
"./tests/jest.overrides.js"
]
}
然后将片段放入jest.overrides.js
我最近使用jest.spyOn
引入v19.0.0
来模拟(通过上下文/对象访问)的warn
方法来实现这一点。console
global
然后可以expect
认为mockedwarn
没有被调用,如下图。
describe('A function that does something', () => {
it('Should not trigger a warning', () => {
var warn = jest.spyOn(global.console, 'warn');
// Do something that may trigger warning via `console.warn`
doSomething();
// ... i.e.
console.warn('stuff');
// Check that warn was not called (fail on warning)
expect(warn).not.toHaveBeenCalled();
// Cleanup
warn.mockReset();
warn.mockRestore();
});
});
对于那些使用 create-react-app 的人,不想运行npm run eject
,您可以将以下代码添加到./src/setupTests.js
:
global.console.warn = (message) => {
throw message
}
global.console.error = (message) => {
throw message
}
现在,当消息传递到console.warn
or时, jest 将失败console.error
。
有一个有用的 npm 包可以帮助您实现这一目标:jest-fail-on-console
它很容易配置。
npm i -D jest-fail-on-console
在 Jest 的setupFilesAfterEnv选项中使用的文件中,添加以下代码:
import failOnConsole from 'jest-fail-on-console'
failOnConsole()
// or with options:
failOnConsole({ shouldFailOnWarn: false })
我决定发布基于user1823021 答案的完整示例
describe('#perform', () => {
var api
// the global.fetch is set to jest.fn() object globally
global.fetch = jest.fn()
var warn = jest.spyOn(global.console, 'warn');
beforeEach(function() {
// before every test, all mocks need to be resetted
api = new Api()
global.fetch.mockReset()
warn.mockReset()
});
it('triggers an console.warn if fetch fails', function() {
// In this test fetch mock throws an error
global.fetch.mockImplementationOnce(() => {
throw 'error triggered'
})
// I run the test
api.perform()
// I verify that the warn spy has been triggered
expect(warn).toHaveBeenCalledTimes(1);
expect(warn).toBeCalledWith("api call failed with error: ", "error triggered")
});
it('calls fetch function', function() {
// I create 2 more mock objects to verify the fetch parameters
const url = jest.fn()
const config = jest.fn()
api.url = url
api.config = config
// I run the test
api.perform()
// I verify that fetch has been called with url and config mocks
expect(global.fetch).toHaveBeenCalledTimes(1)
expect(global.fetch).toBeCalledWith(url, config)
expect(warn).toHaveBeenCalledTimes(0)
});
})
#perform
我正在测试的方法
class Api {
constructor(auth) {
this._credentials = auth
}
perform = async () => {
try {
return await fetch(this.url, this.config)
} catch(error) {
console.warn('api call failed with error: ', error)
}
}
}
CI=true
您可以在运行之前设置环境变量,jest
这将导致它除了错误之外的警告测试失败。
运行文件test
夹中所有测试文件的示例:
CI=true jest ./test
自动 CI/CD 管道(例如 Github Actions)默认设置CI
为true
,这可能是单元测试在引发警告时在本地计算机上通过但在管道中失败的原因之一。
(这里是关于默认环境变量的 Github Actions 文档:https ://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables )