Skip to content

Commit

Permalink
Use test instead of it/describe
Browse files Browse the repository at this point in the history
  • Loading branch information
corrideat committed Feb 10, 2024
1 parent d150391 commit 43b8c2b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 73 deletions.
4 changes: 2 additions & 2 deletions test/lib/runBrowserTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PERFORMANCE OF THIS SOFTWARE.
*/

import { describe } from 'node:test';
import { test } from 'node:test';
import { fileURLToPath } from 'node:url';
import getCodeHelper from './getCodeHelper.js';
import { enabledBrowsers, webdriverTestSuites } from './webdriverTestSuites.js';
Expand All @@ -30,7 +30,7 @@ const runBrowserTest = (m: string, commonBundle?: boolean) => {
);

enabledBrowsers().forEach(([browserName, browserDisplayName]) => {
describe(
test(
`Browser: ${browserDisplayName}, module: ${m}`,
webdriverTestSuites(code, browserName),
);
Expand Down
148 changes: 77 additions & 71 deletions test/lib/webdriverTestSuites.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
*/

import assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import webdriver from 'selenium-webdriver';
import { Options as ChromeOptions } from 'selenium-webdriver/chrome.js';
import { Options as EdgeOptions } from 'selenium-webdriver/edge.js';
import { Options as FirefoxOptions } from 'selenium-webdriver/firefox.js';
import { Options as SafariOptions } from 'selenium-webdriver/safari.js';
import baseTests from './baseTests.json' assert { type: 'json' };
import baseTests from './baseTests.json' with { type: 'json' };

export const enabledBrowsers = () => {
const webdriverBrowsers = new Set(
Expand Down Expand Up @@ -54,11 +53,12 @@ const firefoxOptions = new FirefoxOptions();
const safariOptions = new SafariOptions();

export const webdriverTestSuites =
(codePromise: Promise<string>, browserName: string) => () => {
(codePromise: Promise<string>, browserName: string) =>
(t: Pick<typeof import('node:test'), 'before' | 'after' | 'test'>) => {
let driver: webdriver.WebDriver;
let code: string;

before(
t.before(
async () => {
await Promise.all([
(async () => {
Expand All @@ -84,7 +84,7 @@ export const webdriverTestSuites =
{ timeout: 30e3 },
);

after(
t.after(
async () => {
if (driver) {
await driver.quit().then(() => {
Expand All @@ -95,8 +95,8 @@ export const webdriverTestSuites =
{ timeout: 30e3 },
);

describe('Can run tasks', () => {
it('should return result for sync task', async () => {
t.test('Can run tasks', (t) => {
t.test('should return result for sync task', async () => {
const result = await driver.executeAsyncScript(`
(async () => {
const callback = arguments[arguments.length - 1];
Expand All @@ -115,7 +115,7 @@ export const webdriverTestSuites =
assert.deepEqual(result, ['bar']);
});

it('should return result for async task', async () => {
t.test('should return result for async task', async () => {
const result = await driver.executeAsyncScript(
`(async () => {
const callback = arguments[arguments.length - 1];
Expand All @@ -134,7 +134,7 @@ export const webdriverTestSuites =
assert.deepEqual(result, ['bar']);
});

it('should return result for multiple arguments', async () => {
t.test('should return result for multiple arguments', async () => {
const result = await driver.executeAsyncScript(
`(async () => {
const callback = arguments[arguments.length - 1];
Expand All @@ -154,8 +154,8 @@ export const webdriverTestSuites =
});
});

describe('Error conditions', () => {
it('invalid syntax causes error', async () => {
t.test('Error conditions', (t) => {
t.test('invalid syntax causes error', async () => {
const result = await driver.executeAsyncScript(
`(async () => {
const callback = arguments[arguments.length - 1];
Expand All @@ -175,13 +175,15 @@ export const webdriverTestSuites =

baseTests.forEach(([expression, errorName]: unknown[]) => {
if (String(expression).includes('return')) return;
it(`${expression} ${
errorName === true
? 'succeeds'
: `causes error ${JSON.stringify(errorName)}`
}`, async () => {
const result = await driver.executeAsyncScript(
`(async () => {
t.test(
`${expression} ${
errorName === true
? 'succeeds'
: `causes error ${JSON.stringify(errorName)}`
}`,
async () => {
const result = await driver.executeAsyncScript(
`(async () => {
const callback = arguments[arguments.length - 1];
try {
const sandbox = await m(
Expand All @@ -193,36 +195,39 @@ export const webdriverTestSuites =
callback([null, {name: e && e.name}]);
}
})()`,
);
);

assert.deepEqual(
result,
errorName === true
? [true]
: [
null,
{
name:
// TODO: Handle this in a different
// way. ReferenceError only happens
// when not using globalProxy
Array.isArray(errorName)
? 'TypeError'
: errorName,
},
],
);
});
assert.deepEqual(
result,
errorName === true
? [true]
: [
null,
{
name:
// TODO: Handle this in a different
// way. ReferenceError only happens
// when not using globalProxy
Array.isArray(errorName)
? 'TypeError'
: errorName,
},
],
);
},
);
});

baseTests.forEach(([expression, errorName]: unknown[]) => {
it(`Task with ${expression} ${
errorName === true
? 'succeeds'
: `causes error ${JSON.stringify(errorName)}`
}`, async () => {
const result = await driver.executeAsyncScript(
`(async () => {
t.test(
`Task with ${expression} ${
errorName === true
? 'succeeds'
: `causes error ${JSON.stringify(errorName)}`
}`,
async () => {
const result = await driver.executeAsyncScript(
`(async () => {
const callback = arguments[arguments.length - 1];
try {
const sandbox = await m(
Expand All @@ -240,34 +245,35 @@ export const webdriverTestSuites =
callback([null, null, {name: e && e.name}]);
}
})()`,
);
);

assert.deepEqual(
result,
errorName === true
? [true]
: errorName === 'SyntaxError'
? [
null,
null,
{
name: errorName,
},
]
: [
null,
{
name:
// TODO: Handle this in a different
// way. ReferenceError only happens
// when not using globalProxy
Array.isArray(errorName)
? 'TypeError'
: errorName,
},
],
);
});
assert.deepEqual(
result,
errorName === true
? [true]
: errorName === 'SyntaxError'
? [
null,
null,
{
name: errorName,
},
]
: [
null,
{
name:
// TODO: Handle this in a different
// way. ReferenceError only happens
// when not using globalProxy
Array.isArray(errorName)
? 'TypeError'
: errorName,
},
],
);
},
);
});
});
};

0 comments on commit 43b8c2b

Please sign in to comment.