DenoDDeno
Powered by
elvitinE
Denoโ€ข3y agoโ€ข
2 replies
elvitin

Strange behavior of the assertRejects() method.

Behavior of the
assertRejects()
assertRejects()
method.

assertRejects()
assertRejects()
fails when a non-stubbed method is called before the stubbed
method.

Let's look at the following example:

// main.ts
export class MainClass {
  async mainFn(): Promise<void> {
    await this.foo();
    await this.bar();
  }

  foo(): Promise<string> {
    return Promise.resolve("foo");
  }

  bar(): Promise<string> {
    return Promise.resolve("bar");
  }
}
// main.ts
export class MainClass {
  async mainFn(): Promise<void> {
    await this.foo();
    await this.bar();
  }

  foo(): Promise<string> {
    return Promise.resolve("foo");
  }

  bar(): Promise<string> {
    return Promise.resolve("bar");
  }
}


// main_test.ts
import { describe } from "https://deno.land/std@0.212.0/testing/bdd.ts";
import { stub } from "https://deno.land/std@0.212.0/testing/mock.ts";
import { assertRejects } from "https://deno.land/std@0.212.0/assert/mod.ts";
import { MainClass } from "./main.ts";

describe("unit test", () => {
  const mainObj = new MainClass();
  const barStub = stub(
    mainObj,
    "bar",
    () => new Promise((_, reject) => reject(new Error())),
  );

  assertRejects(() => mainObj.mainFn(), "yeah, bar() rejects with Error");
  barStub.restore();
});
// main_test.ts
import { describe } from "https://deno.land/std@0.212.0/testing/bdd.ts";
import { stub } from "https://deno.land/std@0.212.0/testing/mock.ts";
import { assertRejects } from "https://deno.land/std@0.212.0/assert/mod.ts";
import { MainClass } from "./main.ts";

describe("unit test", () => {
  const mainObj = new MainClass();
  const barStub = stub(
    mainObj,
    "bar",
    () => new Promise((_, reject) => reject(new Error())),
  );

  assertRejects(() => mainObj.mainFn(), "yeah, bar() rejects with Error");
  barStub.restore();
});


execute:
deno test
deno test


output:

FAILED | 0 passed | 1 failed (0ms)

error: Test failed
FAILED | 0 passed | 1 failed (0ms)

error: Test failed


Note that
bar()
bar()
is stubbed to reject the promise with an error. However,
bar()
bar()
only rejects with an error if
foo()
foo()
is not called:

// main.ts
export class MainClass {
  async mainFn(): Promise<void> {
    //await this.foo(); // <-- foo() is not called
    await this.bar();
  }
//rest of code...
// main.ts
export class MainClass {
  async mainFn(): Promise<void> {
    //await this.foo(); // <-- foo() is not called
    await this.bar();
  }
//rest of code...

execute:
deno test
deno test


output:

Check file:///tmp/stdstub/main_test.ts
running 1 test from ./main_test.ts
unit test ... ok (1ms)

ok | 1 passed | 0 failed (4ms)
Check file:///tmp/stdstub/main_test.ts
running 1 test from ./main_test.ts
unit test ... ok (1ms)

ok | 1 passed | 0 failed (4ms)


What am I doing wrong? Is this a bug in the
stub()
stub()
method, or what am I not
understanding?
Deno banner
DenoJoin
Chat about Deno, a modern runtime for JavaScript and TypeScript.
20,934Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Deno runs the whole script instead of the method
KotkoroidKKotkoroid / help
17mo ago
LSP acting strange
shultz๐Ÿ‡ฎ๐Ÿ‡ฑSshultz๐Ÿ‡ฎ๐Ÿ‡ฑ / help
2y ago
Deno.run Strange Issue
jeff.hykinJjeff.hykin / help
4y ago