Agenda

  1. Jest...so what? big deal, who cares?
  2. Your project before and after Jest
  3. Snapshot testing
  4. Visual regression tests with image snapshots
### Why Jest? - ~~New and Shiny~~ - Developer Experience - Snapshot testing - Super easy migration
### Facts - *Actively* supported by **[Facebook](https://github.com/facebook)** - v0.1.0 released **May 14, 2014** - Current version === **"v20.0.4"** - [11,975 stars on GitHub](https://github.com/facebook/jest/stargazers) - Does ***NOT*** support AMD (*and prob never will*)
### "Delightful JavaScript Testing" - Easy setup (and migration) - Colorized output - Regex spec selection - Batteries included: - watch task - expectations and mocking - code coverage (via Istanbul)

Snapshot all the things

(not really)







Compare serializable values

Without snapshots


        // output === {foo: 'foo', bar: 'bar'}
        describe('Some test suite', function() {
            it('can create an object', () => {
                expect(output.foo).toEqual('foo');
                expect(output.bar).toEqual('bar');
            });
        });
            

With snapshots


        // output === {foo: 'foo', bar: 'bar'}
        describe('Some test suite', function() {
            it('can create an object', () => {
                expect(output).toMatchSnapshot();
            });
        });
            
# Using Jest ### Before and After

Before

without Jest

After

without Jest

Before


    {
        "dependencies": {
            "chai": "^4.0.1",
            "chai-shallow-deep-equal": "^1.4.6",
            "istanbul": "^0.4.4",
            "mocha": "^3.4.2",
            "mocha-lcov-reporter": "^1.0.0",
            "mocha-unfunk-reporter": "^0.4.0",
            "nyc": "^11.0.2",
        }
    }
            

After


    {
        "dependencies": {
            "jest": "^20.0.4"
        }
    }
            

Before


    // Mocha uses before()
    describe('Some Test Suite', function() {
        before(() => {
            // one-time setup
        });
        it('can be awesome', () => {/* expects and stuff */});
    });
            

After


    // Jest uses beforeAll()
    describe('Some Test Suite', function() {
        beforeAll(() => {
            // one-time setup
        });
        it('can be awesome', () => {/* expects and stuff */});
    });
            
### You might not need [chai](http://chaijs.com/)
### You might not need [sinon](http://sinonjs.org/)
## Ingredients - [Jest](https://facebook.github.io/jest/) - *Test framework* - [jest-image-snapshot](https://github.com/americanexpress/jest-image-snapshot) - *Jest plugin* - [puppeteer](https://github.com/GoogleChrome/puppeteer) - *Headless Chrome Node API* - [http-server](https://www.npmjs.com/package/http-server) - *Lightweight http server*

Start static server


    {
        "scripts": {
            "server:start": "nohup http-server -p 1337 &"
        }
    }
            

Capture Screenshots


    function captureScreenshots(options) {
        return (async () => {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.setViewport({width, height});
            await page.goto(/* url */);
            // perform action
            await page.screenshot(/* path */);
            browser.close();
            return 'Capture Complete';
        })();
    }
            

Run snaphot tests


    // First run will save snapshots

    // Second run will perform comparisons

    describe('Your software', function() {
        it('can be free of visual regressions', () => {
            return images.each(image => {
                expect(image).toMatchImageSnapshot();
            });
        });
    });
            
### Examples - [generator-omaha](https://github.com/omahajs/generator-omaha) - *Yeoman generator* - [Voxelcss](https://voxelcss.js.org/) - *3D cubes* - [snapsvg-hexagonal](https://github.com/jhwohlgemuth/snapsvg-hexagonal) - *Snap.svg plugin*

References