【问题标题】:how to clear global var between tests in nodejs?如何清除nodejs中测试之间的全局变量?
【发布时间】:2020-07-18 19:17:15
【问题描述】:

我有 2 个测试,每个测试都通过了,但是当我一起运行它们时,第二个测试失败了。 我有这个全局变量(CURR_GUID),我想在测试之间清除它,但我不知道该怎么做。 由于在两次测试之间没有清除,所以它的值仍然是“1111111”而不是null。

有什么想法吗?

W 类

"use strict";

var CURR_GUID = null;

class W {
    constructor() {
        console.log("W ctor")
    }

    async getDataFromDb() {
        if (CURR_GUID) {
            return CURR_GUID;
        } else {
            this.db = require("./Connection").getDb();
            console.log("W getDataFromDb")
            let res = null;
            res = await this.db.one("SELECT guid FROM table", ["NONE"]);
            return res.guid;
        }
    }

    async saveGuid(guid) {
        this.db = require("./Connection").getDb();
        await this.db.none("update table set guid=$1", [guid]);
        CURR_GUID = guid;
        console.log("currr is" + CURR_GUID);
    }
}

module.exports = W;

X 类

"use strict";

const wDao = new (require("../db/W"))();

class X {
    async getGuid() {
        console.log("X getGuid")
        let guid = await wDao.getDataFromDb();
        console.log("got guid " + guid);

        await wDao.saveGuid(guid);
        return guid;
    }
}

module.exports = X;

Test.spec.js

const chai = require("chai");
const expect = chai.expect;
const X = require("../src/service/X");
const Connection = require("../src/db/Connection");
const sinon = require("sinon");

describe("component_test", function () {
    afterEach(function () {
        this.dbStub.restore();
    });

    it("X_component_test", async function () {
        var db = {
            one: async () => {
                return {
                    guid: '1111111',
                    created_on: "123"
                }
            },
            none: async () => {
            }
        };
        this.dbStub = sinon.stub(Connection, "getDb").returns(db);

        var dao = new X();
        var guid = await dao.getGuid();
        expect(guid).to.equal('1111111');
    });

    it("X_component_test_2", async function () {
        var db = {
            one: async () => {
                return {
                    guid: '222222',
                    created_on: "123"
                }
            },
            none: async () => {
            }
        };
        this.dbStub = sinon.stub(Connection, "getDb").returns(db);

        var dao = new X();
        var guid = await dao.getGuid();
        expect(guid).to.equal('222222');
    });
});

【问题讨论】:

    标签: node.js unit-testing mocha.js chai


    【解决方案1】:

    虽然在afterEach 钩子中删除require.cache 中的W 和X 模块应该可以达到预期的结果,但最好稍微修改一下源代码,以便可以在没有缓存操作的情况下对其进行测试。

    W 类

    CURR_GUID 变量转换为实例属性。

    "use strict";
       
    class W {
        constructor() {
            console.log("W ctor");
            this.CURR_GUID = null;
        }
    
        async getDataFromDb() {
            if (this.CURR_GUID) {
                return this.CURR_GUID;
            } else {
                this.db = require("./Connection").getDb();
                console.log("W getDataFromDb")
                let res = null;
                res = await this.db.one("SELECT guid FROM table", ["NONE"]);
                return res.guid;
            }
        }
    
        async saveGuid(guid) {
            this.db = require("./Connection").getDb();
            await this.db.none("update table set guid=$1", [guid]);
            this.CURR_GUID = guid;
            console.log("currr is" + this.CURR_GUID);
        }
    }
    
    module.exports = W;
    

    X 类

    wDao从变量变成实例属性

    "use strict";
    
    const WDao = require("../db/W");
    
    class X {
        constructor() {
            this.wDao = new WDao();
        } 
    
        async getGuid() {
            console.log("X getGuid")
            let guid = await this.wDao.getDataFromDb();
            console.log("got guid " + guid);
    
            await this.wDao.saveGuid(guid);
            return guid;
        }
    }
    
    module.exports = X;
    

    这样,每个测试都应该使用不依赖于闭包状态的 W 和 X 的新实例。

    【讨论】:

      猜你喜欢
      • 2014-05-04
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2013-01-14
      相关资源
      最近更新 更多