Skip to content

Extend Jest and Puppeteer to automatically take screenshots after each test failure or success

License

Notifications You must be signed in to change notification settings

jeeyah/jestscreenshot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jestscreenshot

Extend Jest and Puppeteer to automatically take screenshots after each test failure or success.

Screen shots will be created in a subdirectory of your test script as follows.

Success: screenshots/scriptname-testname.png Failure: screenshots/0-fail-scriptname-testname.png

Import jestscreenshot

let jestscreenshot = require('@jeeyah/jestscreenshot');

In beforeAll, initialize jestscreenshot after initializing Puppeteer

beforeAll(async () => {
  // Create puppeteer browser and page objects first:
  // 
  // browser = await options.puppeteer.launch
  // page = await ret.browser.newPage();
  
  let path = require('path');
  let scriptName = path.basename(__filename).replace('.js', '');
  /** page: page object returned from Puppeteer
   * dirName: __dirname defaults to same directory as script
   * scriptName: Used in the screen shot file name
   * onlyFailures: ONLY take screenshots on test failures, default: false
   */
  let options = {
    page: page, 
    dirName: __dirname,
    scriptName: scriptName
  };
  
  await jestscreenshot.init(options);
});

In afterAll, configure jestscreenshot cleanup just prior to closing browser

jestscreenshot needs to wait for screen shots to finish before the browser closes.

afterAll((done) => {
  jestscreenshot.cleanup(function () {
    if (browser) {
      browser.close();
    }
    done();
  });
});

Example test file

See tests/test.js for a working example.

About

Extend Jest and Puppeteer to automatically take screenshots after each test failure or success

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •