运行 规格时包未激活

Package not activating when running specs

我为 Atom 创建了一个名为 quick-fold 的程序包,它会跳转到下一个可折叠行并根据命令 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') 只是一个承诺 - 它不会激活程序包,但会在程序包被激活时解决。)

像往常一样,我最后想通了...

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() 函数 returns 激活包的承诺。

来自 the docs:

Specs are written by defining a set of blocks with calls to runs, which usually finish with an asynchronous call.

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

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 工作区。


重构 - "standard" 的重构方式

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