Lattes

Latte recipes, espresso drinks and coffee how-to

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Fix Playwright Config Values Not Loading in Mocha

· 6 min read · Espresso Drinks

When you run Playwright tests through Mocha and your playwright.config.js or playwright.config.ts values seem to be ignored, the usual cause is that Mocha loads its own test runner configuration and does not automatically read Playwright's config file. This guide walks you through the exact steps to make your Playwright config load correctly with Mocha, covering setup, common pitfalls, and troubleshooting.

What You'll Need

  • A Node.js project with Mocha and Playwright installed (npm install --save-dev mocha @playwright/test).
  • A playwright.config.js (or .ts) file in your project root.
  • Basic familiarity with the command line and your project's package.json.

Step-by-Step

  1. Verify your Playwright config file is in the right place. Playwright looks for playwright.config.js, playwright.config.ts, or playwright.config.mjs in the current working directory. If your config is elsewhere, specify the path with the --config option when running Playwright tests, but note that Mocha does not use that option directly.
  2. Understand that Mocha does not read Playwright config automatically. Mocha is a generic test runner; it runs your test files as JavaScript/TypeScript. Playwright's config is only read when you use Playwright's own test runner (@playwright/test) or when you explicitly load it in your test setup. So, the first step is to decide how you want to pass config values: either by importing the config in your test files, or by using environment variables that your config reads.
  3. Option A: Import the config directly in your test files. In each test file that needs config values, add a line like const config = require('./playwright.config'); (or import config from './playwright.config' for ES modules). Then use config.use, config.projects, etc., in your test code. This is the simplest approach if you only need a few values.
  4. Option B: Create a setup file that loads the config and sets global variables. Create a file, e.g., test/setup.js, that imports the config and assigns it to global.config. Then in your Mocha configuration, use the --require option to load this setup file before tests run. In package.json, set your test script to mocha --require test/setup.js.
  5. Option C: Use environment variables in your config. Modify your playwright.config.js to read from process.env for values that Mocha tests need. For example, baseURL: process.env.BASE_URL || 'http://localhost:3000'. Then, when running Mocha, set the environment variables in your command line or in a .env file loaded via dotenv.
  6. If you are using Playwright's test runner features (like test.use or fixtures) in your Mocha tests, you are mixing two different test runners. Playwright's test function from @playwright/test is not compatible with Mocha's describe/it. You must use Mocha's it blocks and call Playwright's browser APIs directly. In that case, your config values can be accessed via the imported config object, but you cannot use Playwright's test.use inside Mocha.
  7. Set up Mocha to use the config's use options manually. If your config defines browser launch options, you need to read them and pass them to chromium.launch() in your test setup. For example, in a before hook, do browser = await chromium.launch(config.use.launchOptions).
  8. Check your Mocha configuration file. If you have a .mocharc.js or .mocharc.json, ensure it does not override or reset anything related to Playwright. The most common issue is that Mocha's require option is not set to load your config or setup file.
  9. Run a quick test to confirm. Create a simple test that logs a value from your config. If it prints the expected value, your config is loading correctly. If it prints undefined, go to the troubleshooting section below.

Common Mistakes

  • Assuming Mocha reads Playwright config automatically. This is the #1 mistake. Mocha has no knowledge of Playwright's config file.
  • Using Playwright's test function in Mocha. You cannot use import { test } from '@playwright/test' inside a Mocha test file. Mocha expects its own describe and it.
  • Not exporting the config correctly. If your config file uses ES module syntax (export default) but your project is CommonJS, the import will fail. Make sure your package.json has "type": "module" or use require with a .cjs extension.
  • Placing config in a subdirectory. If you run Mocha from a subdirectory, the config might not be found. Always run Mocha from the project root, or specify the config path explicitly in your import.
  • Overwriting config values in test setup. If you set global variables in a setup file, make sure you don't accidentally overwrite them later.

Tips

  • Use a single source of truth. Instead of duplicating config values in your tests, import the config and reference it. That way, changes only need to be made in one place.
  • Consider using Playwright's own test runner if you need advanced features. If you find yourself fighting Mocha to use Playwright's fixtures, parallelization, or reporting, it might be simpler to switch to @playwright/test entirely. It has built-in support for config.
  • Use environment variables for secrets. Never hardcode credentials in your config or test files. Use process.env and a .env file (with dotenv) to keep them out of version control.
  • Log the config values at the start of tests. Add a console.log in your setup to verify what is loaded; this helps debug quickly.

Troubleshooting

  • Config values are undefined. Check if your import path is correct. If you are using require('./playwright.config'), ensure the file exists and exports an object. Also check if the config file itself throws an error when loaded; run node -e "console.log(require('./playwright.config'))" to see.
  • Mocha does not run any tests. If Mocha cannot find test files, check your spec pattern in .mocharc or the command line. Also ensure your test files have .test.js or .spec.js extensions if you set a pattern.
  • Playwright config is loaded but values are overridden. If you are using environment variables, check that they are set correctly. If you are importing the config, make sure you are not mutating the object elsewhere.
  • ES module import errors. If you get Cannot use import statement outside a module, either rename your config to .mjs and use import, or use require with .cjs.
  • Mocha runs but Playwright browser does not launch. This is not a config-loading issue; it is likely that your test code does not call chromium.launch() correctly. Check that you are using the use options from your config when launching.

Frequently Asked Questions

Can I use Playwright's test.use inside Mocha?

No. test.use is part of Playwright's test runner and only works within @playwright/test. In Mocha, you need to manually read config values and pass them to browser launch or page creation.

Why does Mocha ignore my playwright.config.js?

Because Mocha does not know about Playwright's config file. Mocha only reads its own configuration (like .mocharc) and your test code. You must explicitly import or load the Playwright config in your test setup.

Can I use the same config for both Mocha and Playwright Test Runner?

Yes, you can. The config file is just a JavaScript object. You can import it in both contexts. However, Playwright's test runner will use it automatically, while Mocha requires manual import.

What is the best way to pass a base URL to Mocha tests?

Define it in your Playwright config under use.baseURL, then import the config in your test setup and use config.use.baseURL. Alternatively, set an environment variable and read it in both config and tests.

I get an error "Cannot find module './playwright.config'". What should I do?

Check the file path. If your config is playwright.config.ts, you need to compile it to JavaScript or use a loader like ts-node. For simplicity, use a .js config file.

Does Mocha support parallel execution with Playwright?

Mocha has its own parallel mode (via --parallel), but it works at the file level. You can combine it with Playwright by launching multiple browser instances, but you must manage concurrency yourself. Playwright's test runner has built-in parallelism that is more integrated.