diff --git a/.eslintignore b/.eslintignore index 7f38955118ae2..f4c17fcc0f25e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -11,3 +11,4 @@ lib/ # in the Puppeteer src, so it trips up the ESLint-TypeScript parser. utils/doclint/generate_types/test/test.ts vendor/ +web-test-runner.config.mjs diff --git a/.eslintrc.js b/.eslintrc.js index 84821c4205731..16f41fd3f5b6b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -125,6 +125,17 @@ module.exports = { "default": "array-simple" }] } + }, + { + "files": ["test-browser/**/*.js"], + "parserOptions": { + "sourceType": "module" + }, + "env": { + "es6": true, + "browser": true, + "es2020": true + }, } ] }; diff --git a/.travis.yml b/.travis.yml index 1d50febba49e1..b8b46964506c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -57,6 +57,13 @@ jobs: script: - travis_retry npm run unit + - node_js: "12.16.3" + name: 'Browser tests: Linux/Chromium' + env: + - CHROMIUM=true + script: + - travis_retry npm run test-browser + # This bot runs all the extra checks that aren't the main Puppeteer unit tests - node_js: "10.19.0" name: 'Extra tests: Linux/Chromium' diff --git a/package.json b/package.json index d5ef8a04500f0..5ce4ed5a7e7a7 100644 --- a/package.json +++ b/package.json @@ -8,12 +8,14 @@ "node": ">=10.18.1" }, "scripts": { + "test-browser": "wtr", + "test-browser-watch": "wtr --watch", "unit": "npm run tsc-cjs && mocha --config mocha-config/puppeteer-unit-tests.js", "unit-with-coverage": "cross-env COVERAGE=1 npm run unit", "assert-unit-coverage": "cross-env COVERAGE=1 mocha --config mocha-config/coverage-tests.js", "funit": "PUPPETEER_PRODUCT=firefox npm run unit", "debug-unit": "node --inspect-brk test/test.js", - "test": "npm run tsc && npm run lint --silent && npm run unit-with-coverage && npm run test-types", + "test": "npm run tsc && npm run lint --silent && npm run unit-with-coverage && npm run test-browser && npm run test-types", "prepare": "node typescript-if-required.js", "prepublishOnly": "npm run tsc", "dev-install": "npm run tsc && node install.js", @@ -69,6 +71,7 @@ "@types/ws": "^7.2.4", "@typescript-eslint/eslint-plugin": "^2.28.0", "@typescript-eslint/parser": "^2.28.0", + "@web/test-runner": "^0.6.40", "commonmark": "^0.28.1", "cross-env": "^5.0.5", "dependency-cruiser": "^9.7.0", diff --git a/test-browser/debug.spec.js b/test-browser/debug.spec.js new file mode 100644 index 0000000000000..971d3a5346ce0 --- /dev/null +++ b/test-browser/debug.spec.js @@ -0,0 +1,65 @@ +/** + * Copyright 2020 Google Inc. All rights reserved. + * + * 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 { debug } from '../lib/esm/puppeteer/common/Debug.js'; +import expect from '../node_modules/expect/build-es5/index.js'; + +describe('debug', () => { + let originalLog; + let logs; + beforeEach(() => { + originalLog = console.log; + logs = []; + console.log = (...args) => { + logs.push(args); + }; + }); + + afterEach(() => { + console.log = originalLog; + }); + + it('should return a function', async () => { + expect(debug('foo')).toBeInstanceOf(Function); + }); + + it('does not log to the console if __PUPPETEER_DEBUG global is not set', async () => { + const debugFn = debug('foo'); + debugFn('lorem', 'ipsum'); + + expect(logs.length).toEqual(0); + }); + + it('logs to the console if __PUPPETEER_DEBUG global is set to *', async () => { + globalThis.__PUPPETEER_DEBUG = '*'; + const debugFn = debug('foo'); + debugFn('lorem', 'ipsum'); + + expect(logs.length).toEqual(1); + expect(logs).toEqual([['foo:', 'lorem', 'ipsum']]); + }); + + it('logs only messages matching the __PUPPETEER_DEBUG prefix', async () => { + globalThis.__PUPPETEER_DEBUG = 'foo'; + const debugFoo = debug('foo'); + const debugBar = debug('bar'); + debugFoo('a'); + debugBar('b'); + + expect(logs.length).toEqual(1); + expect(logs).toEqual([['foo:', 'a']]); + }); +}); diff --git a/web-test-runner.config.js b/web-test-runner.config.js new file mode 100644 index 0000000000000..4777a2b5a2b00 --- /dev/null +++ b/web-test-runner.config.js @@ -0,0 +1,30 @@ +/** + * Copyright 2020 Google Inc. All rights reserved. + * + * 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. + */ + +module.exports = { + files: ['test-browser/**/*.spec.js'], + plugins: [ + { + // turn expect UMD into an es module + name: 'esmify-expect', + transform(context) { + if (context.path === '/node_modules/expect/build-es5/index.js') { + return `const module = {}; const exports = {};\n${context.body};\n export default module.exports;`; + } + }, + }, + ], +};