Javascript 运行规范时未激活程序包

Javascript 运行规范时未激活程序包,javascript,jasmine,atom-editor,specifications,Javascript,Jasmine,Atom Editor,Specifications,我已经为Atom创建了一个名为的包,它跳转到下一个可折叠行,并在命令quick fold:fold next上折叠它。我想开始进入Atom规范,这样我就可以在这个包上运行测试,但是我遇到了一个问题,在运行规范时,这个包从来没有被激活过 quick-fold-spec.js: describe('QuickFold package', () => { let editor, editorView; beforeEach(async () => { a

我已经为Atom创建了一个名为的包,它跳转到下一个可折叠行,并在命令
quick fold:fold next
上折叠它。我想开始进入Atom规范,这样我就可以在这个包上运行测试,但是我遇到了一个问题,在运行规范时,这个包从来没有被激活过

quick-fold-spec.js

describe('QuickFold package', () => {

    let editor, editorView;

    beforeEach(async () => {
        await atom.packages.activatePackage('language-javascript');
        await atom.workspace.open('sample.js');

        editor = atom.workspace.getActiveTextEditor();
        editorView = atom.views.getView(editor);
    });

    describe('when the specs are run', () => {
        it('opens the sample file', () => expect(!editor.isEmpty()).toBe(true));
        // true
    });

    describe('when the quick-fold:fold-next event is triggered', () => {
        beforeEach(async () => {
            // Try to activate package by dispatching command:
            atom.commands.dispatch(editorView, 'quick-fold:fold-next');
            await atom.packages.activatePackage('quick-fold'); // Never resolves
        });

        it('activates the package', () => {
            expect(atom.packages.isPackageActive('quick-fold')).toBe(true);
        });

        it('moves the cursor to a different line number', () => {
            expect(editor.getCursorScreenPosition().row).not.toBe(0);
        });
    });
});
但是,
atom.packages.activatePackage('quick-fold')
永远无法解析。程序包未激活,而是超时:

超时:等待规范承诺解决5000毫秒后超时

激活命令在
package.json中设置:

  "activationCommands": {
    "atom-workspace": "quick-fold:fold-next"
  },
因此,发送此消息应该激活包,然后
等待atom.packages.activatePackage('quick-fold')
应该解决。但是光标位置不会改变,包也不会被激活


(请注意,
atom.packages.activatePackage('quick-fold')
仅仅是一个承诺-它不会激活包,但会在包被激活时解决。)

与通常的情况一样,我最终找到了答案

1.
beforeach()
函数缺少一个
runs()
应该是

    beforeEach(async () => {
        await atom.packages.activatePackage('language-javascript');
        await atom.workspace.open('sample.js');

        runs(() => {
            editor = atom.workspace.getActiveTextEditor();
            editorView = atom.views.getView(editor);

            return activationPromise = atom.packages.activatePackage('quick-fold');
        });
    });
其中
runs()
函数在激活包时返回承诺

发件人:

规范是通过定义一组具有对
运行的调用的块来编写的,这些调用通常以异步调用完成

我现在太累了,无法确切理解这意味着什么,但这听起来是真的

2.
package.json中的激活命令应基于
atom文本编辑器
即,不
atom工作区

  "activationCommands": {
    "atom-text-editor": "quick-fold:fold-next"
  },
这可能是因为我们有
atom.commands.dispatch(editorView,'quick fold:fold next')
命令被分派到
editorView=atom.views.getView(editor)
而不是atom工作区


重构——实现重构的“标准”方式
describe('QuickFold package', () => {

    let editor, editorView, activationPromise;

    const foldNext = async (callback) => {
        atom.commands.dispatch(editorView, 'quick-fold:fold-next');
        await activationPromise;
        return callback();
    };

    beforeEach(async () => {
        await atom.packages.activatePackage('language-javascript');
        await atom.workspace.open('sample.js');

        runs(() => {
            editor = atom.workspace.getActiveTextEditor();
            editorView = atom.views.getView(editor);

            return activationPromise = atom.packages.activatePackage('quick-fold');
        });
    });

    describe('when the specs are run', () => {
        it('opens the sample file', () => expect(!editor.isEmpty()).toBe(true));
    });

    describe('when the quick-fold:fold-next event is triggered', () => {
        it('activates the package', () => {
            return foldNext(() => expect(atom.packages.isPackageActive('quick-fold')).toBe(true));
        });

        it('moves the cursor to a different line number', () => {
            return foldNext(() => expect(editor.getCursorScreenPosition().row).not.toBe(0));
        });
    });
});