Skip to content

Modus-Logo-Long-BlackCreated with Sketch.

  • Services
  • Work
  • Blog
  • Resources

    OUR RESOURCES

    Innovation Podcast

    Explore transformative innovation with industry leaders.

    Guides & Playbooks

    Implement leading digital innovation with our strategic guides.

    Practical guide to building an effective AI strategy
  • Who we are

    Our story

    Learn about our values, vision, and commitment to client success.

    Open Source

    Discover how we contribute to and benefit from the global open source ecosystem.

    Careers

    Join our dynamic team and shape the future of digital transformation.

    How we built our unique culture
  • Let's talk
  • EN
  • FR

Writing Tests with CodeceptJS & Nightmare

Published on March 2, 2018
Last Updated on April 23, 2021
Quality Assurance

There are plenty of test automation tools available for a project. Most give you the ability to combine frameworks together to better suit your needs. Because there are so many different options and combinations of testing tools and libraries, it may be difficult finding something to get started with. To help with that, let’s take a look at one such combination for testing: CodeceptJS and Nightmare – a framework and integrated library that make it simple to create fast, scalable acceptance tests for any project.

Nightmare

Nightmare is a library for browser automation and is used as a web crawler as well as a means for UI testing. It uses Electron under the hood and can be considered as the newer, faster version of PhantomJS. Nightmare can run tests in both headless mode and in a window for debugging and because it doesn’t rely on drivers like Selenium, it’s incredibly fast.

It has a simple API and incorporates the same actions engineers are most familiar with like .goto(url[, headers]) and .check(selector). A test written with Nightmare looks like this:

Writing Tests with CodeceptJS Nightmare, Example
*Example provided in project repo



You can also incorporate a more BDD-like structure for your tests by using Mocha and Chai assertions with the Nightmare library.

Writing Tests with CodeceptJS & Nightmare, BDD-like structure with Mocha and Chai
*Example provided in project repo



Nightmare provides a simple library for writing UI tests as it’s fast and well-documented. However, it may not offer some of the same flexibility testers are used to with Selenium as it’s just a library and not a full test framework. Thankfully, there are options to further extend your Nightmare tests.

CodeceptJS

CodeceptJS is a tool for writing acceptance tests. It focuses on test scenarios and the user’s perspective. It supports mobile testing and is built to use many of the popular libraries like Nightmare, WebdriverIO, and Protractor. CodeceptJS adds to Nightmare’s test actions and incorporates readable test scenarios.

Writing Tests with CodeceptJS Nightmare, Example



Another feature of CodeceptJS is its ability to generate page objects, page fragments, and step objects. This makes it much easier organizing tests without a lot of extra effort.

Setting Things Up

Getting things set up is simple. First run npm install -g codeceptjs-nightmare to install all of the necessary dependencies. Next, generate the project by running codeceptjs init. You will be prompted with a few set up questions:

  1. Where are your tests located? (default location ./*_test.js)
  2. What helpers do you want to use? (Use space to select)
  3. Where should logs, screenshots, and reports to be stored?
  4. Would you like to extend I object with custom steps?
  5. Do you want to choose localization for tests?
  6. Where would you like to place custom steps?
  7. [Nightmare] Base url of site to be tested?

This will generate a codecept.json file. Now you can create your first test with codeceptjs gt.


Writing Tests with CodeceptJS Nightmare, JSON file



You can further organize your tests with page object, step object, and page fragment generators. You can find more information in CodeceptJS’ documentation, but here are the available commands:

  • Page Object: codeceptjs gpo
  • Step Object: codeceptjs go --type step
  • Page Fragment: codeceptjs go --type fragment

CodeceptJS makes it easier for teams to begin creating automated tests because it works with some of the most popular libraries and uses simple, acceptance-based scenarios. This lets you create easy-to-read tests and scale your test suite with your project.

There is a test harness available if you want to quickly spin up CodeceptJS-Nightmare tests for your application. It includes all necessary dependencies, an option to generate an HTML report, and is set up to also run in a Docker container via docker-compose. Check it out at https://github.com/ModusCreateOrg/codeceptjs-nightmare-harness.

Wrapping Up

There are many options to choose from when designing a structure for tests. Why work with Nightmare and CodeceptJS? CodeceptJS makes it simple to create a uniform test suite across popular libraries like WebdriverIO, Appium, and Nightmare. It builds upon what these libraries already offer and allows teams to easily create new test projects. Nightmare gives you the ability to create tests that are fast and efficient – a major plus for any automation roadmap.

If you’re having trouble choosing tools for an automation suite or getting started with automation altogether, consider checking out the repo and start writing your own CodeceptJS + Nightmare tests!

Posted in Quality Assurance
Share this

Mallory Mooney

Mallory Mooney was a QA Engineer at Modus Create until 2018. She has experience in developing test automation for a broad range of technologies as well as establishing QA processes for projects. She’s an advocate for open source automation tools and believes in building the “right” tests for the job - not just an arbitrary number of them. Aside from her work, Mallory enjoys photography, Texas BBQ, and a good hard cider.
Follow

Related Posts

  • End-to-end Tests Using Watir Webdriver
    End-to-end Tests Using Watir Webdriver

    Functional automated testing tools have come a long way since the days of winrunner, quick…

  • e2e_tests_nightwatch-cucumber
    Writing E2E Tests with Nightwatch-Cucumber

    Nightwatch.js is an awesome, lightweight, Node.js based, End-to-End (E2E) testing framework. Cucumber.js is a JavaScript…

Want more insights to fuel your innovation efforts?

Sign up to receive our monthly newsletter and exclusive content about digital transformation and product development.

What we do

Our services
AI and data
Product development
Design and UX
IT modernization
Platform and MLOps
Developer experience
Security

Our partners
Atlassian
AWS
GitHub
Other partners

Who we are

Our story
Careers
Open source

Our work

Our case studies

Our resources

Blog
Innovation podcast
Guides & playbooks

Connect with us

Get monthly insights on AI adoption

© 2025 Modus Create, LLC

Privacy PolicySitemap
Scroll To Top
  • Services
  • Work
  • Blog
  • Resources
    • Innovation Podcast
    • Guides & Playbooks
  • Who we are
    • Our story
    • Careers
  • Let’s talk
  • EN
  • FR