I am fairly new with Typescript and was trying to set up a project following this tutorial : http://mherman.org/blog/2016/11/05/developing-a-restful-api-with-node-and-typescript/
I am at the step where we create a file named helloworld.test.ts and run npm test to see if the basic route of the api is working.
However when I run the npm test command (which do : mocha --reporter spec --compilers ts:ts-node/register 'test/**/*.test.ts') I get the following errors :
/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:312
throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset))
^
TSError: тип Unable to compile TypeScript
Cannot find type definition file for 'body-parser'. (2688)
Cannot find type definition file for 'chai'. (2688)
Cannot find type definition file for 'chai-http'. (2688)
Cannot find type definition file for 'debug'. (2688)
Cannot find type definition file for 'express'. (2688)
Cannot find type definition file for 'express-serve-static-core'. (2688)
Cannot find type definition file for 'mime'. (2688)
Cannot find type definition file for 'mocha'. (2688)
Cannot find type definition file for 'morgan'. (2688)
Cannot find type definition file for 'node'. (2688)
Cannot find type definition file for 'serve-static'. (2688)
test/helloworld.test.ts (1,24): Cannot find module 'mocha'. (2307)
test/helloworld.test.ts (2,23): Cannot find module 'chai'. (2307)
test/helloworld.test.ts (3,27): Cannot find module 'chai-http'. (2307)
test/helloworld.test.ts (5,17): Cannot find module '../src/App'. (2307)
test/helloworld.test.ts (10,1): Cannot find name 'describe'. (2304)
test/helloworld.test.ts (12,3): Cannot find name 'it'. (2304)
test/helloworld.test.ts (19,3): Cannot find name 'it'. (2304)
at getOutput (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:312:17)
at /home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:343:18
at Object.compile (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:476:19)
at Module.m._compile (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:406:44)
at Module._extensions..js (module.js:582:10)
at Object.require.extensions.(anonymous function) [as .ts] (/home/louis/Bureau/my-simple-project/node_modules/ts-node/src/index.ts:409:12)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
at Module.require (module.js:500:17)
at require (internal/module.js:20:19)
at /home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:231:27
at Array.forEach (native)
at Mocha.loadFiles (/home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:228:14)
at Mocha.run (/home/louis/Bureau/my-simple-project/node_modules/mocha/lib/mocha.js:514:10)
at Object.<anonymous> (/home/louis/Bureau/my-simple-project/node_modules/mocha/bin/_mocha:480:18)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3)
at Module.runMain (module.js:607:10)
at run (bootstrap_node.js:382:7)
at startup (bootstrap_node.js:137:9)
at bootstrap_node.js:497:3
npm ERR! Test failed. See above for more details.
Content of test/helloworld.test.ts :
import * as mocha from 'mocha';
import * as chai from 'chai';
import chaiHttp = require('chai-http');
import app from '../src/App';
chai.use(chaiHttp);
const expect = chai.expect;
describe('baseRoute', () => {
it('should be json', () => {
return chai.request(app).get('/')
.then(res => {
expect(res.type).to.eql('application/json');
});
});
it('should have a message prop', () => {
return chai.request(app).get('/')
.then(res => {
expect(res.body.message).to.eql('Hello World!');
});
});
});
Content of tsconfig.json :
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"outDir": "dist",
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules"
]
}
Content of package.json :
{
"name": "simple-project",
"version": "1.0.0",
"description": "description",
"main": "index.js",
"scripts": {
"start": "node dist/index.js",
"build": "gulp scripts",
"test": "mocha --reporter spec --compilers ts:ts-node/register 'test/**/*.test.ts'"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/body-parser": "0.0.33",
"@types/chai": "^3.4.34",
"@types/chai-http": "0.0.29",
"@types/debug": "0.0.29",
"@types/express": "^4.0.33",
"@types/mocha": "^2.2.32",
"@types/morgan": "^1.7.32",
"@types/node": "^6.0.46",
"chai": "^3.5.0",
"chai-http": "^3.0.0",
"gulp": "^3.9.1",
"gulp-sourcemaps": "^2.6.1",
"gulp-typescript": "^3.1.1",
"merge-stream": "^1.0.1",
"mocha": "^3.1.2",
"ts-node": "^1.6.1",
"typescript": "^2.0.6"
},
"dependencies": {
"body-parser": "^1.15.2",
"debug": "^2.2.0",
"express": "^4.14.0",
"morgan": "^1.7.0"
}
}
I have the same issue when cloning the repo from the tutorial (https://github.com/mjhea0/typescript-node-api).
I think the npm package are correctly installed since when I run npm start the api is correctly working.
I am not sure wether I should add some files to this post since my file are basically nearly the same as the ones in the repo.
I am using Ubuntu 16.04, my node version is 7.0.0 my npm version is 3.10.10 and the tutorial use typescript 2.
I could reproduce.
ts-nodeis outdatedwill fix it