Closed thenbe closed 1 year ago
This is very interesting. Thank you for the reproduction. All that the @Log()
and @LogAll()
decorator should be doing is adding a finally
block to log the method end. Strange that an unhandled error is arising from it.
It does happen in other test runners, so something is getting modified in an unexpected way. I'll try to look into it
I think this is because the @Log()
decorator technically modifies the descriptor,value when it is a promise to add a .finally
. This decorator and test causes the same warning:
const TestDec = () => {
return (target, propertyKey, descriptor) => {
const impl = descriptor.value;
descriptor.value = function (...args) {
const res = impl.apply(this, ...args);
res.finally(() => {
console.log('added finally');
});
return res;
};
return descriptor;
};
};
class Foo {
@TestDec()
foo() {
return Promise.reject('Boo!');
}
}
it('testing plain decorator', async () => {
const foo = new Foo();
await expect(foo.foo()).rejects.toThrow();
});
Not sure what to do about it just yet, but I'll keep searching through some more obscure stuff
I noticed that making either of the following changes will suppress the warning from vitest. Could the issue be related to this: https://typescript-eslint.io/rules/no-floating-promises/
const TestDec = () => {
return (target, propertyKey, descriptor) => {
const impl = descriptor.value;
- descriptor.value = function (...args) {
+ descriptor.value = async function (...args) {
const res = impl.apply(this, ...args);
- res.finally(() => {
+ await res.finally(() => {
console.log('added finally');
});
return res;
};
return descriptor;
};
};
or
const TestDec = () => {
return (target, propertyKey, descriptor) => {
const impl = descriptor.value;
descriptor.value = function (...args) {
const res = impl.apply(this, ...args);
- res.finally(() => {
+ return res.finally(() => {
console.log('added finally');
});
- return res;
};
return descriptor;
};
};
The latter would be preferable, as I don't want to modify synchronous functions to become async. I'll double check this tomorrow and get some test cases written
Is there an existing issue for this?
Current behavior
The tests pass, but when an (a) an error is thrown inside an async function and (b) we're using ogma's
LogAll()
decorator, then vitest will complain.Error:
Minimum reproduction code
https://github.com/thenbe/repro-ogma-vitest
Steps to reproduce
Expected behavior
Vitest should not complain, just like the first test.
Package(s) version
@ogma/cli
:@ogma/common
:@ogma/logger
:@ogma/nestjs-module
: "^5.1.1"@ogma/platform-express
:@ogma/platform-fastify
:@ogma/platform-graphql-fastify
:@ogma/platform-graphql
:@ogma/platform-grpc
:@ogma/platform-kafka
:@ogma/platform-mqtt
:@ogma/platform-nats
:@ogma/platform-rabbitmq
:@ogma/platform-redis
:@ogma/platform-socket.io
:@ogma/platform-tcp
:@ogma/platform-ws
:@ogma/styler
:Node.js version
18.16.1
In which operating systems have you tested?
Other
I haven't used ogma before, so if I can't tell if this is a genuine bug or if I'm doing something I shouldn't.