Skip to content

Commit

Permalink
fix(codeceptjs): add support for native asserts (fixes #1195, via #1214)
Browse files Browse the repository at this point in the history
  • Loading branch information
baev authored Jan 7, 2025
1 parent ecb126e commit 36c4d2f
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 3 deletions.
10 changes: 7 additions & 3 deletions packages/allure-codeceptjs/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ export class AllureCodeceptJsReporter extends AllureMochaReporter {
// @ts-ignore
if (promise) {
promise.catch((err) => {
if (err instanceof Error) {
if (!err.message && typeof err.inspect === "function") {
// AssertionFailedError doesn't set message attribute
err.message = err.inspect();
}
if (err instanceof Error || err.constructor.name === "Error") {
this.runtime.updateStep(currentStep, (step) => {
step.status = getStatusFromError(err);
step.statusDetails = { ...step.statusDetails, ...getMessageAndTraceFromError(err) };
step.status = getStatusFromError(err as Error);
step.statusDetails = { ...step.statusDetails, ...getMessageAndTraceFromError(err as Error) };
});
}
return Promise.reject(err);
Expand Down
99 changes: 99 additions & 0 deletions packages/allure-codeceptjs/test/spec/assert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { expect, it } from "vitest";
import { Status } from "allure-js-commons";
import { runCodeceptJsInlineTest } from "../utils.js";

it("should support codeceptjs assertions", async () => {
const { tests } = await runCodeceptJsInlineTest({
"nested/login.test.js": `
const { container } = require('codeceptjs')
Feature("login-feature");
Scenario("assert scenario", async ({ I }) => {
await I.pass();
await I.fail();
});
`,
"codecept.conf.js": `
const path = require("node:path");
const { setCommonPlugins } = require("@codeceptjs/configure");
setCommonPlugins();
exports.config = {
tests: "./**/*.test.js",
output: path.resolve(__dirname, "./output"),
plugins: {
allure: {
require: require.resolve("allure-codeceptjs"),
enabled: true,
},
},
helpers: {
Playwright: {
require: "./helper.js",
},
},
};
`,
"helper.js": `
const Helper = require("@codeceptjs/helper");
const { writeFile } = require("fs/promises");
const path = require("path");
const AssertionFailedError = require("./assert.js");
class MyHooksHelper extends Helper {
async pass() {
await Promise.resolve();
}
async fail() {
await Promise.reject(new AssertionFailedError({ actual: "1", expected: "2"}));
}
}
module.exports = MyHooksHelper;
`,
"assert.js": `
function AssertionFailedError(params) {
this.params = params;
this.actual = this.params.actual;
this.expected = this.params.expected;
this.inspect = () => {
return "expect " + this.expected + " but " + this.actual;
}
}
AssertionFailedError.prototype = Object.create(Error.prototype);
AssertionFailedError.constructor = AssertionFailedError;
module.exports = AssertionFailedError;
`,
});

expect(tests).toHaveLength(1);
expect(tests).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "assert scenario",
status: Status.FAILED,
statusDetails: expect.objectContaining({
message: "expect 2 but 1",
}),
steps: [
expect.objectContaining({
name: "I pass",
}),
expect.objectContaining({
name: "I fail",
status: Status.FAILED,
statusDetails: expect.objectContaining({
message: "expect 2 but 1",
}),
}),
],
}),
]),
);
});
1 change: 1 addition & 0 deletions packages/allure-js-commons/src/sdk/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const getStatusFromError = (error: Partial<Error>): Status => {
case error.stack && /@vitest\/expect/gi.test(error.stack):
case error.stack && /playwright\/lib\/matchers\/expect\.js/gi.test(error.stack):
case "matcherResult" in error:
case "inspect" in error && typeof error.inspect === "function":
return Status.FAILED;
default:
return Status.BROKEN;
Expand Down

0 comments on commit 36c4d2f

Please sign in to comment.