Skip to content

Commit

Permalink
wip: merge mocha and esm core branches
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark committed Jun 29, 2022
1 parent 7ba8fc1 commit ce17c7d
Show file tree
Hide file tree
Showing 122 changed files with 2,500 additions and 2,470 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

# Ex: bash jest-retry.sh yarn test-viewer
# Ex: bash test-retry.sh yarn test-viewer

##
# @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ jobs:
# Run tests that require headfull Chrome.
- run: sudo apt-get install xvfb
- name: yarn test-clients
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/jest-retry.sh yarn test-clients
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/test-retry.sh yarn test-clients
- name: yarn test-docs
run: xvfb-run --auto-servernum yarn test-docs
- name: yarn test-viewer
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/jest-retry.sh yarn test-viewer
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/test-retry.sh yarn test-viewer
- name: yarn test-treemap
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/jest-retry.sh yarn test-treemap
run: xvfb-run --auto-servernum bash $GITHUB_WORKSPACE/.github/scripts/test-retry.sh yarn test-treemap

- run: yarn diff:sample-json
- run: yarn diff:flow-sample-json
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/cron-weekly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ jobs:
node-version: 14.x
- run: yarn --frozen-lockfile

- run: yarn jest --testMatch="**/third-party/chromium-synchronization/*-test.js"
- run: yarn mocha --testMatch= 'third-party/chromium-synchronization/*-test.js'
2 changes: 2 additions & 0 deletions .github/workflows/devtools.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ jobs:
- name: Download DevTools Frontend
run: bash $GITHUB_WORKSPACE/lighthouse/lighthouse-core/test/chromium-web-tests/download-devtools.sh

- run: npx patch-package # TODO: remove when mocha replaces jest
working-directory: ${{ github.workspace }}/lighthouse
- name: Roll Lighthouse + build DevTools
run: bash $GITHUB_WORKSPACE/lighthouse/lighthouse-core/test/chromium-web-tests/roll-devtools.sh

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
run: xvfb-run --auto-servernum yarn unit:ci

# Only gather coverage on latest node, where c8 is the most accurate.
- name: yarn unit-core
- name: yarn unit:cicoverage
if: ${{ matrix.node == env.LATEST_NODE }}
run: |
xvfb-run --auto-servernum yarn unit-core
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lcov.info
lighthouse-cli/results
results.html
last-run-results.html
/lighthouse-plugin-simple

*.trace.json
*.devtoolslog.json
Expand Down
5 changes: 4 additions & 1 deletion build/test/plugins/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

module.exports = {
env: {
jest: true,
mocha: true,
},
globals: {
expect: true,
},
};
5 changes: 4 additions & 1 deletion clients/test/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

module.exports = {
env: {
jest: true,
mocha: true,
},
globals: {
expect: true,
},
};
4 changes: 2 additions & 2 deletions clients/test/extension/popup-test-pptr.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Lighthouse chrome popup', function() {
let page;
const pageErrors = [];

beforeAll(async function() {
before(async function() {
// start puppeteer
browser = await puppeteer.launch({
headless: false,
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('Lighthouse chrome popup', function() {
await page.goto('file://' + path.join(lighthouseExtensionPath, 'popup.html'), {waitUntil: 'networkidle2'});
}, 10 * 1000);

afterAll(async () => {
after(async () => {
if (browser) {
await browser.close();
}
Expand Down
14 changes: 7 additions & 7 deletions clients/test/lightrider/lightrider-entry-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';

import {jest} from '@jest/globals';
import jestMock from 'jest-mock';
import {strict as assert} from 'assert';
import {runLighthouseInLR} from '../../lightrider/lightrider-entry.js';
import {Runner} from '../../../lighthouse-core/runner.js';
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('lightrider-entry', () => {
});

it('specifies the channel as lr', async () => {
const runStub = jest.spyOn(Runner, 'gather');
const runStub = jestMock.spyOn(Runner, 'gather');

const mockConnection = {};
const url = 'https://example.com';
Expand All @@ -71,7 +71,7 @@ describe('lightrider-entry', () => {
});

it('uses the desktop config preset when device is desktop', async () => {
const runStub = jest.spyOn(Runner, 'gather');
const runStub = jestMock.spyOn(Runner, 'gather');

const mockConnection = {};
const url = 'https://example.com';
Expand All @@ -85,7 +85,7 @@ describe('lightrider-entry', () => {
});

it('uses the mobile config preset when device is mobile', async () => {
const runStub = jest.spyOn(Runner, 'gather');
const runStub = jestMock.spyOn(Runner, 'gather');

const mockConnection = {};
const url = 'https://example.com';
Expand All @@ -99,7 +99,7 @@ describe('lightrider-entry', () => {
});

it('overrides the default config when one is provided', async () => {
const runStub = jest.spyOn(Runner, 'gather');
const runStub = jestMock.spyOn(Runner, 'gather');

const mockConnection = {};
const url = 'https://example.com';
Expand Down Expand Up @@ -127,8 +127,8 @@ describe('lightrider-entry', () => {
});

it('exposes artifacts when logAssets is true', async () => {
Runner.gather = jest.fn();
Runner.audit = jest.fn(Runner.audit).mockReturnValue(Promise.resolve({
Runner.gather = jestMock.fn();
Runner.audit = jestMock.fn(Runner.audit).mockReturnValue(Promise.resolve({
lhr: {},
artifacts: {
Artifact: new Error('some error'),
Expand Down
5 changes: 4 additions & 1 deletion docs/recipes/auth/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
module.exports = {
extends: '../../../.eslintrc.cjs',
env: {
jest: true,
mocha: true,
},
globals: {
expect: true,
},
rules: {
'new-cap': 0,
Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/integration-test/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

module.exports = {
env: {
jest: true,
mocha: true,
},
};
14 changes: 0 additions & 14 deletions docs/recipes/integration-test/docs-jest.config.js

This file was deleted.

9 changes: 4 additions & 5 deletions docs/recipes/integration-test/example-lh-auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,22 @@
/* eslint-disable new-cap */

/**
* @fileoverview Example Jest tests for demonstrating how to run Lighthouse on an authenticated
* @fileoverview Example Mocha tests for demonstrating how to run Lighthouse on an authenticated
* page as integration tests. See docs/recipes/auth/README.md for more.
*/

/** @typedef {import('lighthouse/types/lhr')} LH */

const puppeteer = require('puppeteer');
const lighthouse = require('lighthouse');
const {expect} = require('expect');
const server = require('../auth/server/server.js');
const {login, logout} = require('../auth/example-lh-auth.js');

const CHROME_DEBUG_PORT = 8042;
const SERVER_PORT = 10632;
const ORIGIN = `http://localhost:${SERVER_PORT}`;

jest.setTimeout(30000);

// Provide a nice way to assert a score for a category.
// Note, you could just use `expect(lhr.categories.seo.score).toBeGreaterThanOrEqual(0.9)`,
// but by using a custom matcher a better error report is generated.
Expand Down Expand Up @@ -78,7 +77,7 @@ describe('my site', () => {
/** @type {import('puppeteer').Page} */
let page;

beforeAll(async () => {
before(async () => {
await new Promise(resolve => server.listen(SERVER_PORT, resolve));
browser = await puppeteer.launch({
args: [`--remote-debugging-port=${CHROME_DEBUG_PORT}`],
Expand All @@ -87,7 +86,7 @@ describe('my site', () => {
});
});

afterAll(async () => {
after(async () => {
await browser.close();
await new Promise(resolve => server.close(resolve));
});
Expand Down
4 changes: 2 additions & 2 deletions docs/recipes/integration-test/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"private": true,
"scripts": {
"test": "jest example-lh-auth.test.js"
"test": "mocha --timeout 30000 example-lh-auth.test.js"
},
"dependencies": {
"jest": "^24.9.0",
"mocha": "^10.0.0",
"lighthouse": "file:../../../dist/lighthouse.tgz"
}
}
4 changes: 2 additions & 2 deletions docs/recipes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"private": true,
"scripts": {
"install-all": "rm -rf /tmp/.junk && rm -f {auth,custom-gatherer-puppeteer,integration-test}/yarn.lock && yarn --cwd auth/ --cache-folder /tmp/.junk && yarn --cwd integration-test/ --cache-folder /tmp/.junk && yarn --cwd custom-gatherer-puppeteer/ --cache-folder /tmp/.junk",
"integration-test": "yarn --cwd integration-test/ jest --config-path=docs-jest.config.js",
"integration-test": "yarn --cwd integration-test/ test",
"custom-gatherer-puppeteer-test": "yarn --cwd custom-gatherer-puppeteer/ test",
"test": "yarn install-all && yarn integration-test && yarn custom-gatherer-puppeteer-test"
"test": "sh test-recipes.sh"
}
}
21 changes: 21 additions & 0 deletions docs/recipes/test-recipes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

##
# @license Copyright 2022 The Lighthouse Authors. 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.
##

set -eux

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# Locally, make sure dist/lighthouse.tgz is the latest code.
if [ -z "${CI:-}" ]; then
yarn --cwd ../.. build-pack
fi

yarn install-all
yarn integration-test
yarn custom-gatherer-puppeteer-test
10 changes: 6 additions & 4 deletions flow-report/test/common-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
* 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 {jest} from '@jest/globals';
import jestMock from 'jest-mock';
import {act, render} from '@testing-library/preact';

// @ts-ignore: File is not under 'rootDir'
import {timers} from '../../lighthouse-core/test/test-env/fake-timers.js';
import {FlowStepThumbnail} from '../src/common';

let lhr: LH.Result;

jest.useFakeTimers();
timers.useFakeTimers();

describe('FlowStepThumbnail', () => {
beforeEach(() => {
global.console.warn = jest.fn();
global.console.warn = jestMock.fn();

lhr = {
gatherMode: 'navigation',
Expand Down Expand Up @@ -97,7 +99,7 @@ describe('FlowStepThumbnail', () => {

expect(thumbnail.src).toContain('frame1');
await act(() => {
jest.advanceTimersByTime(501);
timers.advanceTimersByTime(501);
});
expect(thumbnail.src).toContain('frame2');
});
Expand Down
11 changes: 4 additions & 7 deletions flow-report/test/flow-report-pptr-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,34 @@
* 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 {jest} from '@jest/globals';
import puppeteer, {Browser, Page} from 'puppeteer';

import ReportGenerator from '../../report/generator/report-generator.js';
import {flowResult} from './sample-flow';

jest.setTimeout(35_000);

describe('Lighthouse Flow Report', () => {
console.log('\n✨ Be sure to have recently run this: yarn build-report');

let browser: Browser;
let page: Page;
const pageErrors: Error[] = [];

beforeAll(async () => {
before(async () => {
browser = await puppeteer.launch({
headless: true,
});
page = await browser.newPage();
page.on('pageerror', pageError => pageErrors.push(pageError));
});

afterAll(async () => {
after(async () => {
if (pageErrors.length > 0) console.error(pageErrors);

await browser.close();
});

describe('Renders the flow report', () => {
beforeAll(async () => {
before(async () => {
const html = ReportGenerator.generateFlowReportHtml(flowResult);
await page.setContent(html);
});
Expand All @@ -43,4 +40,4 @@ describe('Lighthouse Flow Report', () => {
expect(pageErrors).toHaveLength(0);
});
});
});
}).timeout(35_000);
20 changes: 20 additions & 0 deletions flow-report/test/run-flow-report-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

##
# @license Copyright 2022 The Lighthouse Authors. 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.
##

set -eux

ARGS=(
--testMatch='{flow-report/**/*-test.ts,flow-report/**/*-test.tsx}'
--require=flow-report/test/setup/env-setup.ts
--loader=@esbuild-kit/esm-loader
# util-test.tsx won't finish on its own because of an open MessagePort, so help it out.
# See https://github.com/jsdom/jsdom/issues/2448#issuecomment-802288244
--exit
)

yarn mocha ${ARGS[*]} "$@"
Loading

0 comments on commit ce17c7d

Please sign in to comment.