से चाई और टाइपस्क्रिप्ट का उपयोग करके एक मोचा परीक्षण चला रहा है, मैं कमांड लाइन पर चलते समय टाइपस्क्रिप्ट, मोचा और चाई एक साथ काम करने की कोशिश कर रहा हूं। मैं टाइपस्क्रिप्ट संस्करण 0.9.1.1 का उपयोग कर रहा हूँ।कमांड लाइन
/// <reference path="../definitions/mocha.d.ts" />
/// <reference path="../definitions/chai.d.ts" />
// import chai = require('node_modules/chai/chai');
var expect = chai.expect;
describe("Calculator",() => {
var calc: Calculator;
beforeEach(() => {
calc = new Calculator();
});
describe("Add",() => {
it("should have correct results",() => {
calc.add(1);
calc.add(2);
expect(calc.current()).to.equal(3);
});
it("this test should fail",() => {
expect(calc.current()).to.equal(10000);
});
})
});
मैं भी एक अलग Calculator.js फ़ाइल है:
मैं CalculatorTest.ts है।
मैं पृष्ठ के साथ ब्राउज़र में इस ठीक चला सकते हैं:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Mocha Calculator Tests</title>
<link rel="stylesheet" href="scripts/node_modules/mocha/mocha.css" />
</head>
<body>
<div id="mocha"></div>
<script src="scripts/node_modules/mocha/mocha.js"></script>
<script src="scripts/node_modules/chai/chai.js"></script>
<script>mocha.setup('bdd')</script>
<script src="scripts/Calculator.js"></script>
<script src="scripts/test/CalculatorTest.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>
</body>
</html>
हालांकि, अगर मैं
./node_modules/mocha/bin/mocha
या
./node_modules/mocha/bin/mocha -r chai
साथ कमांड लाइन पर चलाने का प्रयास मुझे त्रुटि मिलती है:
C:\javascript\Test\Test\scripts\test\CalculatorTest.
js:4
var expect = chai.expect;
^
ReferenceError: chai is not defined
at Object.<anonymous> (C:\javascript\Test\Test\scripts\test\CalculatorTest.js:4:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
क्या कोई तरीका है कि मुझे चाई ऑब्जेक्ट देखना चाहिए?
अद्यतन: यहाँ क्या होता है जब आयात uncommented है के बारे में अधिक जानकारी है:
अगर मैं DefinitelyTyped से chai.d.ts बदलें कि वह
declare module 'chai' {
शुरू होता है और शीर्ष बदल फ़ाइल में कुछ पंक्तियां:
/// <reference path="../definitions/mocha.d.ts" />
import chai = require('../definitions/chai');
var expect = chai.expect;
तब फ़ाइल सफलतापूर्वक संकलित होती है।
हालांकि, जब मैं कमांड लाइन से चलाने के लिए, मैं
% ./node_modules/mocha/bin/mocha
C:\javascript\Test\Test\scripts\test\CalculatorTest.js:2
define(["require", "exports", '../definitions/chai'], function(require, export
^
ReferenceError: define is not defined
at Object.<anonymous> (C:\javascript\Test\Test\scripts\test\CalculatorTest.js:2:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
मिल उत्पन्न JavaScript फ़ाइल के साथ शुरू होता:
/// <reference path="../definitions/mocha.d.ts" />
define(["require", "exports", '../definitions/chai'], function(require, exports, __chai__) {
var chai = __chai__;
मुझे विश्वास है कि टाइपप्रति एक मॉड्यूल यहाँ पैदा कर रहा है, के बाद से मेरे पास है आयात विवरण
धन्यवाद, कि मुझे एक कदम आगे है, लेकिन अद्यतन प्रश्न देखें। –
आपको '- मॉड्यूल आमंज 'के साथ संकलित करने की आवश्यकता है, न कि' - मॉड्यूल amd' –
मैं chai.d.ts फ़ाइल को अद्यतन करने के खिलाफ सलाह दूंगा। अधिकांश परियोजनाओं में, टाइपस्क्रिप्ट परिभाषा फाइलें '' tsd'' (आजकल '' टाइपिंग'') जैसी किसी चीज़ के माध्यम से प्रबंधित होती हैं। इसलिए, अन्य प्रोजेक्ट सदस्यों को मैन्युअल रूप से प्रत्येक रिपो पुल पर इसे अपडेट करने की संभावना है। इसके बजाए, मुझे सलाह है कि आप अपने रेपो के भीतर एक अलग '' .d.ts'' रखें और वहां कोई अतिरिक्त मॉड्यूल परिभाषित करें। यह मैन्युअल रूप से प्रबंधित करने से रोक देगा आप नई रेपो खींच पर निर्भरताएं हैं। बेशक आप इस जवाब से कोड में जोड़ सकते हैं, जो बिल्कुल ठीक है। –
Nicky