2013-10-02 19 views
5

से चाई और टाइपस्क्रिप्ट का उपयोग करके एक मोचा परीक्षण चला रहा है, मैं कमांड लाइन पर चलते समय टाइपस्क्रिप्ट, मोचा और चाई एक साथ काम करने की कोशिश कर रहा हूं। मैं टाइपस्क्रिप्ट संस्करण 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__; 

मुझे विश्वास है कि टाइपप्रति एक मॉड्यूल यहाँ पैदा कर रहा है, के बाद से मेरे पास है आयात विवरण

उत्तर

5

मुझे लगता है कि आप निश्चित रूप से chai.d.ts का उपयोग कर रहे हैं।

चूंकि आप बाहरी मॉड्यूल (आयात के माध्यम से) के रूप में चाई का उपयोग कर रहे हैं, तो आपको .d.ts फ़ाइल को संशोधित करने की आवश्यकता होगी। बदले

declare module chai {

declare module 'chai' {

तब करने के लिए आप यह लिख सकते हैं और सब कुछ काम करना चाहिए:

import chai = require('chai'); 

आप एक वेबपेज में इस का उपयोग करना चाहते हैं, तो आप ' वेब के मुकाबले NeedJS का उपयोग करना होगा और नोड (--module commonjs) के लिए अलग-अलग संकलन करना होगा (--module amd)।

+0

धन्यवाद, कि मुझे एक कदम आगे है, लेकिन अद्यतन प्रश्न देखें। –

+0

आपको '- मॉड्यूल आमंज 'के साथ संकलित करने की आवश्यकता है, न कि' - मॉड्यूल amd' –

+1

मैं chai.d.ts फ़ाइल को अद्यतन करने के खिलाफ सलाह दूंगा। अधिकांश परियोजनाओं में, टाइपस्क्रिप्ट परिभाषा फाइलें '' tsd'' (आजकल '' टाइपिंग'') जैसी किसी चीज़ के माध्यम से प्रबंधित होती हैं। इसलिए, अन्य प्रोजेक्ट सदस्यों को मैन्युअल रूप से प्रत्येक रिपो पुल पर इसे अपडेट करने की संभावना है। इसके बजाए, मुझे सलाह है कि आप अपने रेपो के भीतर एक अलग '' .d.ts'' रखें और वहां कोई अतिरिक्त मॉड्यूल परिभाषित करें। यह मैन्युअल रूप से प्रबंधित करने से रोक देगा आप नई रेपो खींच पर निर्भरताएं हैं। बेशक आप इस जवाब से कोड में जोड़ सकते हैं, जो बिल्कुल ठीक है। – Nicky

0

रन टाइपप्रति करने के लिए एक पूरा सेटअप के लिए मेरे उदाहरण लें मोचा और चाय के साथ परीक्षण:

test.ts

import { expect } from 'chai'; 
import 'ts-node'; 
import 'mocha'; 

describe('#MyDummyTest',() => { 
    it('sould convert be true!',() => { 
     const result = true; 
     expect(result).to.equal(true); 
    }); 
}); 

packaje.json

"devDependencies": { 
    "@angular/cli": "^1.4.9", 
    "@types/chai": "^4.0.4", 
    "@types/mocha": "^2.2.44", 
    "chai": "^4.1.2", 
    "mocha": "^3.5.3", 
    "ts-node": "^3.3.0", 
    "typings": "^2.1.1" 
}, 
"scripts": { 
    "test": "mocha --compilers ts:ts-node/register,tsx:ts-node/register --reporter spec test/test.ts" 
    "test-w": "mocha --compilers ts:ts-node/register,tsx:ts-node/register --watch --reporter spec test/test.ts" 
} 

tsconfig.j बेटा

"compilerOptions": { 
    "types": [ "mocha", "chai" ], 
    "typeRoots": [ 
     "./node_modules/@types" 
    ] 
} 

इस सूची में आप भी मदद कर सकता है:

npm install -g ts-node 
    npm install -g typescript 

Install type definitions: 
    npm install typings -g 
    npm install typings --save-dev 

Install type definitions: 
    npm install @types/chai 
    npm install @types/mocha --save-dev 

Install type definitions: 
    typings install dt~mocha --global --save 
    typings install npm~chai --save 

Install reporter: 
    npm install ts-node --save-dev 

Install ts mocha: 
    npm i -g ts-mocha