首页 > 解决方案 > 运行规范时包未激活

问题描述

我为 Atom 创建了一个名为quick-fold的包,它跳转到下一个可折叠行并在 command 上折叠它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')永远解决不了。该软件包未激活,而是超时:

timeout: timed out after 5000 msec waiting for spec promise to resolve

规格套件屏幕截图

激活命令设置在package.json

  "activationCommands": {
    "atom-workspace": "quick-fold:fold-next"
  },

所以调度这个应该激活包,然后await atom.packages.activatePackage('quick-fold')应该解决。但是光标位置没有改变,包也没有被激活。

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

标签: javascriptjasmineatom-editorspecifications

解决方案


通常情况下,我最终想通了...

1.beforeEach()功能缺失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()函数在激活包时返回一个承诺。

文档

规范是通过定义一组调用 的块来编写的runs,通常以异步调用结束。

我现在太累了,无法准确理解这意味着什么,但在这里听起来很真实。

2.里面的激活命令package.json要根据atom-text-editor

即,不atom-workspace

  "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));
        });
    });
});

推荐阅读