playwright: [BUG] baseURL type definition error
Context:
- Playwright Version: “@playwright/test”: “1.22.2”,
- Operating System: Windows
- Node.js version: v16.15.1
Describe the bug
I’m using this config:
const baseURL = "someURL";
const config: PlaywrightTestConfig = {
// ...
use: {
baseURL,
// ...
},
// ...
};
and in my tests:
test.describe.parallel("test", () => {
test.beforeEach(async ({ page, baseURL }) => {
await page.goto(baseURL); // typescript error, see below
});
// ...
})
but this is the Typscript error:
Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.ts(2345)
And it is understandable.
How to fix this?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 20 (3 by maintainers)
@ville-nvt, @dgozman this doesn’t work.
If I use
await page.goto('./foo/bar')orawait page.goto('/')it throws with:even if
baseURL === "http://localhost:3000".I think we should re-open this.
I am still facing this issue
As @ville-nvt suggests, you don’t usually need to reference
baseURLdirectly, you use relative urls instead.If you really need
baseURL, silence TypeScript by adding an exclamation mark.I actually resolve it. I was not providing the config reference in the command when running a single file
I might be wrong because I don’t know TypeScript, but isn’t this code wrong?
In the documentation,
baseURLconfiguration expects a string, but in the code below, at the top we set a custom constbaseURLand in theuseblock, isn’t thebaseURLwrong? Like, there, thebaseURLis the configuration option that is expecting= some variable or stringfrom the top constant. Or using this way will spread/destruct the top constant?I’m sure I explained correctly my thoughts.
For me, it should be like this:
The baseUrl is not working when navigating to “/” in global setup files. It works only in the tests.