fix(test-runner): toHaveURL respect baseURL (#9050)

This commit is contained in:
Max Schmitt 2021-09-21 21:41:24 +02:00 committed by GitHub
parent 9b0e0c2273
commit f9c5279c3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 15 deletions

View File

@ -16,7 +16,6 @@
import { Locator, Page } from '../../..';
import { constructURLBasedOnBaseURL, isString } from '../../utils/utils';
import { currentTestInfo } from '../globals';
import type { Expect } from '../types';
import { toBeTruthy } from './toBeTruthy';
import { toEqual } from './toEqual';
@ -239,10 +238,7 @@ export function toHaveURL(
expected: string | RegExp,
options?: { timeout?: number },
) {
const testInfo = currentTestInfo();
if (!testInfo)
throw new Error(`toHaveURL must be called during the test`);
const baseURL = testInfo.project.use.baseURL;
const baseURL = (page.context() as any)._options.baseURL;
return toMatchText.call(this, 'toHaveURL', page, 'Page', async () => {
return page.url();

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
import http from 'http';
import path from 'path';
import { test, expect, stripAscii } from './playwright-test-fixtures';
test('should support toHaveCount', async ({ runInlineTest }) => {
@ -166,12 +166,8 @@ test('should support toHaveURL', async ({ runInlineTest }) => {
expect(result.exitCode).toBe(1);
});
test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo) => {
test('should support toHaveURL with baseURL from webServer', async ({ runInlineTest }, testInfo) => {
const port = testInfo.workerIndex + 10500;
const server = http.createServer((req: http.IncomingMessage, res: http.ServerResponse) => {
res.end('<html><body>hello</body></html>');
});
await new Promise(resolve => server.listen(port, resolve));
const result = await runInlineTest({
'a.test.ts': `
const { test } = pwt;
@ -189,9 +185,10 @@ test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo
`,
'playwright.config.ts': `
module.exports = {
use: {
baseURL: 'http://localhost:${port}',
}
webServer: {
command: 'node ${JSON.stringify(path.join(__dirname, 'assets', 'simple-server.js'))} ${port}',
port: ${port},
},
};
`,
}, { workers: 1 });
@ -201,7 +198,6 @@ test('should support toHaveURL with baseURL', async ({ runInlineTest }, testInfo
expect(result.passed).toBe(1);
expect(result.failed).toBe(1);
expect(result.exitCode).toBe(1);
await new Promise(resolve => server.close(resolve));
});
test('should support respect expect.timeout', async ({ runInlineTest }) => {