Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(test runner): do not install custom loader hook for '.cjs' (#35962)
Dmitry Gozman8eda25c9284c
diff --git a/packages/playwright/ThirdPartyNotices.txt b/packages/playwright/ThirdPartyNotices.txtindex bce8bb1050..e6c4968b61 100644--- a/packages/playwright/ThirdPartyNotices.txt+++ b/packages/playwright/ThirdPartyNotices.txt@@ -125,7 +125,6 @@ This project incorporates components from the projects listed below. The origina- [email protected] (https://github.com/alexeyraspopov/picocolors)- [email protected] (https://github.com/alexeyraspopov/picocolors)- [email protected] (https://github.com/micromatch/picomatch)-- [email protected] (https://github.com/danez/pirates)- [email protected] (https://github.com/jestjs/jest)- [email protected] (https://github.com/facebook/react)- [email protected] (https://github.com/paulmillr/readdirp)@@ -3631,32 +3630,6 @@ THE SOFTWARE.=========================================END OF [email protected] AND INFORMATION-%% [email protected] NOTICES AND INFORMATION BEGIN HERE-=========================================-MIT License--Copyright (c) 2016-2018 Ari Porad--Permission is hereby granted, free of charge, to any person obtaining a copy-of this software and associated documentation files (the "Software"), to deal-in the Software without restriction, including without limitation the rights-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell-copies of the Software, and to permit persons to whom the Software is-furnished to do so, subject to the following conditions:--The above copyright notice and this permission notice shall be included in all-copies or substantial portions of the Software.--THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE-SOFTWARE.-=========================================-END OF [email protected] AND INFORMATION-%% [email protected] NOTICES AND INFORMATION BEGIN HERE=========================================MIT License@@ -4006,6 +3979,6 @@ END OF [email protected] AND INFORMATIONSUMMARY BEGIN HERE=========================================-Total Packages: 137+Total Packages: 136=========================================END OF SUMMARY\ No newline at end of filediff --git a/packages/playwright/bundles/utils/package-lock.json b/packages/playwright/bundles/utils/package-lock.jsonindex 5fd1392d33..95f4c43dc1 100644--- a/packages/playwright/bundles/utils/package-lock.json+++ b/packages/playwright/bundles/utils/package-lock.json@@ -12,7 +12,6 @@"enquirer": "2.3.6","get-east-asian-width": "1.3.0","json5": "2.2.3",- "pirates": "4.0.4","source-map-support": "0.5.21","stoppable": "1.1.0"},@@ -238,14 +237,6 @@"url": "https://github.com/sponsors/jonschlinkert"}},- "node_modules/pirates": {- "version": "4.0.4",- "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.4.tgz",- "integrity": "sha512-ZIrVPH+A52Dw84R0L3/VS9Op04PuQ2SEoJL6bkshmiTic/HldyW9Tf7oH5mhJZBK7NmDx27vSMrYEXPXclpDKw==",- "engines": {- "node": ">= 6"- }- },"node_modules/readdirp": {"version": "3.6.0","resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",@@ -443,11 +434,6 @@"resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.1.tgz","integrity": "sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA=="},- "pirates": {- "version": "4.0.4",- "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.4.tgz",- "integrity": "sha512-ZIrVPH+A52Dw84R0L3/VS9Op04PuQ2SEoJL6bkshmiTic/HldyW9Tf7oH5mhJZBK7NmDx27vSMrYEXPXclpDKw=="- },"readdirp": {"version": "3.6.0","resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz",diff --git a/packages/playwright/bundles/utils/package.json b/packages/playwright/bundles/utils/package.jsonindex 08dd4267c6..c6f39dd86f 100644--- a/packages/playwright/bundles/utils/package.json+++ b/packages/playwright/bundles/utils/package.json@@ -7,7 +7,6 @@"enquirer": "2.3.6","get-east-asian-width": "1.3.0","json5": "2.2.3",- "pirates": "4.0.4","source-map-support": "0.5.21","stoppable": "1.1.0"},diff --git a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts b/packages/playwright/bundles/utils/src/utilsBundleImpl.tsindex 6928295763..6fa08b5aef 100644--- a/packages/playwright/bundles/utils/src/utilsBundleImpl.ts+++ b/packages/playwright/bundles/utils/src/utilsBundleImpl.ts@@ -19,9 +19,6 @@import json5Library from 'json5';export const json5 = json5Library;-import * as piratesLibrary from 'pirates';-export const pirates = piratesLibrary;-import sourceMapSupportLibrary from 'source-map-support';export const sourceMapSupport = sourceMapSupportLibrary;diff --git a/packages/playwright/src/third_party/pirates.ts b/packages/playwright/src/third_party/pirates.tsnew file mode 100644index 0000000000..26ee803809--- /dev/null+++ b/packages/playwright/src/third_party/pirates.ts@@ -0,0 +1,59 @@+/**+ * MIT License+ *+ * Copyright (c) 2016-2018 Ari Porad+ * Modifications copyright (c) Microsoft Corporation.+ *+ * Permission is hereby granted, free of charge, to any person obtaining a copy+ * of this software and associated documentation files (the "Software"), to deal+ * in the Software without restriction, including without limitation the rights+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+ * copies of the Software, and to permit persons to whom the Software is+ * furnished to do so, subject to the following conditions:+ *+ * The above copyright notice and this permission notice shall be included in all+ * copies or substantial portions of the Software.+ *+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+ * SOFTWARE.+ */++import Module from 'module';+import path from 'path';++export function addHook(transformHook: (code: string, filename: string) => string, shouldTransform: (filename: string) => boolean, extensions: string[]) {+ // This is a shortened and slightly changed version of https://github.com/danez/pirates.+ //+ // We cannot use pirates directly due to https://github.com/microsoft/playwright/issues/35812.+ // If we overwrite the '.cjs', the following code does not run because there is a custom loader defined:+ // https://github.com/nodejs/node/blob/b1973550e09d5a2a07c70be5de6e3ae4722ad230/lib/internal/modules/esm/translators.js#L397-L403+ //+ // Here we rely on the default '.js' loader to handle '.cjs' files.+ const extensionsToOverwrite = extensions.filter(e => e !== '.cjs');+ const allSupportedExtensions = new Set(extensions);+ const loaders = (Module as any)._extensions;+ const jsLoader = loaders['.js'];+ for (const extension of extensionsToOverwrite) {+ const originalLoader = loaders[extension] || jsLoader;+ function newLoader(this: any, mod: any, filename: string, ...loaderArgs: any[]) {+ if (allSupportedExtensions.has(path.extname(filename)) && shouldTransform(filename)) {+ const oldCompile = mod._compile;+ function newCompile(this: any, code: string, file: string, ...ignoredArgs: any[]) {+ // Note: we do not pass |args| downstream to make sure "esm modules" loaded through here+ // are treated as "commonjs", for example for ".mjs" files.+ // In theory, we should fix this, but it is a breaking change, even for playwright's own tests.+ mod._compile = oldCompile;+ return oldCompile.call(this, transformHook(code, filename), file);+ }+ mod._compile = newCompile;+ }+ originalLoader.call(this, mod, filename, ...loaderArgs);+ }+ loaders[extension] = newLoader;+ }+}diff --git a/packages/playwright/src/transform/DEPS.list b/packages/playwright/src/transform/DEPS.listindex d32b89e112..91c6162c12 100644--- a/packages/playwright/src/transform/DEPS.list+++ b/packages/playwright/src/transform/DEPS.list@@ -1,5 +1,6 @@[*]../util.ts../utilsBundle.ts+../third_party/pirates.ts../third_party/tsconfig-loader.ts../common/globals.tsdiff --git a/packages/playwright/src/transform/transform.ts b/packages/playwright/src/transform/transform.tsindex 595588d49b..1719fd504c 100644--- a/packages/playwright/src/transform/transform.ts+++ b/packages/playwright/src/transform/transform.ts@@ -22,8 +22,9 @@ import url from 'url';import { loadTsConfig } from '../third_party/tsconfig-loader';import { createFileMatcher, fileIsModule, resolveImportSpecifierAfterMapping } from '../util';-import { pirates, sourceMapSupport } from '../utilsBundle';+import { sourceMapSupport } from '../utilsBundle';import { belongsToNodeModules, currentFileDepsCollector, getFromCompilationCache, installSourceMapSupport } from './compilationCache';+import { addHook } from '../third_party/pirates';import type { BabelPlugin, BabelTransformFunction } from './babelBundle';import type { Location } from '../../types/testReporter';@@ -291,11 +292,10 @@ function installTransformIfNeeded() {}(Module as any)._resolveFilename = resolveFilename;- pirates.addHook((code: string, filename: string) => {- if (!shouldTransform(filename))- return code;+ // Hopefully, one day we can migrate to synchronous loader hooks instead, similar to our esmLoader...+ addHook((code, filename) => {return transformHook(code, filename).code;- }, { exts: ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts', '.cjs', '.cts'] });+ }, shouldTransform, ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.mts', '.cjs', '.cts']);}const collectCJSDependencies = (module: Module, dependencies: Set<string>) => {diff --git a/packages/playwright/src/utilsBundle.ts b/packages/playwright/src/utilsBundle.tsindex d0e2a37e77..1e61ccc5ca 100644--- a/packages/playwright/src/utilsBundle.ts+++ b/packages/playwright/src/utilsBundle.ts@@ -15,7 +15,6 @@*/export const json5: typeof import('../bundles/utils/node_modules/json5/lib') = require('./utilsBundleImpl').json5;-export const pirates: typeof import('../bundles/utils/node_modules/pirates') = require('./utilsBundleImpl').pirates;export const sourceMapSupport: typeof import('../bundles/utils/node_modules/@types/source-map-support') = require('./utilsBundleImpl').sourceMapSupport;export const stoppable: typeof import('../bundles/utils/node_modules/@types/stoppable') = require('./utilsBundleImpl').stoppable;export const enquirer: typeof import('../bundles/utils/node_modules/enquirer') = require('./utilsBundleImpl').enquirer;diff --git a/tests/playwright-test/loader.spec.ts b/tests/playwright-test/loader.spec.tsindex ba665ee3f4..4372b25245 100644--- a/tests/playwright-test/loader.spec.ts+++ b/tests/playwright-test/loader.spec.ts@@ -1085,7 +1085,7 @@ test('should remove import css', async ({ runInlineTest }) => {expect(result.passed).toBe(1);});-test.fixme('should dynamically import re-exported cjs namespace', {+test('should dynamically import re-exported cjs namespace', {annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/35812' },}, async ({ runInlineTest }) => {const result = await runInlineTest({