【问题标题】:Isolating js files that are needed for one test suite隔离一个测试套件所需的 js 文件
【发布时间】:2017-12-13 15:00:56
【问题描述】:

我正在为我的测试堆栈使用 karma/mocha/chrome headless。

--js 1
document.addEventListener('test', ...);
do sth more.

--js 2
 document.addEventListener('test', ...);    
 do sth more differently.

-- test suite 1
   require(test1)
   i am dispatching the test event here to test js 1

-- test suite 2
   require(test2)
   I am dispatchinh the test event here to test js 2

问题是这两个文件现在都将在全球范围内可用。当我运行测试套件 2 时,js1 和 js2 事件都会监听我的调度,因为 js1 仍然是全局可用的。

我的完美场景是。将 js1 加载到测试套件 1 并将其封装在那里。在运行测试后,Hacky 方法将删除它。

我的问题是,如何确保所需的 js 文件在一个测试套件的范围内本地加载?基本上只加载特定测试套件的必要文件,并将它们与其他测试套件隔离。

谢谢。

【问题讨论】:

    标签: javascript requirejs mocha.js karma-runner google-chrome-headless


    【解决方案1】:

    如果我正确理解了这个问题,您希望将您的模块范围限定为相关的测试文件吗?

    如果是这样,那么您可以像这样加载您的模块:

    const sharedModule = require('some/shared/module');
    
    describe('some test', function() {
        const scopedModule = require('some/path/to/module');
    
        it('should...', function() {
            // Test code...
        })
    
    )
    

    这将导致您的模块仅在测试开始执行时才被加载,并被限定在该块内。

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多