javascript - 运行规范时包未激活
问题描述
我为 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')
只是一个承诺——它不会激活包,但会在包被激活时解决。)
解决方案
通常情况下,我最终想通了...
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));
});
});
});
推荐阅读
- java - Eclipse - 创建一个新模板以添加“静态”字
- python - 是否可以使用reduce对列表进行排序?
- node.js - 节点 ssh2 到 ssh 和 sudo su 作为用户并运行命令
- facebook-prophet - 使用 fb-prophet 包在 R 中使用其他回归器按组进行预测
- powershell - 将多个文件的名称更改为文件名的一部分
- javascript - 在 JavaScript 中获取子节点的值
- algorithm - 线性时间深度优先搜索的时间复杂度
- oracle-adf - 在 ADF 表中启用滚动+分页
- sql - 如果 subId 是其他项目的 id,则从同一个表中检索数据
- ios - 更新按钮标题