Diullei / ts-boilerplate

A boilerplate for building TypeScript applications.
MIT License
1 stars 0 forks source link

How use module reference in specs ? #1

Closed AbraaoAlves closed 10 years ago

AbraaoAlves commented 10 years ago

I put a new file, 'mainSpec.ts', inside folder 'specs' with this content:

/// <reference path="../../typings/tsd.d.ts" />
/// <reference path="../../src/_reference.ts" />

import main = require('main'); // ? How Do this ?

describe('a test example', () => {

    it('it example', () => {
        expect(main.teste()).toEqual('testando');
    });
});

Follow directives of typescript documentation about Global / External-agnostic Libraries:

/// <reference path="_reference.ts" />

module main{
    export var teste = function teste() {
        return 'testando';
    }; 
}

declare module 'main'{
    export = main;
}

When I run command line, I get:

Exception loading : c:\Users\abraaoalves...\ts-boilerplate-master-nodejs\test\specs\mainSpec.js { [Error: Cannot find module 'main'] code: 'MODULE_NOT_FOUND' }

Diullei commented 10 years ago

Hi @AbraaoAlves,

The code snippet: import main = require('main'); when compiled will be changed to: var main = require('main');. With this, node.js will looking for a module installed via npm. In this case, you will need to put the relative path to main.ts file: var main = require('../../src/main');

Let's change the main.ts file to:

/// <reference path="_reference.ts" />

export module main {
    export var teste = function teste() {
        return 'testando';
    };
}

And the file mainSpec.ts to:

/// <reference path="../../typings/tsd.d.ts" />
/// <reference path="../../src/_reference.ts" />

import main = require('../../src/main');

describe('a test example', () => {

    it('it example', () => {
        expect(main.main.teste()).toEqual('testando');
    });
});

Now you can execute the tests.

To avoid the main.main.teste() we can change the main.ts file to:

/// <reference path="_reference.ts" />

module main {
    export var teste = function teste() {
        return 'testando';
    };
}

export = main;

Let me know if this working good to you. I'll improve the README with more instructions. I need more time to do it :)

AbraaoAlves commented 10 years ago

Ok, this work ! But what is wrong with Global / External-agnostic Libraries declaration ? This should be independent of module use ?

Diullei commented 10 years ago

You will use external modules if you are building a type declaration to an existent npm module (if using commonjs/node.js) or if you are using an AMD module that has been configured with a specific name.

Example:

If you are using node.js and have the underscore npm module installed you can use the following type declaration to load the underscore module:

declare module "underscore" {
    //...
}

And load this with:

import _ = require("underscore");

If you are using an AMD structure like RequireJS, you will use the same type declaration above.

The external module exists to supply the CommonJS and AMD specifications. Take a look at this post: https://typescript.codeplex.com/wikipage?title=Modules%20in%20TypeScript looking for "Going External" topic.

AbraaoAlves commented 10 years ago

Ô cabra machu arretado ! Tu merece um caminhão carregado de coco babão, machu véi.

translate cearence -> english:

Thanks a lot for your help.

Diullei commented 10 years ago

kkkkkkkkkkkkkk... vlw brother! :)

Diullei commented 10 years ago

@AbraaoAlves Just to you know, I updated the README with some instructions and changed the exampleSpec.ts to load the main module ;)