找不到名称“模块”
我想运行一个简单的打字稿文件,我正在导出一个名为sum
的函数,如下所示:
我正在编写节点脚本。
function sum(a:number):number{ return a; } module.exports.sum=sum;
我不明白我做错了什么。
我写了这个简单的脚本来理解unit testing用例。 我想如果这个文件运行正常,那么我会去使用摩卡和柴启动基本的testing用例。
以下是我的testing代码:
"use strict" // Import chai. let chai = require('chai'), path = require('path'); chai.should(); let SampleTest = require(path.join(__dirname, '..', 'sample.js')); describe('Sampletesting', () => { describe('function sum', function(){ it('should return number', function(){ sum(1).should.equal(1); }) }) });
代码的和平对我来说工作得很好;
假设我们有一个名为math_utils.ts
的模块,它导出一个名为foo
的函数和一个名为obj
// typescript syntax for exporting export function foo(a: number): number { // whatever } export const obj = { life: 42 } const _aPrivateObjectToTheModule = {} function _aPrivateFunctionToTheModule() {}
现在我们在同一个文件夹中定义另一个文件,例如math_utils.spec.ts
,它将导入我们的模块。
import { should } from 'chai' import { foo } from './math_utils' // now we are able to call math_utils.foo() describe('foo', () => { it('should....', () => { foo(1).should.equal(1); }) })
现在,只是为了总结,在打字稿中,您可以通过这种方式导入模块成员…或导入整个模块,如下所示:
import * as chai from 'chai' import * as MathUtils from './math_utils' // now in an object called MathUtils we have every member defined with the keyword export const should = chai.should describe('foo', () => { it('should....', () => { MathUtils.foo(1).should.equal(1); }) }) describe('obj', ()=> { // ... MathUtils.obj })