Skip to content

Commit

Permalink
refactor(testing): draw testing lib into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Dec 21, 2023
1 parent 7163b32 commit 8950d82
Show file tree
Hide file tree
Showing 19 changed files with 110 additions and 78 deletions.
5 changes: 2 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
},
"author": "Laurent RENARD",
"devDependencies": {
"@cofn/test-lib": "workspace:*",
"playwright": "^1.40.1",
"vite": "^5.0.4",
"zora": "^5.2.0",
"zora-reporters": "^1.4.0"
"vite": "^5.0.4"
}
}
12 changes: 0 additions & 12 deletions packages/core/test/_tools/readable.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/core/test/_tools/test.js

This file was deleted.

2 changes: 1 addition & 1 deletion packages/core/test/abort-signal.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { define } from '../src/index.js';
import { test } from './_tools/test.js';
import { nextTick } from './utils.js';
import { test } from '@cofn/test-lib/client';

const debug = document.getElementById('debug');
define('abort-signal', function* ({ $signal, $host }) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/reactive-attributes.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from './_tools/test.js';
import { test } from '@cofn/test-lib/client';
import { define } from '../src/index.js';
import { nextTick } from './utils.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/shadow-dom.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from './_tools/test.js';
import { test } from '@cofn/test-lib/client';
import { define } from '../src/index.js';

const debug = document.getElementById('debug');
Expand Down
6 changes: 3 additions & 3 deletions packages/core/test/simple-component.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { define } from '../src/index.js';
import { test } from './_tools/test.js';
import { test } from '@cofn/test-lib/client';
import { nextTick } from './utils.js';

const debug = document.getElementById('debug');
Expand All @@ -17,10 +17,10 @@ define('simple-component', function* ({ $root, $host }) {
});

const withEl = (specFn) =>
function zora_spec_fn(assert) {
async function zora_spec_fn(assert) {
const el = document.createElement('simple-component');
debug.appendChild(el);
return specFn({ ...assert, el });
return await specFn({ ...assert, el });
};
test(
'define a simple component from a coroutine',
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/test-suite.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<head>
<meta charset='UTF-8'>
<title>Test suite for package core</title>
<link rel='stylesheet' href="./_tools/html-reporter/reporter.css" type="text/css">
<style>
@import url("@cofn/test-lib/client/theme.css");
</style>
</head>
<body>
<div id="report-container">
Expand All @@ -13,14 +15,12 @@ <h1>Test reporting</h1>
<div id="debug"></div>
</body>
<script type='module'>
import { report } from './_tools/test.js';
import { report, createHTMLReporter, createSocketSink } from '@cofn/test-lib/client';
import './simple-component.test.js';
import './reactive-attributes.test.js';
import './abort-signal.test.js';
import './shadow-dom.test.js';

import { createHTMLReporter, createSocketSink } from './_tools/writable.js';

const [st1, st2] = report().tee();
st1.pipeTo(createSocketSink(import.meta.hot));
st2.pipeTo(createHTMLReporter({element: document.getElementById('report')}))
Expand Down
39 changes: 2 additions & 37 deletions packages/core/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,6 @@
import { defineConfig } from 'vite';
import { ReadableStream } from 'node:stream/web';
import { createDiffReporter } from 'zora-reporters';

const createStream = ({ ws, client: _client }) => {
let listener;
return new ReadableStream({
start(controller) {
listener = (data, client) => {
if (client === _client) {
if (data.type === 'STREAM_ENDED') {
controller.close();
ws.off('zora', listener);
} else {
controller.enqueue(data);
}
}
};

ws.on('zora', listener);
},
});
};
const plugin = () => ({
name: 'zora-dev',
async configureServer(server) {
const report = createDiffReporter();
const socketStreams = new WeakMap();

server.ws.on('zora', async ({ type }, client) => {
if (type === 'STREAM_STARTED') {
const readableStream = createStream({ ws: server.ws, client });
await report(readableStream);
}
});
},
});
import zoraDev from '@cofn/test-lib/vite';

export default defineConfig({
plugins: [plugin()],
plugins: [zoraDev()],
});
27 changes: 27 additions & 0 deletions packages/test-lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@cofn/test-lib",
"version": "1.0.0",
"description": "",
"type": "module",
"module": "./src/client/index.js",
"style": "./src/client/theme.css",
"scripts": {},
"exports": {
"./package.json": "./package.json",
"./client": {
"import": "./src/client/index.js"
},
"./client/theme.css": {
"import": "./src/client/theme.css",
"require": "./src/client/theme.css"
},
"./vite": {
"import": "./src/vite/index.js"
}
},
"author": "Laurent RENARD",
"dependencies": {
"zora": "^5.2.0",
"zora-reporters": "^1.4.0"
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const createHTMLReporter = ({ element }) => {
}
case 'ASSERTION': {
currentTestEl.addAssertion(data);
console.log(data);
break;
}
case 'TEST_END': {
Expand Down
3 changes: 3 additions & 0 deletions packages/test-lib/src/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './test.js';
export * from './socket-sink.js';
export * from './html-repoter.js';
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,3 @@ export const createSocketSink = (ws) => {
},
});
};

export { createHTMLReporter } from './html-reporter/repoter.js';
18 changes: 18 additions & 0 deletions packages/test-lib/src/client/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createHarness } from 'zora/es';
const createReporter = (messageStream) => {
return new ReadableStream({
async pull(controller) {
const { done, value } = await messageStream.next();
if (done) {
controller.close();
return;
}
return controller.enqueue(value);
},
});
};

const { test, skip, only, report: _report } = createHarness();
const report = () => _report({ reporter: createReporter });

export { test, skip, only, report };
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--flag-success-color: #1a751a;
--flag-failure-color: #ef4d4d;
--code-block-bg-color: #ececec;
--spacing: 1em;
}

@media (prefers-color-scheme: light) {
Expand Down Expand Up @@ -49,7 +50,6 @@ body * {
}

zora-test-result {
--spacing: 1em;
border: 1px solid currentColor;
border-radius: 0 4px 4px 0;
}
Expand Down
36 changes: 36 additions & 0 deletions packages/test-lib/src/vite/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ReadableStream } from 'node:stream/web';
import { createDiffReporter } from 'zora-reporters';

const createStream = ({ ws, client: _client }) => {
let listener;
return new ReadableStream({
start(controller) {
listener = (data, client) => {
if (client === _client) {
if (data.type === 'STREAM_ENDED') {
controller.close();
ws.off('zora', listener);
} else {
controller.enqueue(data);
}
}
};

ws.on('zora', listener);
},
});
};
export default () => ({
name: 'zora-dev',
async configureServer(server) {
const report = createDiffReporter();
const socketStreams = new WeakMap();

server.ws.on('zora', async ({ type }, client) => {
if (type === 'STREAM_STARTED') {
const readableStream = createStream({ ws: server.ws, client });
await report(readableStream);
}
});
},
});
16 changes: 11 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8950d82

Please sign in to comment.