@aarondewes/wp-jest-console 中文文档教程

发布于 3年前 浏览 27 项目主页 更新于 3年前

Jest Console

自定义 Jest 匹配器 控制台 对象来测试 WordPress 中的 JavaScript 代码。

此包将 console.errorconsole.infoconsole.logconsole.warn 函数转换为模拟和跟踪他们的电话。 每当测试代码调用上述 console 方法之一时,它还会强制使用相关匹配器之一。 这意味着当 console.error 时,您需要使用 .toHaveErrored().toHaveErroredWith( arg1, arg2, ... ) 断言 得到执行,并在调用console.infoconsole.logconsole.warn时使用相应的方法。 否则你的测试将失败! 这是一个有意识的设计决策,有助于在以下情况下检测弃用警告 重构代码时升级依赖库或更小的错误。

Installation

安装模块:

npm install @wordpress/jest-console --save-dev

注意:此包需要 Node.js 12.0.0 或更高版本。 它与旧版本不兼容。

Setup

最简单的设置是使用 Jest 的 setupFilesAfterEnv 配置选项:

"jest": {
  "setupFilesAfterEnv": [
    "@wordpress/jest-console"
  ]
},

Usage

.toHaveErrored()

使用 .toHaveErrored 确保调用 console.error 函数。

例如,假设您有一个 drinkAll( flavor ) 函数,可以让您喝下所有可用的饮料。 您可能想检查函数是否为 'octopus' 调用了 console.error,因为 'octopus' 风味确实 很奇怪,为什么会有章鱼味的东西? 您可以使用此测试套件做到这一点:

describe( 'drinkAll', () => {
    test( 'drinks something lemon-flavored', () => {
        drinkAll( 'lemon' );
        expect( console ).not.toHaveErrored();
    } );

    test( 'errors when something is octopus-flavored', () => {
        drinkAll( 'octopus' );
        expect( console ).toHaveErrored();
    } );
} );

.toHaveErroredWith( arg1, arg2, ... )

使用 .toHaveErroredWith 确保 console.error 函数被调用 具体论据。

例如,假设您有一个 drinkAll( flavor ) 函数,让您喝下所有可用的饮料。 您可能想检查函数调用 console.error 是否带有针对 'octopus' 的特定消息,因为 'octopus' 味道真的很奇怪,为什么会有章鱼味的东西? 为确保这有效,您可以编写:

describe( 'drinkAll', () => {
    test( 'errors with message when something is octopus-flavored', () => {
        drinkAll( 'octopus' );
        expect( console ).toHaveErroredWith(
            'Should I really drink something that is octopus-flavored?'
        );
    } );
} );

.toHaveInformed()

使用 .toHaveInformed 确保调用 console.info 函数。

.toHaveErrored() 几乎相同的用法。

.toHaveInformedWith( arg1, arg2, ... )

使用 .toHaveInformedWith 确保调用了 console.info 函数 具体论据。

.toHaveErroredWith() 几乎相同的用法。

.toHaveLogged()

使用 .toHaveLogged 确保调用了 console.log 函数。

.toHaveErrored() 几乎相同的用法。

.toHaveLoggedWith( arg1, arg2, ... )

使用 .toHaveLoggedWith 确保调用了 console.log 函数 具体论据。

.toHaveErroredWith() 几乎相同的用法。

.toHaveWarned()

使用 .toHaveWarned 确保 console.warn 函数被调用。

.toHaveErrored() 几乎相同的用法。

.toHaveWarnedWith( arg1, arg2, ... )

使用 .toHaveWarneddWith 确保调用了 console.warn 函数 具体论据。

.toHaveErroredWith() 几乎相同的用法。



代码即诗歌。

Jest Console

Custom Jest matchers for the Console object to test JavaScript code in WordPress.

This package converts console.error, console.info, console.log and console.warn functions into mocks and tracks their calls. It also enforces usage of one of the related matchers whenever tested code calls one of the mentioned console methods. It means that you need to assert with .toHaveErrored() or .toHaveErroredWith( arg1, arg2, ... ) when console.error gets executed, and use the corresponding methods when console.info, console.log or console.warn are called. Your test will fail otherwise! This is a conscious design decision which helps to detect deprecation warnings when upgrading dependent libraries or smaller errors when refactoring code.

Installation

Install the module:

npm install @wordpress/jest-console --save-dev

Note: This package requires Node.js 12.0.0 or later. It is not compatible with older versions.

Setup

The simplest setup is to use Jest's setupFilesAfterEnv config option:

"jest": {
  "setupFilesAfterEnv": [
    "@wordpress/jest-console"
  ]
},

Usage

.toHaveErrored()

Use .toHaveErrored to ensure that console.error function was called.

For example, let's say you have a drinkAll( flavor ) function that makes you drink all available beverages. You might want to check if function calls console.error for 'octopus' instead, because 'octopus' flavor is really weird and why would anything be octopus-flavored? You can do that with this test suite:

describe( 'drinkAll', () => {
    test( 'drinks something lemon-flavored', () => {
        drinkAll( 'lemon' );
        expect( console ).not.toHaveErrored();
    } );

    test( 'errors when something is octopus-flavored', () => {
        drinkAll( 'octopus' );
        expect( console ).toHaveErrored();
    } );
} );

.toHaveErroredWith( arg1, arg2, ... )

Use .toHaveErroredWith to ensure that console.error function was called with specific arguments.

For example, let's say you have a drinkAll( flavor ) function again makes you drink all available beverages. You might want to check if function calls console.error with a specific message for 'octopus' instead, because 'octopus' flavor is really weird and why would anything be octopus-flavored? To make sure this works, you could write:

describe( 'drinkAll', () => {
    test( 'errors with message when something is octopus-flavored', () => {
        drinkAll( 'octopus' );
        expect( console ).toHaveErroredWith(
            'Should I really drink something that is octopus-flavored?'
        );
    } );
} );

.toHaveInformed()

Use .toHaveInformed to ensure that console.info function was called.

Almost identical usage as .toHaveErrored().

.toHaveInformedWith( arg1, arg2, ... )

Use .toHaveInformedWith to ensure that console.info function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().

.toHaveLogged()

Use .toHaveLogged to ensure that console.log function was called.

Almost identical usage as .toHaveErrored().

.toHaveLoggedWith( arg1, arg2, ... )

Use .toHaveLoggedWith to ensure that console.log function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().

.toHaveWarned()

Use .toHaveWarned to ensure that console.warn function was called.

Almost identical usage as .toHaveErrored().

.toHaveWarnedWith( arg1, arg2, ... )

Use .toHaveWarneddWith to ensure that console.warn function was called with specific arguments.

Almost identical usage as .toHaveErroredWith().



Code is Poetry.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文