I'm attempting to test a promise with Jest CLI, this code executes as it should when it's run in the browser. However I want to start writing tests for it.
我正在尝试使用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;
}
}
Currently I'm trying to get this very basic test to work. I'm testing with pit
as per the documentation which links to jasmine-pit.
目前我正试图让这个非常基本的测试工作。我正在根据链接到jasmine-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");
});
});
});
})
When I run my tests with babel-node ./node_modules/.bin/jest
, the test above fails with this as it's stack trace. Notably I get Promise is not defined
.
当我使用babel-node ./node_modules/.bin/jest运行我的测试时,上面的测试因为它的堆栈跟踪而失败。值得注意的是,我得到的Promise没有定义。
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)
I don't know how to fix this. The node version I'm using has to be 0.10.x, otherwise I can't run the Jest-CLI. But node version 0.10.x doesn't have promises. I also don't understand how Jest-CLI works with my ES6 classes and syntax but won't recognize Promises.
我不知道如何解决这个问题。我使用的节点版本必须是0.10.x,否则我无法运行Jest-CLI。但节点版本0.10.x没有承诺。我也不明白Jest-CLI如何使用我的ES6类和语法,但不会识别Promises。
Any idea how to get promises to work in my setup?
知道如何在我的设置中获得承诺吗?
EDIT
I've added the es6-promise
polyfill to the top of my test file and flagged it not to be mocked. This provides an adequate fix.
我已经将es6-promise polyfill添加到我的测试文件的顶部并标记它不被嘲笑。这提供了足够的解决方案。
jest.dontMock('es6-promise');
require('es6-promise').polyfill();
2 个解决方案
#1
1
The output mentions babel-node
, which means that babel
is running as a require hook, compiling your ES6 to ES5 on the fly. You need to include a shim for native promises, and expose it as global.Promise
. I'd recommend this one: https://github.com/jakearchibald/es6-promise
输出提到了babel-node,这意味着babel作为require钩子运行,在运行时将ES6编译为ES5。您需要为本机承诺包含一个垫片,并将其公开为global.Promise。我推荐这个:https://github.com/jakearchibald/es6-promise
#2
0
replace jest scriptPreprocessor
with babel-jest should work fine for all es6 comparability issues
用babel-jest替换jest scriptPreprocessor应该适用于所有es6可比性问题
#1
1
The output mentions babel-node
, which means that babel
is running as a require hook, compiling your ES6 to ES5 on the fly. You need to include a shim for native promises, and expose it as global.Promise
. I'd recommend this one: https://github.com/jakearchibald/es6-promise
输出提到了babel-node,这意味着babel作为require钩子运行,在运行时将ES6编译为ES5。您需要为本机承诺包含一个垫片,并将其公开为global.Promise。我推荐这个:https://github.com/jakearchibald/es6-promise
#2
0
replace jest scriptPreprocessor
with babel-jest should work fine for all es6 comparability issues
用babel-jest替换jest scriptPreprocessor应该适用于所有es6可比性问题