Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: complete testResults information #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,23 @@ describe('a test suite', () => {
it('should create element', () => {
expect(element.innerHTML).toBe('hello there');
});

describe('an inner test suite', () => {
it("should always be true: Level 2", () => {
expect(true).toBeTrue();
expect(undefined).toBeUndefined();
expect(null).toBeNull();
expect(["foo", "bar"]).toHaveSize(2);
expect({ "foo": "bar" }).toEqual({ "foo": "bar" });
});
describe('another inner test suite', () => {
it("should always be true: level 3", () => {
expect(true).toBeTrue();
expect(undefined).toBeUndefined();
expect(null).toBeNull();
expect(["foo", "bar"]).toHaveSize(2);
expect({ "foo": "bar" }).toEqual({ "foo": "bar" });
});
});
});
});
154 changes: 128 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const jasmineTestRunnerConfig = () => {
reporters: [
defaultReporter({ reportTestResults: true, reportTestProgress: true })
],
testRunnerHtml: (_path: any, config: { testFramework: { config?: JasmineConfig }}) => {
testRunnerHtml: (_path: any, config: { testFramework: { config?: JasmineConfig } }) => {
const testFramework = {
path: './node_modules/jasmine-core/lib/jasmine-core/jasmine.js',
config: {
Expand All @@ -35,7 +35,7 @@ export const jasmineTestRunnerConfig = () => {
<script>${fs.readFileSync(jasminePath, 'utf8')}</script>
<script type="module">
import { getConfig, sessionStarted, sessionFinished, sessionFailed } from '@web/test-runner-core/browser/session.js';

const testFramework = {
...${JSON.stringify(testFramework)}
};
Expand All @@ -48,44 +48,146 @@ export const jasmineTestRunnerConfig = () => {
Object.assign(window, jasmineRequire.interface(jasmine, env));
window.onload = function () {};

const findParentNode = (treeNode, result) => {
if (treeNode.id === result.parentSuiteId) {
return treeNode;
} else if (treeNode.suites) {
for (let i = 0; i < treeNode.suites.length; i++) {
const childSuite = treeNode.suites[i];
const elementFound = findParentNode(childSuite, result);
if (elementFound) {
return elementFound;
}
}
}
return null;
};
const findResultNode = (treeNode, result) => {
if (treeNode.id === result.id) {
return treeNode;
}
if (treeNode.tests) {
for (let i = 0; i < treeNode.tests.length; i++) {
const childTest = treeNode.tests[i];
const elementFound = findResultNode(childTest, result);
if (elementFound) {
return elementFound;
}
}
}
if (treeNode.suites) {
for (let i = 0; i < treeNode.suites.length; i++) {
const childSuite = treeNode.suites[i];
const elementFound = findResultNode(childSuite, result);
if (elementFound) {
return elementFound;
}
}
}
return null;
};
const buildTestResults = (jasmineTreeNode) => {
const treeNode = {
name: jasmineTreeNode.name
};
if (jasmineTreeNode.tests) {
treeNode.tests = [];
for (let i = 0; i < jasmineTreeNode.tests.length; i++) {
const jasmineTestNode = jasmineTreeNode.tests[i];
treeNode.tests.push({
name: jasmineTestNode.name,
passed: jasmineTestNode.passed,
skipped: jasmineTestNode.skipped,
duration: jasmineTestNode.duration,
error: jasmineTestNode.errors?.[0]
});
}
}
if (jasmineTreeNode.suites) {
treeNode.suites = [];
for (let i = 0; i < jasmineTreeNode.suites.length; i++) {
const jasmineSuiteNode = jasmineTreeNode.suites[i];
treeNode.suites.push(buildTestResults(jasmineSuiteNode));
}
}
return treeNode;
};

const failedSpecs = [];
const allSpecs = [];
const failedImports = [];

const jasmineRootTreeNode = {
id: null,
name: "",
suites: [],
tests: []
};

env.addReporter({
jasmineStarted: () => {},
suiteStarted: () => {},
specStarted: () => {},
suiteDone: () => {},
jasmineStarted: suiteInfo => {},
suiteStarted: result => {
if (!result.parentSuiteId) {
jasmineRootTreeNode.id = result.id;
jasmineRootTreeNode.name = result.description;
} else {
const nodeFound = findParentNode(jasmineRootTreeNode, result);
if (nodeFound) {
nodeFound.suites.push({
id: result.id,
name: result.description,
suites: [],
tests: []
});
}
}
},
specStarted: result => {
if (!result.parentSuiteId) {
jasmineRootTreeNode.id = result.id;
jasmineRootTreeNode.name = result.description;
} else {
const nodeFound = findParentNode(jasmineRootTreeNode, result);
if (nodeFound) {
nodeFound.tests.push({
id: result.id,
name: result.description
});
}
}
},
specDone: result => {
[...result.passedExpectations, ...result.failedExpectations].forEach(e => {
allSpecs.push({
name: e.description,
passed: e.passed,
});
});
const nodeFound = findResultNode(jasmineRootTreeNode, result);
nodeFound.passed = result.status === "passed";
nodeFound.skipped = false;
nodeFound.duration = result.duration;

if (result.failedExpectations && result.failedExpectations.length > 0) {
nodeFound.errors = [];
for (let i = 0; i < result.failedExpectations.length; i++) {
const e = result.failedExpectations[i];

if (result.status !== 'passed' || result.status !== 'incomplete') {
result.failedExpectations.forEach(e => {
failedSpecs.push({
const testResultError = {
message: result.fullName + ': ' + e.message,
name: e.description,
name: result.description,
stack: e.stack,
expected: e.expected,
actual: e.actual,
});
});
expected: JSON.stringify(e.expected) ?? String(e.expected),
actual: JSON.stringify(e.actual) ?? String(e.actual)
};

failedSpecs.push(testResultError);
nodeFound.errors.push(testResultError);
};
}
},
suiteDone: result => {
const nodeFound = findResultNode(jasmineRootTreeNode, result);
nodeFound.passed = result.status === "passed";
},
jasmineDone: result => {
sessionFinished({
passed: result.overallStatus === 'passed',
errors: [...failedSpecs, ...failedImports],
testResults: {
name: '',
suites: [],
tests: allSpecs,
},
testResults: buildTestResults(jasmineRootTreeNode)
});
},
});
Expand Down
5 changes: 5 additions & 0 deletions web-test-runner.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { playwrightLauncher } from '@web/test-runner-playwright';
import { esbuildPlugin } from '@web/dev-server-esbuild';
import { defaultReporter, summaryReporter } from "@web/test-runner";
import { jasmineTestRunnerConfig } from './dist/lib/index.js'; // web-test-runner-jasmine

export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
Expand All @@ -25,4 +26,8 @@ export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
lines: 90,
},
},
reporters: [
defaultReporter(),
summaryReporter(),
],
});