Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️

License

NotificationsYou must be signed in to change notification settings

mattphillips/babel-jest-assertions

Repository files navigation

🃏⁉️

Adds expect.assertions(n) and expect.hasAssertions to all tests automatically


Build StatusCode CoverageversiondownloadsMIT LicensePRs WelcomeRoadmapExamples

Problem

Ever wondered if your tests are actually running their assertions, especially in asynchronous tests? Jest has two featuresbuilt in to help with this:expect.assertions(number)andexpect.hasAssertions(). These can beuseful when doing something like:

it('resolves to one',()=>{Promise.reject(1).then(value=>expect(value).toBe(1));});

The issue here is thecatch case is not dealt with in this test,which is fine as we are testing the happy path,but this test will currently pass even though thePromise rejects and the assertion is never ran.

Solution

One solution is to manually adjust the above test to includeexpect.assertions(number) andexpect.hasAssertions()this is quite verbose and prone to human error.

An alternative is a babel plugin to automate adding these additional properties, and this is such plugin 😉

Installation

With npm:

npm install --save-dev babel-jest-assertions

With yarn:

yarn add -D babel-jest-assertions

Setup

.babelrc

{"plugins": ["babel-jest-assertions"]}

CLI

babel --plugins babel-jest-assertions script.js

Node

require('babel-core').transform('code',{plugins:['babel-jest-assertions'],})

Usage

Simply write your tests as you would normally and this plugin will add the verification of assertions in the background.

One assertion

it('resolves to one',()=>{Promise.reject(1).then(value=>expect(value).toBe(1));});

↓ ↓ ↓ ↓ ↓ ↓

it('resolves to one',()=>{expect.hasAssertions();expect.assertions(1);Promise.reject(1).then(value=>expect(value).toBe(1));});

Note: this test will now fail 🎉

Multiple assertions

it('counts multiple assertions too',()=>{expect(1+0).toBe(1);expect(0+1).toBe(1);});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too',()=>{expect.hasAssertions();expect.assertions(2);expect(1+0).toBe(1);expect(0+1).toBe(1);});

Asynchronous assertions

it('counts multiple assertions too',async()=>{constres=awaitfetch('www.example.com');expect(res.json).toBeTruthy();constjson=awaitres.json();expect(json).toEqual({whatever:'trevor'});});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too',async()=>{expect.hasAssertions();expect.assertions(2);constres=awaitfetch('www.example.com');expect(res.json).toBeTruthy();constjson=awaitres.json();expect(json).toEqual({whatever:'trevor'});});

beforeEach and afterEach blocks

If you have expectations inside either ofbeforeEach orafterEach blocks for your test then these expects will beincluded in the count - even if you have nested describe blocks each with their ownbeforeEach/afterEach the countwill accumulate.

beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});describe('.add',()=>{beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});it('returns 1 when given 0 and 1',()=>{expect(add(1,0)).toEqual(1);});describe('.add2',()=>{beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});it('returns 1 when given 0 and 1',()=>{expect(add2(1,0)).toEqual(1);});});});beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});describe('.add',()=>{beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});it('returns 1 when given 0 and 1',()=>{expect.assertions(5);expect.hasAssertions();expect(add2(1,0)).toEqual(1);});describe('.add2',()=>{beforeEach(()=>{expect(true).toBe(true);});afterEach(()=>{expect(true).toBe(true);});it('returns 1 when given 0 and 1',()=>{expect.assertions(7);expect.hasAssertions();expect(add2(1,0)).toEqual(1);});});});

Comments are ignored

it('ignores commented-out assertions',async()=>{constres=awaitfetch('www.example.com');// expect(res.json).toBeTruthy();constjson=awaitres.json();/*    expect(json).toEqual({ whatever: 'trevor1' });  */expect(json).toEqual({whatever:'trevor'});/* expect(json).toEqual({ whatever: 'trevor2' }); */});

↓ ↓ ↓ ↓ ↓ ↓

it('counts multiple assertions too',async()=>{expect.hasAssertions();expect.assertions(1);constres=awaitfetch('www.example.com');// expect(res.json).toBeTruthy();constjson=awaitres.json();/*    expect(json).toEqual({ whatever: 'trevor1' });  */expect(json).toEqual({whatever:'trevor'});/* expect(json).toEqual({ whatever: 'trevor2' }); */});

Override

If you add eitherexpect.assertions(number) orexpect.hasAssertions() then your defaults will be favoured and theplugin will skip the test.

it('will leave test as override supplied',()=>{expect.hasAssertions();expect.assertions(1);if(true){expect(true).toBe(true);}if(false){expect(false).toBe(false);}});

↓ ↓ ↓ ↓ ↓ ↓

it('will leave test as override supplied',()=>{expect.hasAssertions();expect.assertions(1);if(true){expect(true).toBe(true);}if(false){expect(false).toBe(false);}});

Contributors


Matt Phillips

💻📖🚇⚠️

Ramesh Nair

💻📖💡⚠️

Huy Nguyen

🐛

Simon Boudrias

🐛

Giuseppe

🤔

LICENSE

MIT

About

Babel Plugin that adds safety to your tests by verifying assertions are actually ran 🃏⁉️

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors2

  •  
  •  

[8]ページ先頭

©2009-2025 Movatter.jp