Skip to content

Commit

Permalink
Replace AVA with bun:test (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Sep 28, 2023
1 parent 61be460 commit af4ff81
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 1,555 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -27,8 +27,8 @@ jobs:
run: npm ci || npm install
- name: build
run: npm run build --if-present
- name: AVA
run: npx ava
- name: Test
run: npm run test:unit

Build:
runs-on: ubuntu-latest
Expand Down
1 change: 0 additions & 1 deletion collector.ts
Expand Up @@ -3,7 +3,6 @@
export const testableUrls = new Map<string, string[]>();

export function addTests(test: string, urls: string[]): void {
// @ts-expect-error KISS for Vite
if (process.env.NODE_ENV !== 'bundling') {
testableUrls.set(test, urls);
}
Expand Down
88 changes: 46 additions & 42 deletions test.ts → index.test.ts
@@ -1,4 +1,7 @@
import test from 'ava';
/// <reference types="bun-types" />
/// <reference lib="dom" />
import assert from 'node:assert/strict';
import {test} from 'bun:test';
import stripIndent from 'strip-indent';
import {getAllUrls, getTests} from './collector.js';
import * as pageDetect from './index.js';
Expand All @@ -15,12 +18,12 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {

const validURLs = getTests(detectName);

if (validURLs[0] === 'combinedTestOnly' || String(detect).startsWith('()=>')) {
if (validURLs[0] === 'combinedTestOnly' || String(detect).startsWith('() =>')) {
continue;
}

test(detectName + ' has tests', t => {
t.true(
test(detectName + ' has tests', () => {
assert.ok(
Array.isArray(validURLs),
`The function \`${detectName}\` doesn’t have any tests. Set them via \`collect.set()\``,
);
Expand All @@ -31,8 +34,8 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
}

for (const url of validURLs) {
test(`${detectName} ${url.replace('https://github.com', '')}`, t => {
t.true(
test(`${detectName} ${url.replace('https://github.com', '')}`, () => {
assert.ok(
detect(new URL(url)),
stripIndent(`
Is this URL \`${detectName}\`?
Expand All @@ -52,9 +55,10 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {

for (const url of allUrls) {
if (!validURLs.includes(url)) {
test(`${detectName} NO ${url}`, t => {
t.false(
test(`${detectName} NO ${url}`, () => {
assert.equal(
detect(new URL(url)),
false,
stripIndent(`
Is this URL \`${detectName}\`?
${url.replace('https://github.com', '')}
Expand All @@ -68,125 +72,125 @@ for (const [detectName, detect] of Object.entries(pageDetect)) {
}
}

test('is404', t => {
test('is404', () => {
document.title = 'Page not found · GitHub';
t.true(pageDetect.is404());
assert.ok(pageDetect.is404());

document.title = 'File not found · GitHub';
t.true(pageDetect.is404());
assert.ok(pageDetect.is404());

document.title = 'examples/404: Page not found examples';
t.false(pageDetect.is404());
assert.equal(pageDetect.is404(), false);

document.title = 'Dashboard';
t.false(pageDetect.is404());
assert.equal(pageDetect.is404(), false);

document.title = 'Page not found · Issue #266 · sintaxi/surge · GitHub';
t.false(pageDetect.is404());
assert.equal(pageDetect.is404(), false);
});

test('is500', t => {
test('is500', () => {
document.title = 'Server Error · GitHub';
t.true(pageDetect.is500());
assert.ok(pageDetect.is500());

document.title = 'Unicorn! · GitHub';
t.true(pageDetect.is500());
assert.ok(pageDetect.is500());

document.title = 'examples/500: Server Error examples';
t.false(pageDetect.is500());
assert.equal(pageDetect.is500(), false);

document.title = 'sindresorhus/unicorn: You can’t tell what doesn’t exist';
t.false(pageDetect.is500());
assert.equal(pageDetect.is500(), false);

document.title = 'Dashboard';
t.false(pageDetect.is500());
assert.equal(pageDetect.is500(), false);

document.title = 'Server Error · Issue #266 · sintaxi/surge · GitHub';
t.false(pageDetect.is500());
assert.equal(pageDetect.is500(), false);
});

test('isPRCommit404', t => {
test('isPRCommit404', () => {
document.title = 'Commit range not found · Pull Request #3227 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3227/commits/32c8a88360a85739f151566eae0225d530ce6a15';
t.true(pageDetect.isPRCommit404());
assert.ok(pageDetect.isPRCommit404());

document.title = 'Experiment with `@primer/octicons-react` icons by FloEdelmann · Pull Request #3227 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3227/commits/edbdcdd5559a2a8da78abdc7cb0814155713974c';
t.false(pageDetect.isPRCommit404());
assert.equal(pageDetect.isPRCommit404(), false);

document.title = 'Commit range not found by SomeContributor · Pull Request #999999 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/999999/commits/32c8a88360a85739f151566eae0225d530ce6a15';
t.false(pageDetect.isPRCommit404());
assert.equal(pageDetect.isPRCommit404(), false);
});

test('isPRFile404', t => {
test('isPRFile404', () => {
document.title = 'Commit range not found · Pull Request #789 · sindresorhus/eslint-plugin-unicorn';
location.href = 'https://github.com/sindresorhus/eslint-plugin-unicorn/pull/789/files/a58b37845f1b2660221de019e4ae6c736feedc26..eed168224d7994652b1d1ff69a5c8cebee223faf';
t.true(pageDetect.isPRFile404());
assert.ok(pageDetect.isPRFile404());

document.title = 'Add `align-repository-header` feature by fregante · Pull Request #3313 · sindresorhus/refined-github';
location.href = 'https://github.com/sindresorhus/refined-github/pull/3313/files/a14fb2c94eae3ca83a3a97688a171fcc3405524f..fbeeba9825f12b5ded9cd4bb04d5df4b0cf2f2a8';
t.false(pageDetect.isPRFile404());
assert.equal(pageDetect.isPRFile404(), false);
});

test('isRepoFile404', t => {
test('isRepoFile404', () => {
document.title = 'File not found';
location.href = 'https://github.com/fregante/GhostText/tree/3cacd7df71b097dc525d99c7aa2f54d31b02fcc8/chrome/scripts/InputArea';
t.true(pageDetect.isRepoFile404());
assert.ok(pageDetect.isRepoFile404());

document.title = 'File not found';
location.href = 'https://github.com/refined-github/refined-github/blob/some-non-existent-ref/source/features/bugs-tab.tsx';
t.true(pageDetect.isRepoFile404());
assert.ok(pageDetect.isRepoFile404());
});

const {getRepositoryInfo} = pageDetect.utils;
test('getRepositoryInfo', t => {
test('getRepositoryInfo', () => {
const inputTypes = [
getRepositoryInfo, // Full URL
(url: string) => getRepositoryInfo(new URL(url).pathname), // Pathname only
(url: string) => getRepositoryInfo(new URL(url)), // URL object
];
for (const getRepositoryInfoAdapter of inputTypes) {
t.is(getRepositoryInfoAdapter('https://github.com'), undefined);
t.is(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
t.is(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined);
assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: '',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'blame/master/package.json',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'commit/57bf4',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'compare/test-branch',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
path: 'tree/master/distribution',
});
t.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
owner: 'refined-github',
name: 'github-url-detection',
nameWithOwner: 'refined-github/github-url-detection',
Expand Down

0 comments on commit af4ff81

Please sign in to comment.