我试图用Jest CLI测试一个承诺,这个代码在它在浏览器中运行时应该执行。但是我想开始为它编写测试。
class ListCollection {
constructor() {
this.items = new Array();
}
addItem(string) {
const addItemPromise = new Promise(
function (resolve, reject) {
// set up async getting like a XMLHttpRequest
setTimeout( () => {
this.items.push(string);
resolve(string);
}.bind(this), 2000);
}.bind(this)
);
return addItemPromise;
}
}
目前,我正试图让这个非常基本的测试工作。我根据documentation which links to jasmine-pit使用pit
进行测试。
jest.dontMock('../collections');
import Collection from '../collections';
describe("Collection", () => {
let collection;
beforeEach(() => {
collection = new Collection();
});
describe("Adding an item", () => {
pit('Spec 1', function () {
return collection.addItem("Hello World").then(function (string) {
expect(string).toBe("Hello World");
});
});
});
})
当我使用babel-node ./node_modules/.bin/jest
运行我的测试时,上面的测试因为它的堆栈跟踪而失败。值得注意的是,我得到Promise is not defined
。
Rollos-Mac-Pro:react-boilerplate Rollo$ babel-node ./node_modules/.bin/jest
Using Jest CLI v0.4.0
FAIL app/collections/__tests__/collectionTests.js
ReferenceError: /Users/Rollo/react-boilerplate/app/collections/__tests__/collectionTests.js: /Users/Rollo/react-boilerplate/app/collections/collections.js: **Promise is not defined**
at ListCollection.addItem (/Users/Rollo/react-boilerplate/app/collections/collections.js:24:34)
at /Users/Rollo/react-boilerplate/app/collections/collections.js:48:12
at Object.runContentWithLocalBindings (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/lib/utils.js:361:17)
at Loader._execModule (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:250:9)
at Loader.requireModule (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:916:12)
at Loader.requireModuleOrMock (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:937:17)
at /Users/Rollo/react-boilerplate/app/collections/__tests__/collectionTests.js:7:34
at Object.runContentWithLocalBindings (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/lib/utils.js:361:17)
at Loader._execModule (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:250:9)
at Loader.requireModule (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/HasteModuleLoader/HasteModuleLoader.js:916:12)
at jasmineTestRunner (/Users/Rollo/react-boilerplate/node_modules/jest-cli/src/jasmineTestRunner/jasmineTestRunner.js:242:16)
at /Users/Rollo/react-boilerplate/node_modules/jest-cli/src/TestRunner.js:371:12
at _fulfilled (/Users/Rollo/react-boilerplate/node_modules/jest-cli/node_modules/q/q.js:798:54)
at self.promiseDispatch.done (/Users/Rollo/react-boilerplate/node_modules/jest-cli/node_modules/q/q.js:827:30)
at Promise.promise.promiseDispatch (/Users/Rollo/react-boilerplate/node_modules/jest-cli/node_modules/q/q.js:760:13)
at /Users/Rollo/react-boilerplate/node_modules/jest-cli/node_modules/q/q.js:574:44
at flush (/Users/Rollo/react-boilerplate/node_modules/jest-cli/node_modules/q/q.js:108:17)
at /Users/Rollo/react-boilerplate/node_modules/jest-cli/src/lib/FakeTimers.js:325:7
at process._tickCallback (node.js:448:13)
我不知道如何解决这个问题。我使用的节点版本必须是0.10.x,否则我无法运行Jest-CLI。但节点版本0.10.x并没有承诺。我也不了解Jest-CLI如何使用我的ES6类和语法,但却没有认识到Promises。
知道如何在我的设置中获得承诺吗?
修改
我已将es6-promise
polyfill添加到我的测试文件的顶部,并标记它不会被模拟。这提供了足够的解决方案。
jest.dontMock('es6-promise');
require('es6-promise').polyfill();
答案 0 :(得分:1)
输出提到babel-node
,这意味着babel
作为需求挂钩运行,动态地将ES6编译为ES5。您需要为本机承诺包含一个垫片,并将其公开为global.Promise
。我推荐这个:https://github.com/jakearchibald/es6-promise
答案 1 :(得分:0)
用babel-jest替换jest scriptPreprocessor
应该适用于所有es6可比性问题