Your own tests

Page content:

To test what a page does, beyond the automatic checks, write a page test: a file in test/pages that describes one page. Its default export is a Nightwatch page object, and every other exported function is a test case. Its path follows the page's, as postdoc create page writes it: test/pages/guide/index.js for /guide/index.html. A file and a folder with the same name, such as guide.js and guide/, would both be the page object guide, and Nightwatch finds only one of them.

test/pages/guide/index.jsjs
export default {
  url: '/guide/index.html',

  elements: {
    content: '#content',
    firstHeading: 'main h2'
  }
};

export function contentIsVisible({ page }) {
  page.assert.visible('@content');
  page.assert.textContains('@firstHeading', 'Introduction');
}

For each page test, the plugin opens the page in the served build, waits until it has finished rendering, and runs the test cases on it, one after another. A test case receives the page object as page, its element API as element, and browser. ES modules list their exports alphabetically, so that's the order the test cases run in.

To check one page against stricter accessibility rules than the page checks, add a11y settings to it. They add an accessibility test of that page:

test/pages/guide/index.jsjs
export default {
  url: '/guide/index.html',

  a11y: {
    selector: 'main',
    config: {
      rules: {
        'heading-order': { enabled: true }
      }
    }
  }
};

The default export can also have before, after, beforeEach and afterEach hooks. The plugin's README describes every setting.

Other Nightwatch tests in the tests folder, test/ by default, run as they are. While postdoc test runs, the address of the served build is in the POSTDOC_BASE_URL environment variable and is Nightwatch's launch URL, so relative URLs open the build. After an action that moves to another page, wait for it with browser.waitForPostDocReady().

To use your own Nightwatch configuration, point nightwatch.config at it, and list the plugin there:

postdoc.config.jsjs
export default {
  nightwatch: {
    config: './nightwatch.conf.js'
  }
};
nightwatch.conf.jsjs
const configuration = {
  plugins: ['nightwatch-plugin-postdoc'],

  test_settings: {
    default: {
      desiredCapabilities: {
        browserName: 'chrome'
      }
    },
    // postdoc test runs the environment named after each browser in nightwatch.browser,
    // chrome by default.
    chrome: {}
  }
};

export default configuration;

// Nightwatch loads its configuration with require(), which returns this export of an
// ES module instead of the module itself.
export { configuration as 'module.exports' };

Watch mode

sh
npx postdoc test --watch

runs your tests again whenever a file in the tests folder changes. The page checks don't run in watch mode.

Edit this page on GitHub