我为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')
只是一个承诺-它不会激活软件包,但会在激活软件包时解决。)
答案 0 :(得分:0)
通常情况下,我最终都知道了...
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()
函数返回激活软件包的承诺。
来自the docs:
通过定义一组调用
runs
的块来编写规范,这些块通常以异步调用结束。
我现在太累了,无法确切理解这意味着什么,但这在这里是真的。
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));
});
});
});