Browse Source

Improved testing and figuring out that the istanbul tests seem to duplicate anything in bundle.js vs. things tested directly in tests/ so I'll probably have to do one and not the other.

master
Zed A. Shaw 2 months ago
parent
commit
1a596828e4
  1. 1
      client/fsm.js
  2. 8
      client/helpers.js
  3. 63
      client/pages/admin/Tests.svelte
  4. 2
      lib/testing.js
  5. 16
      tests/client/assert.js
  6. 12
      tests/client/fsm.js
  7. 2
      tests/client/helpers.js
  8. 22
      tests/ui/admin_tests.js

1
client/fsm.js

@ -50,4 +50,3 @@ export default class FSM {
return this.state;
}
}

8
client/helpers.js

@ -18,14 +18,14 @@ export const defer = (debug="") => {
let promise = new Promise((resolve, reject) => {
if(debug) {
res = () => {
res = (result) => {
console.log("resolved defer", debug);
resolve();
resolve(result);
}
rej = () => {
rej = (error) => {
console.log("REJECT defer", debug);
rej = reject;
reject(error);
}
} else {
res = resolve;

63
client/pages/admin/Tests.svelte

@ -0,0 +1,63 @@
<script>
import { onMount } from 'svelte';
import Layout from '$/client/Layout.svelte';
import api from "$/client/api.js";
import assert from "$/client/assert.js";
import { random_numbers, defer } from "$/client/helpers.js";
// just get some random numbers to run that function
const r = random_numbers(2);
assert(r.length == 2, "Whoops, random_numbers failed.");
// this is a mock on purpose to test the mock code
api.mock({
"/admin/fake/test": {
"get": [500, {"message": "Failure."}],
}
});
const assert_promise = defer("assert promise");
const bad_promise = defer("bad promise");
onMount(async () => {
const [status, data] = await api.get("/admin/fake/test");
bad_promise.reject(new Error("Bad worked."));
try {
assert(status === 200, "Fake test call failed (it should).");
} catch(error) {
console.error(error, "Assert worked!");
assert_promise.resolve("resolve-message");
}
});
</script>
<Layout testid="page-admin-test" centered={ true }>
<h1>Additional Tests</h1>
<callout class="info">
<span>
<b>NOTE:</b> This page basically does nothing except provide access to
things for the unit test in tests/ui/admin_tests.js. It has a mock
on purpose to test that they keep working, and does asserts as well.
You can delete this if you also delete the test.
</span>
</callout>
{#await assert_promise}
<p data-testid="no-assert">Waiting for assert.</p>
{:then msg}
<p data-testid="assert-works">Assert is working. Good job! Message: { msg }</p>
{/await}
{#await bad_promise}
<p data-testid="bad-await">This should error.</p>
{:then}
<p data-testid="bad-failed">OOPS! This shouldn't run.</p>
{:catch error}
<p data-testid="bad-worked">Defer failure worked: { error }</p>
{/await}
</Layout>

2
lib/testing.js

@ -35,7 +35,7 @@ export const form = async (page, fields, button_id) => {
export const playstart = async (url) => {
if(!HEADLESS) console.log("Running playwright in visible mode (not HEADLESS).");
if(COVERAGE) console.log("Coverage reports going to .nyc_output.");
if(COVERAGE) console.log(`Coverage reports going to ${COVERAGE}.`);
const browser = await playwright['chromium'].launch({headless: HEADLESS});
const context = await browser.newContext();

16
tests/client/assert.js

@ -0,0 +1,16 @@
import test from "ava";
import assert from "../../client/assert.js";
test("quick assert test", async (t) => {
let assert_ran = false;
try {
// confirm assert works inside FSM
assert(false, "This should fail.");
} catch(error) {
assert_ran = true;
}
t.is(assert_ran, true);
});

12
tests/client/fsm.js

@ -1,6 +1,10 @@
import test from "ava";
import FSM from "../../client/fsm.js";
const add_test = () => {
console.log("Additional function call on state transition passed.");
}
class TestMachine {
constructor(data) {
console.log("DATA IS", data);
@ -19,10 +23,10 @@ class TestMachine {
}
close(state, inc) {
switch(state) {
switch(state) {
case "OPEN":
this.data.count = this.data.count + inc;
return "CLOSED";
return ["CLOSED", add_test];
default:
return "ERROR";
}
@ -34,6 +38,10 @@ test("Basic FSM operations", async (t) => {
let events = new TestMachine(data);
let fsm1 = new FSM(data, events);
fsm1.onTransition((fsm) => {
console.log("On transition: ", fsm.state);
});
await fsm1.do('open', 3);
t.is(fsm1.state, "OPEN");
t.is(data.count, 3);

2
tests/client/helpers.js

@ -7,7 +7,7 @@ const rand = (min, max) => crypto.randomInt(min, max);
const lock = new Mutex();
test("Basic FSM operations", async (t) => {
test("quick helpers test", async (t) => {
const wait_count = rand(0, 50);
await lock.hold();

22
tests/ui/admin_tests.js

@ -0,0 +1,22 @@
import test from 'ava';
import {expect, tid, playstart, playstop} from '../../lib/testing.js';
test.before(async t => t.context = await playstart('http://localhost:5001/client/#/admin/tests/'));
test.after(async t => {
await playstop(t.context.browser, t.context.p);
});
test('test /admin/tests/ works', async (t) => {
const {p} = t.context;
try {
await expect(t, p, tid("page-admin-test"));
// confirm we get errors
await expect(t, p, tid("assert-works"));
await expect(t, p, tid("bad-worked"));
} catch (error) {
console.log(error);
t.fail(error.message);
}
});
Loading…
Cancel
Save