Skip to content

Commit

Permalink
Update suite test assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
RobAndrewHurst committed Nov 13, 2024
1 parent 81eab3b commit c50f7df
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codi-test-framework",
"version": "v0.0.39",
"version": "v0.0.40",
"description": "A simple test framework for JavaScript",
"main": "src/testRunner.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion src/core/describe.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export async function describe(description, callback) {
state.pushSuite(suite);

try {
await Promise.resolve(callback());
await Promise.resolve(callback(description));
} catch (error) {
console.error(chalk.red(`Suite failed: ${description}`));
console.error(chalk.red(error.stack));
Expand Down
15 changes: 8 additions & 7 deletions src/core/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { state } from '../state/TestState.js';
* @returns {Promise<void>}
* @throws {Error} If called outside a describe block
*/
export async function it(description, callback) {
if (!state.currentSuite) {
throw new Error('Test case defined outside of describe block');
export async function it(description, callback, suiteDescription) {
// Find the suite by its description
const suite = state.suiteStack.find(s => s.description === suiteDescription);

if (!suite) {
throw new Error(`Cannot find test suite with description: ${suiteDescription}`);
}

const test = {
Expand All @@ -25,15 +28,13 @@ export async function it(description, callback) {
test.status = 'passed';
test.duration = performance.now() - test.startTime;
state.passedTests++;

} catch (error) {

test.status = 'failed';
test.error = error;
test.duration = performance.now() - test.startTime;
state.failedTests++;

}

state.currentSuite.tests.push(test);
// Add the test to the correct suite
suite.tests.push(test);
}
2 changes: 1 addition & 1 deletion src/runners/nodeRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export async function runTestFunction(testFn, options, description) {
}

try {
await Promise.resolve(testFn());
await Promise.resolve(testFn(suite.description));
} catch (error) {
console.error(`Error in test ${testFn.name}:`, error);
state.failedTests++;
Expand Down
9 changes: 0 additions & 9 deletions src/state/TestState.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,6 @@ class TestState {
return ((performance.now() - this.startTime) / 1000).toFixed(2);
}

/**
* Get the current test suite
* @method
* @returns {object|null} Current test suite or null if none active
*/
get currentSuite() {
return this.suiteStack[this.suiteStack.length - 1] || null;
}

/**
* Add a new suite to the stack
* @method
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const assertFalse = assertions.assertFalse;
export const assertThrows = assertions.assertThrows;
export const assertNoDuplicates = assertions.assertNoDuplicates;

export const version = 'v0.0.39';
export const version = 'v0.0.40';

/**
* CLI entry point for running tests
Expand Down
20 changes: 10 additions & 10 deletions tests/example.test.mjs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
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', () => {
await describe('I am an Example Test Suite', (description) => {

it('should pass equality assertion', () => {
assertEqual(1, 1, 'Expected 1 to equal 1');
});
}, description);

it('should pass inequality assertion', () => {
assertNotEqual(1, 2, 'Expected 1 not to equal 2');
});
}, description);

it('should pass true assertion', () => {
assertTrue(true, 'Expected true to be true');
});
}, description);

it('should pass false assertion', () => {
assertFalse(false, 'Expected false to be false');
});
}, description);

it('should pass error assertion', () => {
assertThrows(() => {
throw new Error('An error occurred');
}, 'An error occurred', 'Expected an error to be thrown');
});
}, description);

it('should deeply compare objects', () => {
const obj1 = { a: 1, b: { c: 2 } };
const obj2 = { a: 1, b: { c: 2 } };
assertEqual(obj1, obj2, 'Expected objects to be deeply equal');
});
}, description);

it('should check for duplicates', () => {
const array = ['field1', 'field2']
assertNoDuplicates(array, 'There should be no duplicates');
});
}, description);

});

await runTestFunction(testFunction, { showSummary: false });

function testFunction() {
it('First', () => {
assertEqual(1, 2, 'Expected 1 to equal 1');
});
assertEqual(1, 1, 'Expected 1 to equal 1');
}, 'Function: testFunction');
}

0 comments on commit c50f7df

Please sign in to comment.