How to Fix “did not expect test() to be called here” in Playwright
When test() is called while the Playwright configuration file (playwright.config.ts) is being loaded, running the E2E tests with npx playwright test does not start a single test. It stops with Playwright Test did not expect test() to be called here. For example, this happens when you export a value such as a base URL from a test file and import it into the configuration file. Moving that value into a separate file that does not call test() removes the error.
I reproduced the error in a test environment and included the message as it was printed. I also checked how to find the source of the error and how to fix it, and when importing from a fixtures file causes the error and when it does not.
The test environment was Ubuntu 24.04, Node.js 22.23.5, @playwright/test 1.63.0 (Chromium 158.0.8127.36, headless), and TypeScript 5.9.3. The test pages were served by a small server built with Node.js's http module (it returns an HTML page with only one heading), started with webServer. I did not test the other causes listed in the message: having two copies of @playwright/test, and calling test() inside an async test.describe(). I also did not test Windows, macOS, or running from the VS Code extension.
Reproducing the error
In the test file, I made the base URL a constant and exported it. The test itself only opens the top page and checks the heading.
// tests/home.spec.ts
import { test, expect } from '@playwright/test';
export const BASE_URL = 'http://localhost:3000';
test('トップページの見出しが出る', async ({ page }) => {
await page.goto('/');
await expect(page.getByRole('heading', { level: 1 })).toHaveText('猫の手カウンター');
});
In the configuration file, I imported this constant to use it for baseURL and the url of webServer, so that the same URL did not have to be written twice.
// playwright.config.ts
import { defineConfig } from '@playwright/test';
import { BASE_URL } from './tests/home.spec';
export default defineConfig({
testDir: './tests',
reporter: 'list',
use: {
baseURL: BASE_URL,
browserName: 'chromium',
},
webServer: {
command: 'node server.mjs',
url: BASE_URL,
},
});
When I ran this, it stopped before any test started. The exit code was 1. (I replaced the path with /path/to/app and omitted part of the stack trace.)
$ npx playwright test
Error: Playwright Test did not expect test() to be called here.
Most common reasons include:
- You are calling test() in a configuration file.
- You are calling test() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
when one of the dependencies in your package.json depends on @playwright/test.
- You are calling test() from an async test.describe() block. Only sync ones are supported.
at _TestTypeImpl._currentSuite (/path/to/app/node_modules/playwright/lib/common/index.js:2224:13)
at _TestTypeImpl._createTest (/path/to/app/node_modules/playwright/lib/common/index.js:2238:24)
at /path/to/app/node_modules/playwright/lib/common/index.js:1154:12
at Object.<anonymous> (/path/to/app/tests/home.spec.ts:5:5)
…(中略)
at Object.<anonymous> (/path/to/app/playwright.config.ts:2:1)
…(中略)
at loadUserConfig (/path/to/app/node_modules/playwright/lib/common/index.js:1268:52)
…(後略)
The message means that Playwright Test does not expect test() to be called at this point. It then lists four common causes: calling test() in the configuration file, calling it in a file that the configuration file imports, having two versions of @playwright/test, and calling test() from an async test.describe() block. This case matches the second one.
Running npx playwright test --list, which only lists the tests, stopped with the same message. Passing a file such as npx playwright test tests/home.spec.ts, or filtering by test name with -g "見出し", also gave the same result.
Finding the file that causes the error from the stack trace
The stack trace below the message shows which file to fix. If you skip the node_modules and node:internal lines, two lines remain:
tests/home.spec.ts:5:5: wheretest()was calledplaywright.config.ts:2:1: where that file was loaded
Among the skipped node_modules lines, loadUserConfig near the bottom is the clue. loadUserConfig is the function that loads the configuration file, so the error happened while the configuration file was being loaded. Reading the trace from the bottom up: the import on line 2 of the configuration file loaded tests/home.spec.ts, and the test() call on line 5 of that file raised the error.
I checked the Playwright 1.63.0 source to see why calling test() while the configuration file is loading causes this error. When test() is called, it looks for "the test file currently being loaded" and registers the test there. This current test file is set only while Playwright loads a test file, and it is cleared when loading finishes. While the configuration file is loading, nothing is set, so test() has no place to register the test and raises this error.

Moving the shared value into a file without test()
Put any value you want to use in both the configuration file and the test files in a file that does not call test(). Here I created shared/urls.ts and wrote only BASE_URL in it.
// shared/urls.ts
export const BASE_URL = 'http://localhost:3000';
I changed the configuration file to import from this shared/urls.ts. Only the second line changed.
// playwright.config.ts
import { defineConfig } from '@playwright/test';
import { BASE_URL } from './shared/urls';
I removed the export const BASE_URL line from the test file. The test uses page.goto('/'), and the URL it opens is determined by baseURL in the configuration file, so the test file does not need to read BASE_URL.
When I ran this, the error was gone and the test passed.
$ npx playwright test
Running 1 test using 1 worker
✓ 1 tests/home.spec.ts:3:5 › トップページの見出しが出る (221ms)
1 passed (1.1s)
I placed shared/ outside testDir (./tests) so that it is clear from the location that it is a different file from the tests.
When importing from a fixtures file
I also tested a case where BASE_URL is in tests/fixtures.ts, the file that defines the fixtures, and the configuration file imports it from there. The import line in the configuration file is shown below. Everything else is the same as the first setup.
// playwright.config.ts
import { BASE_URL } from './tests/fixtures';
// tests/fixtures.ts
import { test as base } from '@playwright/test';
export const BASE_URL = 'http://localhost:3000';
export const test = base.extend<{ heading: string }>({
heading: async ({}, use) => {
await use('猫の手カウンター');
},
});
export { expect } from '@playwright/test';
In this form, the tests passed without an error. test.extend() only creates a new test and does not register a test. As the official fixtures documentation explains, a test created with test.extend() can be imported and used from several test files.
The same kind of error can also happen without calling test() directly. When I added a top-level test.beforeEach() to this file, loading the configuration file stopped with an error.
// Added at the end of tests/fixtures.ts
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
The message was as follows (I omitted the stack trace):
$ npx playwright test
Error: Playwright Test did not expect test.beforeEach() to be called here.
Most common reasons include:
- You are calling test.beforeEach() in a configuration file.
- You are calling test.beforeEach() in a file that is imported by the configuration file.
- You have two different versions of @playwright/test. This usually happens
when one of the dependencies in your package.json depends on @playwright/test.
- You are calling test.beforeEach() from an async test.describe() block. Only sync ones are supported.
…(後略)
The test() part of the message changes to the name of the function that was called. In the Playwright 1.63.0 source, test.describe(), hooks such as test.beforeEach(), test.describe.configure(), and test.use() are checked in the same way, in addition to test(). All of them are meant to be called only while a test file is being loaded.
In this case too, changing the configuration file's import to shared/urls.ts made the tests pass. In tests/fixtures.ts, I changed the BASE_URL line to export { BASE_URL } from '../shared/urls';. The test.beforeEach() stayed in place, but when the file was imported by a test file and loaded as part of that, it did not cause an error. What I checked here is only that the error goes away. I am not recommending placing hooks in a fixtures file.
When test() is called in the configuration file
Calling test() directly in the configuration file gave the same message. The top of the configuration file looked like this:
// playwright.config.ts
import { defineConfig, test } from '@playwright/test';
test('設定ファイルの中のテスト', async () => {});
The difference was in the stack trace. The place where test() was called was playwright.config.ts:3:5, which points to line 3 of the configuration file.
$ npx playwright test
Error: Playwright Test did not expect test() to be called here.
…(中略)
at Object.<anonymous> (/path/to/app/playwright.config.ts:3:5)
…(後略)
A test() written in the configuration file does not run as a test. Write tests in the test files inside testDir.





