Fix Playwright Config Values Not Loading in Mocha
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
- Verify your Playwright config file is in the right place. Playwright looks for
playwright.config.js,playwright.config.ts, orplaywright.config.mjsin the current working directory. If your config is elsewhere, specify the path with the--configoption when running Playwright tests, but note that Mocha does not use that option directly. - 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. - 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');(orimport config from './playwright.config'for ES modules). Then useconfig.use,config.projects, etc., in your test code. This is the simplest approach if you only need a few values. - 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 toglobal.config. Then in your Mocha configuration, use the--requireoption to load this setup file before tests run. Inpackage.json, set your test script tomocha --require test/setup.js. - Option C: Use environment variables in your config. Modify your
playwright.config.jsto read fromprocess.envfor 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.envfile loaded viadotenv. - If you are using Playwright's test runner features (like
test.useor fixtures) in your Mocha tests, you are mixing two different test runners. Playwright'stestfunction from@playwright/testis not compatible with Mocha'sdescribe/it. You must use Mocha'sitblocks 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'stest.useinside Mocha. - Set up Mocha to use the config's
useoptions manually. If your config defines browser launch options, you need to read them and pass them tochromium.launch()in your test setup. For example, in abeforehook, dobrowser = await chromium.launch(config.use.launchOptions). - Check your Mocha configuration file. If you have a
.mocharc.jsor.mocharc.json, ensure it does not override or reset anything related to Playwright. The most common issue is that Mocha'srequireoption is not set to load your config or setup file. - 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
testfunction in Mocha. You cannot useimport { test } from '@playwright/test'inside a Mocha test file. Mocha expects its owndescribeandit. - 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 yourpackage.jsonhas"type": "module"or userequirewith a.cjsextension. - 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/testentirely. It has built-in support for config. - Use environment variables for secrets. Never hardcode credentials in your config or test files. Use
process.envand a.envfile (withdotenv) to keep them out of version control. - Log the config values at the start of tests. Add a
console.login 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 usingrequire('./playwright.config'), ensure the file exists and exports an object. Also check if the config file itself throws an error when loaded; runnode -e "console.log(require('./playwright.config'))"to see. - Mocha does not run any tests. If Mocha cannot find test files, check your
specpattern in.mocharcor the command line. Also ensure your test files have.test.jsor.spec.jsextensions 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.mjsand useimport, or userequirewith.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 theuseoptions 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.