Giter Club home page Giter Club logo

Comments (6)

tumile avatar tumile commented on July 18, 2024

So I tried fixing this

runTests = async (): Promise<void> => {
    const promises: Promise<void>[] = [];
    this.tests.forEach((test) => {
      // wait for both beforeEachs and test.func
      const promise = Promise.all(this.beforeEachs.map((f) => f())).then(() =>
        test.func()
      );
      promises.push(promise);
      Deno.test(`[${this.prefix}] ${test.name}`, () => {
        return promise;
      });
    });
    try {
      await Promise.allSettled(promises);
    } finally {
      this.afterAlls.forEach(async (f) => await f());
    }
  };

But the order of execution is not as expected

OK
OK
running 2 tests
test [command] set ... ok (2ms)
test [command] keys ... FAILED (0ms)

The "OK" is from client.flushdb. They are executed before all tests are run. pinging @uki00a for thoughts on how to resolve this 😅. Thanks in advance!

from redis.

uki00a avatar uki00a commented on July 18, 2024

How about the following?

runTests = async (): Promise<void> => {
    const promises: Promise<void>[] = [];

    const defineTest = (test: { name: string, func: TestFunc }) => {
      Deno.test(`[${this.prefix}] ${test.name}`, () => {
        // wait for both beforeEachs and test.func
        const promise = this.beforeEachs.reduce(async (p, f) => {
          await p;
          return f();
        }, Promise.resolve()).then(() => test.func());
        promises.push(promise);
        return promise;
      });
    };

    this.tests.forEach(defineTest);
    try {
      await Promise.allSettled(promises);
    } finally {
      this.afterAlls.forEach(async (f) => await f());
    }
  };

Diff:

 runTests = async (): Promise<void> => {
     const promises: Promise<void>[] = [];
-    this.tests.forEach((test) => {
-      // wait for both beforeEachs and test.func
-      const promise = Promise.all(this.beforeEachs.map((f) => f())).then(() =>
-        test.func()
-      );
-      promises.push(promise);
+
+    const defineTest = (test: { name: string, func: TestFunc }) => {
       Deno.test(`[${this.prefix}] ${test.name}`, () => {
+        // wait for both beforeEachs and test.func
+        const promise = this.beforeEachs.reduce(async (p, f) => {
+          await p;
+          return f();
+        }, Promise.resolve()).then(() => test.func());
+        promises.push(promise);
         return promise;
       });
-    });
+    };
+
+    this.tests.forEach(defineTest);
     try {
       await Promise.allSettled(promises);
     } finally {

from redis.

tumile avatar tumile commented on July 18, 2024

I still got BadResource, which means client is closed before the beforeEach promise resolves

from redis.

tumile avatar tumile commented on July 18, 2024

Actually by putting the promises.push() inside Deno.test, it's run asynchronously and so if you log promises after tests.forEach it's empty

from redis.

uki00a avatar uki00a commented on July 18, 2024

OK, so how about the following code? (deferred() is exported in vendor/https/deno.land/std/async/mod.ts)

runTests = async (): Promise<void> => {
    const promises: Promise<void>[] = [];

    const defineTest = (test: { name: string, func: TestFunc }) => {
      const promise = deferred<void>();
      promises.push(promise);
      Deno.test(`[${this.prefix}] ${test.name}`, async () => {
        try {
          // wait for both beforeEachs and test.func
          await this.beforeEachs.reduce(async (p, f) => {
            await p;
            return f();
          }, Promise.resolve()).then(() => test.func());
          promise.resolve();
        } catch (e) {
          promise.reject(e);
        }
        return promise;
      });
    };

    this.tests.forEach(defineTest);
    try {
      await Promise.allSettled(promises);
    } finally {
      this.afterAlls.forEach(async (f) => await f());
    }
  };

Diff:

     const promises: Promise<void>[] = [];

     const defineTest = (test: { name: string, func: TestFunc }) => {
-      Deno.test(`[${this.prefix}] ${test.name}`, () => {
-        // wait for both beforeEachs and test.func
-        const promise = this.beforeEachs.reduce(async (p, f) => {
-          await p;
-          return f();
-        }, Promise.resolve()).then(() => test.func());
-        promises.push(promise);
+      const promise = deferred<void>();
+      promises.push(promise);
+      Deno.test(`[${this.prefix}] ${test.name}`, async () => {
+        try {
+          // wait for both beforeEachs and test.func
+          await this.beforeEachs.reduce(async (p, f) => {
+            await p;
+            return f();
+          }, Promise.resolve()).then(() => test.func());
+          promise.resolve();
+        } catch (e) {
+          promise.reject(e);
+        }
         return promise;
       });
     };

from redis.

tumile avatar tumile commented on July 18, 2024

The above didn't work either...
So after a few hours this is what I came up with, a little hacky but works. This ensures that beforeEach and tests are run sequentially.

  runTests = async (): Promise<void> => {
    try {
      for (const test of this.tests) {
        let res: void | Error;
        try {
          res = await this.beforeEachs
            .reduce((p, f) => p.then(f), Promise.resolve())
            .then(test.func);
        } catch (err) {
          res = err;
        }
        Deno.test(`[${this.prefix}] ${test.name}`, () => {
          if (res instanceof Error) {
            throw res;
          }
        });
      }
    } finally {
      this.afterAlls.forEach(async (f) => await f());
    }
  };

from redis.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.