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

refactor(testing): align additional error messages #5810

Merged
merged 1 commit into from
Aug 26, 2024
Merged
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
6 changes: 4 additions & 2 deletions testing/_test_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ export class TestSuiteInternal<T> implements TestSuite<T> {

const { suite } = describe;
if (suite && !TestSuiteInternal.suites.has(suite.symbol)) {
throw new Error("suite does not represent a registered test suite");
throw new Error(
"Cannot construct Test Suite: suite does not represent a registered test suite",
);
}
const testSuite = suite
? TestSuiteInternal.suites.get(suite.symbol)
Expand All @@ -101,7 +103,7 @@ export class TestSuiteInternal<T> implements TestSuite<T> {
const value = fn() as any;
if (value instanceof Promise) {
throw new Error(
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
'Returning a Promise from "describe" is not supported: tests must be defined synchronously',
);
}
} finally {
Expand Down
6 changes: 3 additions & 3 deletions testing/bdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
export function it<T>(...args: ItArgs<T>) {
if (TestSuiteInternal.runningCount > 0) {
throw new Error(
"cannot register new test cases after already registered test cases start running",
"Cannot register new test cases after already registered test cases start running",

Check warning on line 565 in testing/bdd.ts

View check run for this annotation

Codecov / codecov/patch

testing/bdd.ts#L565

Added line #L565 was not covered by tests
);
}
const options = itDefinition(...args);
Expand Down Expand Up @@ -717,7 +717,7 @@
if (!TestSuiteInternal.current) {
if (TestSuiteInternal.started) {
throw new Error(
"cannot add global hooks after a global test is registered",
"Cannot add global hooks after a global test is registered",

Check warning on line 720 in testing/bdd.ts

View check run for this annotation

Codecov / codecov/patch

testing/bdd.ts#L720

Added line #L720 was not covered by tests
);
}
TestSuiteInternal.current = new TestSuiteInternal({
Expand Down Expand Up @@ -1070,7 +1070,7 @@
): TestSuite<T> {
if (TestSuiteInternal.runningCount > 0) {
throw new Error(
"cannot register new test suites after already registered test cases start running",
"Cannot register new test suites after already registered test cases start running",

Check warning on line 1073 in testing/bdd.ts

View check run for this annotation

Codecov / codecov/patch

testing/bdd.ts#L1073

Added line #L1073 was not covered by tests
);
}
const options = describeDefinition(...args);
Expand Down
2 changes: 1 addition & 1 deletion testing/bdd_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2055,7 +2055,7 @@ Deno.test("describe()", async (t) => {
// deno-lint-ignore no-explicit-any
() => describe("async describe", (async () => {}) as any),
Error,
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
'Returning a Promise from "describe" is not supported: tests must be defined synchronously',
);
},
);
Expand Down
38 changes: 28 additions & 10 deletions testing/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,9 @@ function functionSpy<
restore: {
enumerable: true,
value: () => {
throw new MockError("Function cannot be restored");
throw new MockError(
"Cannot restore: function cannot be restored",
);
},
},
});
Expand Down Expand Up @@ -643,15 +645,21 @@ function methodSpy<
Return,
>(self: Self, property: keyof Self): MethodSpy<Self, Args, Return> {
if (typeof self[property] !== "function") {
throw new MockError("Property is not an instance method");
throw new MockError(
"Cannot spy: property is not an instance method",
);
}
if (isSpy(self[property])) {
throw new MockError("Already spying on instance method");
throw new MockError(
"Cannot spy: already spying on instance method",
);
}

const propertyDescriptor = Object.getOwnPropertyDescriptor(self, property);
if (propertyDescriptor && !propertyDescriptor.configurable) {
throw new MockError("Cannot spy on non-configurable instance method");
throw new MockError(
"Cannot spy: non-configurable instance method",
);
}

const original = self[property] as unknown as (
Expand Down Expand Up @@ -690,7 +698,9 @@ function methodSpy<
enumerable: true,
value: () => {
if (restored) {
throw new MockError("Instance method already restored");
throw new MockError(
"Cannot restore: instance method already restored",
);
}
if (propertyDescriptor) {
Object.defineProperty(self, property, propertyDescriptor);
Expand Down Expand Up @@ -767,7 +777,9 @@ function constructorSpy<
static readonly calls = calls;
static readonly restored = false;
static restore() {
throw new MockError("Constructor cannot be restored");
throw new MockError(
"Cannot restore: constructor cannot be restored",
);
}
} as ConstructorSpy<Self, Args>;
return spy;
Expand Down Expand Up @@ -1082,15 +1094,19 @@ export function stub<
func?: (this: Self, ...args: Args) => Return,
): Stub<Self, Args, Return> {
if (self[property] !== undefined && typeof self[property] !== "function") {
throw new MockError("Property is not an instance method");
throw new MockError(
"Cannot stub: property is not an instance method",
);
}
if (isSpy(self[property])) {
throw new MockError("Already spying on instance method");
throw new MockError(
"Cannot stub: already spying on instance method",
);
}

const propertyDescriptor = Object.getOwnPropertyDescriptor(self, property);
if (propertyDescriptor && !propertyDescriptor.configurable) {
throw new MockError("Cannot stub non-configurable instance method");
throw new MockError("Cannot stub: non-configurable instance method");
}

const fake = func ?? (() => {}) as (this: Self, ...args: Args) => Return;
Expand Down Expand Up @@ -1135,7 +1151,9 @@ export function stub<
enumerable: true,
value: () => {
if (restored) {
throw new MockError("Instance method already restored");
throw new MockError(
"Cannot restore: instance method already restored",
);
}
if (propertyDescriptor) {
Object.defineProperty(self, property, propertyDescriptor);
Expand Down
34 changes: 17 additions & 17 deletions testing/mock_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Deno.test("spy()", () => {
assertThrows(
() => func.restore(),
MockError,
"Function cannot be restore",
"Cannot restore: function cannot be restored",
);
assertEquals(func.restored, false);
});
Expand Down Expand Up @@ -125,7 +125,7 @@ Deno.test("spy() works on function", () => {
assertThrows(
() => func.restore(),
MockError,
"Function cannot be restored",
"Cannot restore: function cannot be restored",
);
assertEquals(func.restored, false);

Expand Down Expand Up @@ -239,7 +239,7 @@ Deno.test("spy() works on instance method", () => {
assertThrows(
() => func.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(func.restored, true);
});
Expand Down Expand Up @@ -277,7 +277,7 @@ Deno.test("spy() works on instance method symbol", () => {
assertThrows(
() => func.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(func.restored, true);
});
Expand Down Expand Up @@ -342,7 +342,7 @@ Deno.test("spy() works on instance method property descriptor", () => {
assertThrows(
() => action.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(action.restored, true);
});
Expand Down Expand Up @@ -444,7 +444,7 @@ Deno.test("spy() supports explicit resource management", () => {
if (funcRef) funcRef.restore();
},
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(funcRef.restored, true);
}
Expand Down Expand Up @@ -478,7 +478,7 @@ Deno.test("spy() works on constructor", () => {
assertThrows(
() => PointSpy.restore(),
MockError,
"Constructor cannot be restored",
"Cannot restore: constructor cannot be restored",
);
});

Expand Down Expand Up @@ -546,7 +546,7 @@ Deno.test("spy() throws when try spying already spied method", () => {
assertThrows(
() => spy(obj, "fn"),
MockError,
"Already spying on instance method",
"Cannot spy: already spying on instance method",
);
});

Expand All @@ -556,7 +556,7 @@ Deno.test("spy() throws when the property is not a method", () => {
// deno-lint-ignore no-explicit-any
() => spy(obj as any, "fn"),
MockError,
"Property is not an instance method",
"Cannot spy: property is not an instance method",
);
});

Expand All @@ -566,7 +566,7 @@ Deno.test("spy() throws when the property is not configurable", () => {
assertThrows(
() => spy(obj, "fn"),
MockError,
"Cannot spy on non-configurable instance method",
"Cannot spy: non-configurable instance method",
);
});

Expand Down Expand Up @@ -602,7 +602,7 @@ Deno.test("stub()", () => {
assertThrows(
() => func.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(func.restored, true);
});
Expand Down Expand Up @@ -640,7 +640,7 @@ Deno.test("stub() works on function", () => {
assertThrows(
() => func.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(func.restored, true);
});
Expand Down Expand Up @@ -684,7 +684,7 @@ Deno.test("stub() supports explicit resource management", () => {
if (funcRef) funcRef.restore();
},
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(funcRef.restored, true);
}
Expand Down Expand Up @@ -727,7 +727,7 @@ Deno.test("stub() handles non existent function", () => {
assertThrows(
() => func.restore(),
MockError,
"Instance method already restored",
"Cannot restore: instance method already restored",
);
assertEquals(func.restored, true);
});
Expand Down Expand Up @@ -797,7 +797,7 @@ Deno.test("stub() throws when the property is not a method", () => {
// deno-lint-ignore no-explicit-any
() => stub(obj as any, "fn"),
MockError,
"Property is not an instance method",
"Cannot stub: property is not an instance method",
);
});

Expand All @@ -807,7 +807,7 @@ Deno.test("stub() throws when try stubbing already stubbed method", () => {
assertThrows(
() => stub(obj, "fn"),
MockError,
"Already spying on instance method",
"Cannot stub: already spying on instance method",
);
});

Expand All @@ -817,7 +817,7 @@ Deno.test("stub() throws then the property is not configurable", () => {
assertThrows(
() => stub(obj, "fn"),
MockError,
"Cannot stub non-configurable instance method",
"Cannot stub: non-configurable instance method",
);
});

Expand Down
Loading