Skip to content

Commit

Permalink
chore: Migrate to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldowseza committed May 7, 2024
1 parent 4cd1376 commit 6c0c362
Show file tree
Hide file tree
Showing 18 changed files with 2,766 additions and 4,283 deletions.
6,919 changes: 2,704 additions & 4,215 deletions package-lock.json

Large diffs are not rendered by default.

37 changes: 5 additions & 32 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
"clean": "rm -rf build/screenshots coverage lib && mkdir -p build/screenshots",
"prestart": "npm run clean && npm run build",
"start": "node demo",
"pretest": "npm run clean && npm run build",
"test": "run-p -r start-http-server run-tests",
"run-tests": "wait-on http://localhost:9615 && jest",
"run-tests": "wait-on http://localhost:9615 && vitest run",
"start-http-server": "http-server test/fixtures --port=9615 -a localhost --silent",
"lint": "eslint --ext=ts,js --ignore-path .gitignore .",
"prebuild": "npm run clean",
Expand All @@ -30,7 +29,7 @@
},
"dependencies": {
"@aws-sdk/client-device-farm": "^3.398.0",
"@types/pngjs": "^6.0.1",
"@types/pngjs": "^6.0.4",
"get-stream": "^6.0.1",
"lodash": "^4.17.21",
"p-retry": "^4.6.2",
Expand All @@ -39,25 +38,24 @@
"webdriverio": "^7.25.2"
},
"devDependencies": {
"@types/jest": "^29.2.0",
"@types/lodash": "^4.14.186",
"@types/node": "^16.0.0",
"@types/node": "^18.0.0",
"@types/pixelmatch": "^5.2.4",
"@typescript-eslint/eslint-plugin": "^5.41.0",
"@typescript-eslint/parser": "^5.41.0",
"@vitest/coverage-istanbul": "^1.2.2",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-header": "^3.1.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unicorn": "^44.0.2",
"http-server": "^14.1.1",
"husky": "^8.0.1",
"jest": "^29.2.2",
"lint-staged": "^13.0.3",
"npm-run-all": "^4.1.5",
"prettier": "^2.7.1",
"ts-jest": "^29.0.3",
"typescript": "^4.9.5",
"vitest": "^1.5.3",
"wait-on": "^7.2.0"
},
"exports": {
Expand All @@ -71,30 +69,5 @@
"*.{js,ts}": [
"eslint --fix"
]
},
"jest": {
"testEnvironment": "node",
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.ts",
"!src/browser-scripts/**",
"!src/page-objects/browser-scripts.ts",
"!src/chrome-launcher.ts"
],
"coverageReporters": [
"json-summary",
"lcov",
"cobertura"
],
"globalSetup": "<rootDir>/test/utils/start-chromedriver.ts",
"globalTeardown": "<rootDir>/test/utils/stop-chromedriver.ts",
"transform": {
"^.+\\.ts$": [
"ts-jest",
{
"tsconfig": "tsconfig.test.json"
}
]
}
}
}
1 change: 1 addition & 0 deletions test/browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect } from 'vitest';
import getBrowserCreator from '../src/browser';

test('should throw on unknown type of selenium provider', () => {
Expand Down
3 changes: 2 additions & 1 deletion test/browsers/browserstack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, test, expect, beforeEach, vi } from 'vitest';
import { remoteMock } from './wdio-mock';
import BrowserStackBrowserCreator from '../../src/browsers/browserstack';

Expand All @@ -16,7 +17,7 @@ const browserOptions = {

describe('BrowserStack browserCreator ', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should try to reconnect in case of a full BrowserStack queue', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/browsers/devicefarm-mobile.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, test, expect } from 'vitest';
import './wdio-mock';
import MobileBrowserCreator from '../../src/browsers/devicefarm-mobile';

Expand Down
7 changes: 4 additions & 3 deletions test/browsers/devicefarm.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, test, expect, vi, beforeEach } from 'vitest';
import './wdio-mock';
import DevicefarmBrowserCreator from '../../src/browsers/devicefarm';

const createTestGridUrl = jest.fn();
const createTestGridUrl = vi.fn();

jest.mock('@aws-sdk/client-device-farm', () => {
vi.mock('@aws-sdk/client-device-farm', () => {
class DeviceFarm {
createTestGridUrl = createTestGridUrl;
}
Expand All @@ -16,7 +17,7 @@ const browserName = 'Chrome';

describe('Devicefarm browserCreator ', () => {
beforeEach(() => {
jest.clearAllMocks();
vi.clearAllMocks();
});

test('should throw if devicefarm returned an invalid response', async () => {
Expand Down
5 changes: 3 additions & 2 deletions test/browsers/wdio-mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { beforeEach, vi } from 'vitest';
import type { RemoteOptions } from 'webdriverio';

class FakeWebdriver {
Expand All @@ -15,13 +16,13 @@ class FakeWebdriver {
}
}

export const remoteMock = jest.fn();
export const remoteMock = vi.fn();

beforeEach(() => {
remoteMock.mockReset();
remoteMock.mockImplementation((options: RemoteOptions) => new FakeWebdriver(options));
});

jest.mock('webdriverio', () => ({
vi.mock('webdriverio', () => ({
remote: remoteMock,
}));
1 change: 1 addition & 0 deletions test/compare-images.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect } from 'vitest';
import fs from 'fs';
import { PNG } from 'pngjs';
import useBrowser from '../src/use-browser';
Expand Down
1 change: 1 addition & 0 deletions test/full-page-screenshot.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect } from 'vitest';
import type { Browser as PuppeteerBrowser } from 'puppeteer-core';

import { parsePng, packPng } from '../src/image-utils/utils';
Expand Down
1 change: 1 addition & 0 deletions test/image-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { describe, test, expect } from 'vitest';
import fs from 'fs';
import path from 'path';
import mergeImages from '../src/image-utils/merge';
Expand Down
9 changes: 5 additions & 4 deletions test/page-object.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect, describe, vi } from 'vitest';
import { ScreenshotPageObject } from '../src/page-objects';
import { calculateIosTopOffset } from '../src/page-objects/utils';
import useBrowser from '../src/use-browser';
Expand Down Expand Up @@ -131,7 +132,7 @@ describe('waitForAssertion', () => {
test(
'successful assertion',
setupTest(async page => {
const assertion = jest.fn(async () => expect(true).toEqual(true));
const assertion = vi.fn(async () => expect(true).toBe(true));
await page.waitForAssertion(assertion);
expect(assertion).toHaveBeenCalledTimes(1);
})
Expand All @@ -141,7 +142,7 @@ describe('waitForAssertion', () => {
'retrying once assertion',
setupTest(async page => {
let counter = 0;
const assertion = jest.fn(async () => {
const assertion = vi.fn(async () => {
counter++;
expect(counter).toEqual(2);
});
Expand All @@ -153,8 +154,8 @@ describe('waitForAssertion', () => {
test(
'reports the original error into the outer scope',
setupTest(async page => {
const assertion = jest.fn(async () => expect(true).toEqual(false));
await expect(page.waitForAssertion(assertion)).rejects.toThrowError(/toEqual/);
const assertion = vi.fn(async () => expect(true).toBe(false));
await expect(page.waitForAssertion(assertion)).rejects.toThrowError(/expected true to be false/);
expect(assertion).toHaveBeenCalledTimes(6);
})
);
Expand Down
10 changes: 5 additions & 5 deletions test/use-browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { test, expect, vi } from 'vitest';
import { promisify } from 'util';
import useBrowser from '../src/use-browser';
import { configure } from '../src/use-browser';
Expand Down Expand Up @@ -27,7 +28,7 @@ test(
);

test('should close browser after test finish', async () => {
const onDeleteSession = jest.fn();
const onDeleteSession = vi.fn();
await useBrowser(async browser => {
// FIXME: Casting to any isn't ideal, but it's the only way to overwrite this command
(browser as any).overwriteCommand('deleteSession', async (originalCommand: any) => {

Check warning on line 34 in test/use-browser.test.ts

View workflow job for this annotation

GitHub Actions / build / build

Unexpected any. Specify a different type
Expand All @@ -38,10 +39,9 @@ test('should close browser after test finish', async () => {
expect(onDeleteSession).toHaveBeenCalled();
});

test('propagates an error happened in test', async () => {
// the browser is configured to wait for 3x5000ms before throwing an error
// so we need to increasing jest default timeout
jest.setTimeout(20 * 10000);
// the browser is configured to wait for 3x5000ms before throwing an error
// so we need to increase the default timeout to 15000
test('propagates an error happened in test', { timeout: 15000 }, async () => {
function brokenTest() {
return useBrowser(async browser => {
await (await browser.$('#not-existing')).click();
Expand Down
13 changes: 8 additions & 5 deletions test/utils/start-chromedriver.ts → test/utils/global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import dns from 'dns';
import { startWebdriver } from '../../lib/chrome-launcher';
import dns from 'node:dns';
import { startWebdriver, shutdownWebdriver } from '../../lib/chrome-launcher';
import { chromeDriverPort } from './config';

export default async () => {
export async function setup() {
// Make sure to favor IPv4 name resolution over IPv6 so that the localhost debugger can be found.
dns.setDefaultResultOrder('ipv4first');

await startWebdriver(chromeDriverPort);
};
}

export function teardown() {
shutdownWebdriver();
}
3 changes: 1 addition & 2 deletions test/utils/setup-local-driver.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { beforeAll } from 'vitest';
import { chromeDriverPort } from './config';
import { configure } from '../../src/use-browser';

jest.setTimeout(80 * 1000);

beforeAll(async () => {
configure({
browserName: 'ChromeHeadless',
Expand Down
5 changes: 0 additions & 5 deletions test/utils/stop-chromedriver.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"module": "commonjs",
"moduleResolution": "node",
"lib": ["es2017", "dom"],
"types": ["node", "webdriverio/async", "jest"],
"types": ["node", "webdriverio/async"],
"declaration": true,
"strict": true,
"esModuleInterop": true,
Expand Down
8 changes: 0 additions & 8 deletions tsconfig.test.json

This file was deleted.

23 changes: 23 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
environment: 'node',
globalSetup: './test/utils/global-setup.ts',
testTimeout: 15000,
coverage: {
enabled: process.env.CI === 'true',
provider: 'istanbul',
include: ['src/**'],
exclude: [
'**/debug-tools/**',
'**/test/**',
'src/browser-scripts/**',
'src/page-objects/browser-scripts.ts',
'src/chrome-launcher.ts',
],
},
},
});

0 comments on commit 6c0c362

Please sign in to comment.