Jest.js:布尔值返回值在Jest中以{}返回

时间:2019-12-12 18:45:23

标签: typescript jestjs

我在下面的返回值之前记录了false的期望值,这使我感到困惑,因为我不知道{}的来源。有人知道原因吗?

 FAIL  functions/src/classes/__tests__/ScriptTag.test.ts
  constructor
    ✕ exists() returns false for no matching scriptTag from fake fetch (13ms)

  ● constructor › exists() returns false for no matching scriptTag from fake fetch

    expect(received).toStrictEqual(expected) // deep equality

    Expected: false
    Received: {}

      168 |     const scriptTagTester = new ScriptTagTester("","", "https://firebaseDomain.com/scriptTag.js");
      169 | 
    > 170 |     await expect(scriptTagTester.exists()).toStrictEqual(false);
          |                                            ^
      171 | 
      172 |   })
      173 |   

  console.log functions/src/classes/ScriptTag.ts:79
    [ 'somescript.js', 'somescript2.js', 'somescript3.js', 'scriptTag.js' ] =====scriptTagFileNames=====

  console.log functions/src/classes/ScriptTag.ts:81
    true =====found===== // this is for a prior test and probably unrelated

  console.log functions/src/classes/ScriptTag.ts:79
    [ 'somescript.js', 'somescript2.js' ] =====scriptTagFileNames=====

  console.log functions/src/classes/ScriptTag.ts:81
    false =====found=====

Test Suites: 1 failed, 1 total
Tests:       1 failed, 6 passed, 7 total

这里是测试类和目标类。我正在扩展目标类,以使其所有方法公开进行测试。

ScriptTag.test.js

  test("exists() returns false for no matching scriptTag from fake fetch", async() => {
    jest.spyOn(ScriptTagTester.prototype, "fetchAllScriptTags")
      .mockResolvedValueOnce(dummyScriptTagsMissingTarget);
    const scriptTagTester = new ScriptTagTester("","", "https://firebaseDomain.com/scriptTag.js");

    await expect(scriptTagTester.exists()).toStrictEqual(false);

  })

ScriptTag.ts

  protected async exists(): Promise<boolean> {
    const scriptTags: ScriptTagObject[] = await this.fetchAllScriptTags();

    const scriptTagFileNames: string[] = scriptTags.map((scriptTagRecord: ScriptTagObject) => {
      const tagFileName: string = this.getFileName(scriptTagRecord.src);
      return tagFileName;
    });

    // if has an index, return true, else false
    console.log(scriptTagFileNames, `=====scriptTagFileNames=====`);
    const fileIsFound: boolean = Boolean(scriptTagFileNames.indexOf(this.localFileName) > -1);
    console.log(fileIsFound, `=====found=====`);
    return fileIsFound;
  }

1 个答案:

答案 0 :(得分:1)

在此docs中,您应该在.resolves函数之后使用expect

短弓箭手:

await expect(scriptTagTester.exists()).resolves.toStrictEqual(false);

例如

ScriptTag.ts

export interface ScriptTagObject {
  src: string;
}

export class ScriptTag {
  localFileName = 'b.js';

  constructor(a, b, c) {}

  protected async exists(): Promise<boolean> {
    const scriptTags: ScriptTagObject[] = await this.fetchAllScriptTags();

    const scriptTagFileNames: string[] = scriptTags.map((scriptTagRecord: ScriptTagObject) => {
      const tagFileName: string = this.getFileName(scriptTagRecord.src);
      return tagFileName;
    });

    console.log(scriptTagFileNames, `=====scriptTagFileNames=====`);
    const fileIsFound: boolean = Boolean(scriptTagFileNames.indexOf(this.localFileName) > -1);
    console.log(fileIsFound, `=====found=====`);
    return fileIsFound;
  }

  protected async fetchAllScriptTags(): Promise<ScriptTagObject[]> {
    return [{ src: 'a' }];
  }

  private getFileName(src) {
    return src;
  }
}

ScriptTag.test.ts

import { ScriptTag, ScriptTagObject } from './ScriptTag';

class ScriptTagTester extends ScriptTag {
  constructor(a, b, c) {
    super(a, b, c);
  }
}

describe('ScriptTag', () => {
  test('exists() returns false for no matching scriptTag from fake fetch', async () => {
    const dummyScriptTagsMissingTarget: ScriptTagObject[] = [
      { src: 'somescript.js' },
      { src: 'somescript2.js' },
      { src: 'somescript3.js' },
      { src: 'scriptTag.js' },
    ];
    jest
      .spyOn(ScriptTagTester.prototype as any, 'fetchAllScriptTags')
      .mockResolvedValueOnce(dummyScriptTagsMissingTarget);

    const scriptTagTester = new ScriptTagTester('', '', 'https://firebaseDomain.com/scriptTag.js');
    await expect((scriptTagTester as any).exists()).resolves.toStrictEqual(false);
  });
});

带有覆盖率报告的单元测试结果:

 PASS  src/stackoverflow/59310993/ScriptTag.test.ts
  ScriptTag
    ✓ exists() returns false for no matching scriptTag from fake fetch (18ms)

  console.log src/stackoverflow/59310993/ScriptTag.ts:3266
    [ 'somescript.js',
      'somescript2.js',
      'somescript3.js',
      'scriptTag.js' ] '=====scriptTagFileNames====='

  console.log src/stackoverflow/59310993/ScriptTag.ts:3274
    false '=====found====='

--------------|----------|----------|----------|----------|-------------------|
File          |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files     |    94.44 |      100 |    83.33 |    93.75 |                   |
 ScriptTag.ts |    94.44 |      100 |    83.33 |    93.75 |                25 |
--------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        5.86s, estimated 16s

源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59310993