Skip to content

Commit

Permalink
Webrunner changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Nov 13, 2024
1 parent 6eb9652 commit c703ecd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"scripts": {
"test": "bun --bun cli.js tests --returnResults",
"test_web": "bun --bun ./tests/webfunction.js",
"version": "bun --bun cli.js --version"
},
"keywords": [
Expand Down
30 changes: 30 additions & 0 deletions src/runners/webRunner.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { state } from '../state/TestState.js';
import chalk from 'chalk';
import { runTestFunction } from './nodeRunner.js';

/**
* Run a web test file
Expand Down Expand Up @@ -103,4 +104,33 @@ export async function runWebTests(testFiles, options) {
}

return summary;
}

/**
* Run a single test function
* @async
* @function runTestFunction
* @param {Function} testFn - Test function to run
* @returns {Promise<object>} Test results
*/
export async function runWebTestFunction(testFn, options) {

options ??= {
quiet: false,
showSummary: true
}

state.setOptions(options);

await runTestFunction(testFn);

if (options.showSummary) {
state.printSummary();
}

return {
passedTests: state.passedTests,
failedTests: state.failedTests,
testResults: state.testResults
};
}
2 changes: 0 additions & 2 deletions tests/example.test.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, it, assertEqual, assertNotEqual, assertTrue, assertFalse, assertThrows, assertNoDuplicates, runTestFunction } from '../src/testRunner.js';

// First test suite
await describe('I am an Example Test Suite', () => {

it('should pass equality assertion', () => {
Expand Down Expand Up @@ -41,7 +40,6 @@ await describe('I am an Example Test Suite', () => {

await runTestFunction(testFunction);


function testFunction() {
describe('First Layer', () => {
it('First', () => {
Expand Down
53 changes: 53 additions & 0 deletions tests/webfunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { runWebTestFunction } from "../src/runners/webRunner.js";
import { describe, it, assertEqual, } from '../src/testRunner.js';


await runWebTestFunction(testFunction, {
quiet: true,
showSummary: true
});

function testFunction() {
describe('First Layer', () => {
it('First', () => {
assertEqual(1, 2, 'Expected 1 to equal 1');
});
it('Second', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
it('Third', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});

secondFunction();
});
}

function secondFunction() {
describe('Second Layer', () => {
it('First', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
it('Second', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
it('Third', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
thirdFunction();
});
}

function thirdFunction() {
describe('Third Layer', () => {
it('First', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
it('Second', () => {
assertEqual(1, 2, 'Expected 1 to equal 1');
});
it('Third', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
});
}

0 comments on commit c703ecd

Please sign in to comment.