Skip to content

Commit

Permalink
Replace AVA with Vitest (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Apr 3, 2024
1 parent 402e60e commit 1c81997
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 85 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ jobs:
run: npm ci || npm install
- name: build
run: npm run build --if-present
- name: AVA
run: npx ava
- name: Vitest
run: npx vitest

Build:
runs-on: ubuntu-latest
Expand Down
82 changes: 82 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import {setTimeout} from 'node:timers/promises';
import {assert, test} from 'vitest';
import {JSDOM} from 'jsdom';
import oneMutation from './index.js';

const jsdom = new JSDOM('');
globalThis.document = jsdom.window.document;
globalThis.Text = jsdom.window.Text;
globalThis.MutationObserver = jsdom.window.MutationObserver;

const t = {
is: assert.equal,
like: assert.containSubset,
deepEqual: assert.deepEqual,
};

function onlyTextNotesMutations(mutations: MutationRecord[]) {
for (const {addedNodes} of mutations) {
for (const node of addedNodes) {
if (node.nodeType === node.TEXT_NODE) {
return true;
}
}
}

return false;
}

test('should observe one mutation', async () => {
const observer = oneMutation(document.body, {
childList: true,
});
document.body.append('Text');
const records = await observer;
t.is(records.length, 1);
t.like(records[0], {
target: document.body,
type: 'childList',
addedNodes: [
new Text(),
],
});
});

test('should filter unwanted mutations', async () => {
const observer = oneMutation(document.body, {
filter: onlyTextNotesMutations,
childList: true,
});
document.body.append(document.createElement('div'));
await setTimeout(10);
document.body.append('Text');
const records = await observer;
t.is(records.length, 1);
t.like(records[0], {
target: document.body,
type: 'childList',
addedNodes: [
new Text(),
],
});
});

test('should only listen to the specified mutations', async () => {
const observer = oneMutation(document.body, {
attributes: true,
});
document.body.append(document.createElement('div'));
document.body.dataset.sawadee = 'ครับ';

const emptyNodeListFixture = document
.createElement('div')
.querySelectorAll('a');
const records = await observer;
t.is(records.length, 1);
t.like(records[0], {
target: document.body,
type: 'attributes',
attributeName: 'data-sawadee',
addedNodes: emptyNodeListFixture,
});
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
"scripts": {
"build": "tsc",
"prepack": "tsc --sourceMap false",
"test": "tsc && xo && ava",
"test": "tsc && xo && vitest run",
"watch": "run-p --silent watch:*",
"watch:build": "tsc --watch",
"watch:test": "ava --watch"
"watch:test": "vitest"
},
"xo": {
"envs": [
Expand All @@ -47,11 +47,11 @@
},
"devDependencies": {
"@sindresorhus/tsconfig": "^5.0.0",
"ava": "^6.1.2",
"delay": "^6.0.0",
"@types/jsdom": "^21.1.6",
"jsdom": "^24.0.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.4.3",
"vitest": "^1.4.0",
"xo": "^0.58.0"
},
"engines": {
Expand Down
79 changes: 0 additions & 79 deletions test.js

This file was deleted.

0 comments on commit 1c81997

Please sign in to comment.