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

View File

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