簡體   English   中英

開玩笑單元測試 Singleton class Nodejs

[英]Jest Unit Test Singleton class Nodejs

我有下面的代碼,我正在嘗試對 function 進行單元測試,該 function 正在使用 singletonBB1DZC2CA26 的 class 變量

單例類 a.js

class A{
constructor(){
   this.flag = true;
 }
 
setA(){
  this.flag=false;
  }
}

module.exports = new A(); //Singleton

Class 使用 Singleton Class

import A from './a';

class B {
  constructor() {}

  hello() {
    if (A.flag) {
      console.log('1');
    } else {
      console.log('2');
    }
  }
}

b.test.js 的單元測試用例

import A from './a' //Singleton class
import B from './'

describe('Test Hello', () => {
    const b= new B();
    
    beforeEach(() => {      
    });

    afterEach(() => {
        jest.restoreAllMocks();
    })


    test('Test Hello', async () => {        
        try{
            jest.spyOn(A, 'flag')
                .mockResolvedValueOnce(true);
            let output = b.hello();
                    
        }catch(e){
            console.log('Error', e);
        }
    });
});

所以 b.js 中的一行,console.log(1) 沒有被覆蓋在覆蓋率報告中。 嘗試了多種選擇

你的代碼有很多問題:

  1. 您正在測試b.js模塊,而不是a.js模塊。 所以測試文件名應該是b.test.js

  2. 導入的模塊名稱錯誤。 它應該是./a.js ,而不是A.js

  3. 如果要改變實例a的屬性,只需要在測試用例中賦值即可

例如

a.js

class A {
  constructor() {
    this.flag = true;
  }

  setA() {
    this.flag = false;
  }
}

module.exports = new A(); //Singleton

b.js

import a from './a';

export class B {
  constructor() {}

  hello() {
    if (a.flag) {
      console.log('1');
    } else {
      console.log('2');
    }
  }
}

b.test.js

import a from './a';
import { B } from './b';

describe('67335501', () => {
  afterEach(() => {
    jest.restoreAllMocks();
  });

  it('should print "1"', () => {
    const logSpy = jest.spyOn(console, 'log');
    a.flag = true;
    const b = new B();
    b.hello();
    expect(logSpy).toBeCalledWith('1');
  });

  it('should print "2"', () => {
    const logSpy = jest.spyOn(console, 'log');
    a.flag = false;
    const b = new B();
    b.hello();
    expect(logSpy).toBeCalledWith('2');
  });
});

單元測試結果:

 PASS  examples/67335501/b.test.js (10.808 s)
  67335501
    ✓ should print "1" (18 ms)
    ✓ should print "2" (2 ms)

  console.log
    1

      at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

  console.log
    2

      at console.<anonymous> (node_modules/jest-environment-enzyme/node_modules/jest-mock/build/index.js:866:25)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |    87.5 |      100 |      75 |    87.5 |                   
 a.js     |   66.67 |      100 |      50 |   66.67 | 7                 
 b.js     |     100 |      100 |     100 |     100 |                   
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        12.657 s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM