From 22bc16ce7dc206640da353069aa53001378c49d1 Mon Sep 17 00:00:00 2001 From: Ross Wollman Date: Wed, 27 Apr 2022 18:34:18 -0700 Subject: [PATCH] test: add plugin event order spec (#13802) --- tests/playwright-test/plugins.spec.ts | 75 +++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 tests/playwright-test/plugins.spec.ts diff --git a/tests/playwright-test/plugins.spec.ts b/tests/playwright-test/plugins.spec.ts new file mode 100644 index 0000000000..90a28ec526 --- /dev/null +++ b/tests/playwright-test/plugins.spec.ts @@ -0,0 +1,75 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import fs from 'fs'; +import { test, expect } from './playwright-test-fixtures'; + +test('event order', async ({ runInlineTest }, testInfo) => { + const log = testInfo.outputPath('logs.txt'); + const result = await runInlineTest({ + 'log.ts': ` + import { appendFileSync } from 'fs'; + const log = (...args) => appendFileSync('${log}', args.join(' ') + '\\n'); + export default log; + `, + 'test.spec.ts': ` + import log from './log'; + const { test } = pwt; + test('it works', async ({baseURL}) => { + log('baseURL', baseURL); + }); + `, + 'playwright.config.ts': ` + import { myPlugin } from './plugin.ts'; + module.exports = { + plugins: [ + myPlugin('a'), + myPlugin('b'), + ], + globalSetup: 'globalSetup.ts', + globalTeardown: 'globalTeardown.ts', + }; + `, + 'globalSetup.ts': ` + import log from './log'; + const setup = () => log('globalSetup'); + export default setup; + `, + 'globalTeardown.ts': ` + const teardown = () => log('globalTeardown'); + export default teardown; + `, + 'plugin.ts': ` + import log from './log'; + export const myPlugin = (name: string) => ({ + configure: async (config) => { config.use = (config.use || {}); config.use.baseURL = (config.use.baseURL || '') + name + ' | '; }, + setup: async () => log(name, 'setup'), + teardown: async () => log(name, 'teardown'), + }); + `, + }); + expect(result.exitCode).toBe(0); + expect(result.passed).toBe(1); + const logLines = await fs.promises.readFile(log, 'utf8'); + expect(logLines.split('\n')).toEqual([ + 'a setup', + 'b setup', + 'globalSetup', + 'baseURL a | b | ', + 'b teardown', + 'a teardown', + '', + ]); +});