Git Product home page Git Product logo

typescriptmochawebpackdemo's Introduction

How to setup testing using Typescript, Mocha, Chai, Sinon, Karma and Webpack.

Sample source code based on blog post: How to setup testing using Typescript, Mocha, Chai, Sinon, Karma and Webpack

To get started:

  1. Install NodeJS
  2. Open the command line of your choice and cd to the root directory of this repo on your machine
  3. npm install webpack -g - Installs webpack
  4. Download this repo (clone / fork)
  5. npm install - Installs packages (post install step added to package.json configuration file in order to execute tsd install after installing packages)
  6. npm test - Will execute karma start and run the tests (this step has been added to the package.json configuration file).
  7. npm run coverage - Will execute karma start and run the tests and generate a coverage report (this step has been added to the package.json configuration file).

Change Log:

  • Mar 22, 2017 - Add coverage for karma tests
  • Mar 17, 2015 - Added post install step to automatically run typings install after npm install.

typescriptmochawebpackdemo's People

Contributors

brauliodiez avatar ducksoupdev avatar pkunze avatar xmakina avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

typescriptmochawebpackdemo's Issues

Not working with webpack 2

When I updated webpack to 2.1.0-beta.28 Your template stopped working.
16 01 2017 14:21:38.644:ERROR [preprocess]: Can not load "webpack"!

Solution:

  1. Proper webpack version is 2.2.0-rc.4
  2. in webpack.config.js change to extensions: ['.ts', '.js', '.tsx', '.jsx']

`npm test` from readme results in errors

I followed the steps in the readme - had to add a .typingsrc for my proxy to get around problems at the npm install step - but at npm test, it spits out a bunch of errors:

C:\Users\Paul.Abrams\Source\Repos\tsMochaWebpackDemo>npm test

[email protected] test C:\Users\Paul.Abrams\Source\Repos\tsMoch
aWebpackDemo
karma start karma.unit.js

Hash: 1824a81e7942e27e9ca2
Version: webpack 2.7.0
Time: 110ms
webpack: Compiled successfully.
webpack: Compiling...
webpack: wait until bundle finished:

[at-loader] Using [email protected] from typescript and "tsconfig.json" from C:\U
sers\Paul.Abrams\Source\Repos\tsMochaWebpackDemo/tsconfig.json.

[at-loader] Checking started in a separate process...

[at-loader] Ok, 0.07 sec.

[at-loader] Checking started in a separate process...

[at-loader] Checking finished with 22 errors
Hash: d7a53ac7b7cf78eb9820
Version: webpack 2.7.0
Time: 250ms
Asset Size Chunks Chunk Names
test/MovieDataService.spec.ts 491 kB 0 [emitted] [big] test/MovieDataS
ervice.spec.ts
test/MoviesViewModel.spec.ts 491 kB 1 [emitted] [big] test/MoviesView
Model.spec.ts
chunk {0} test/MovieDataService.spec.ts (test/MovieDataService.spec.ts) 485 k
B [entry]
[0] .//chai/lib/chai/utils/flag.js 807 bytes {0} {1} [built]
[1] ./
/chai/lib/chai/config.js 1.46 kB {0} {1} [built]
[2] .//chai/lib/chai/utils/inspect.js 9.86 kB {0} {1} [built]
[4] ./
/jquery/dist/jquery.js 272 kB {0} {1} [built]
[5] .//assertion-error/index.js 2.43 kB {0} {1} [built]
[12] ./src/MovieDataService.ts 946 bytes {0} {1} [built]
[13] ./
/chai/index.js 40 bytes {0} {1} [built]
[14] ./test/MovieDataService.spec.ts 1.54 kB {0} [built]
[17] .//chai/lib/chai.js 1.28 kB {0} {1} [built]
[18] ./
/chai/lib/chai/assertion.js 3.79 kB {0} {1} [built]
[19] .//chai/lib/chai/core/assertions.js 51.8 kB {0} {1} [built]
[20] ./
/chai/lib/chai/interface/assert.js 40.9 kB {0} {1} [built]
[21] .//chai/lib/chai/interface/expect.js 770 bytes {0} {1} [built]
[22] ./
/chai/lib/chai/interface/should.js 5.67 kB {0} {1} [built]
[31] .//chai/lib/chai/utils/index.js 1.71 kB {0} {1} [built]
+ 29 hidden modules
chunk {1} test/MoviesViewModel.spec.ts (test/MoviesViewModel.spec.ts) 485 kB
[entry] [rendered]
[1] ./
/chai/lib/chai/config.js 1.46 kB {0} {1} [built]
[2] .//chai/lib/chai/utils/inspect.js 9.86 kB {0} {1} [built]
[4] ./
/jquery/dist/jquery.js 272 kB {0} {1} [built]
[5] .//assertion-error/index.js 2.43 kB {0} {1} [built]
[12] ./src/MovieDataService.ts 946 bytes {0} {1} [built]
[13] ./
/chai/index.js 40 bytes {0} {1} [built]
[17] .//chai/lib/chai.js 1.28 kB {0} {1} [built]
[18] ./
/chai/lib/chai/assertion.js 3.79 kB {0} {1} [built]
[19] .//chai/lib/chai/core/assertions.js 51.8 kB {0} {1} [built]
[20] ./
/chai/lib/chai/interface/assert.js 40.9 kB {0} {1} [built]
[21] .//chai/lib/chai/interface/expect.js 770 bytes {0} {1} [built]
[22] ./
/chai/lib/chai/interface/should.js 5.67 kB {0} {1} [built]
[31] ./~/chai/lib/chai/utils/index.js 1.71 kB {0} {1} [built]
[44] ./src/MoviesViewModel.ts 660 bytes {1} [built]
[45] ./test/MoviesViewModel.spec.ts 1.33 kB {1} [built]
+ 30 hidden modules

ERROR in [at-loader] ./test/MovieDataService.spec.ts:6:1
TS2304: Cannot find name 'describe'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:9:3
TS2304: Cannot find name 'describe'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:10:5
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:11:7
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:13:7
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:17:3
TS2304: Cannot find name 'describe'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:18:21
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:21:5
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:26:5
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:27:28
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MovieDataService.spec.ts:32:5
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:5:1
TS2304: Cannot find name 'describe'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:7:29
TS2503: Cannot find namespace 'Sinon'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:8:3
TS2304: Cannot find name 'beforeEach'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:9:5
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:10:28
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:14:3
TS2304: Cannot find name 'afterEach'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:15:5
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:16:5
TS2304: Cannot find name 'sinon'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:19:3
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:23:3
TS2304: Cannot find name 'it'.

ERROR in [at-loader] ./test/MoviesViewModel.spec.ts:28:3
TS2304: Cannot find name 'it'.
webpack: Failed to compile.
01 05 2018 12:10:02.546:INFO [karma]: Karma v1.7.1 server started at http://0.0.
0.0:9876/
01 05 2018 12:10:02.546:INFO [launcher]: Launching browser PhantomJS with unlimi
ted concurrency
01 05 2018 12:10:02.586:INFO [launcher]: Starting browser PhantomJS
01 05 2018 12:10:06.379:INFO [PhantomJS 2.1.1 (Windows 7.0.0)]: Connected on soc
ket _J58yTnC8RELNzsnAAAA with id 89339749
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
INFO: 'sinon.restore is deprecated and will be removed from the public API in a
future version of sinon.'
PhantomJS 2.1.1 (Windows 7.0.0): Executed 7 of 7 SUCCESS (0.02 secs / 0.012 secs
)
01 05 2018 12:10:06.705:WARN [web-server]: 404: /movies
01 05 2018 12:10:06.705:WARN [web-server]: 404: /movies
01 05 2018 12:10:06.705:WARN [web-server]: 404: /movies
01 05 2018 12:10:06.705:WARN [web-server]: 404: /movies

C:\Users\Paul.Abrams\Source\Repos\tsMochaWebpackDemo>

Link to article

Excellent project ! I think it would be a good idea to add a link your blog post just in case some body reaches the project in github.

Proposal simplify steps

We could skip having the developer to run tsd and ask node to do that automatically after each "npm install", something like adding to the package.json:

{
  "name": "...",
  "scripts": {
    "postinstall": "tsd reinstall --overwrite --save"
  },

On the other hand shouldn't be a good idea to add the steps to start from scratch? Something like:

To get started:

  1. Install NodeJS
  2. npm install webpack -g - Installs webpack
  3. 'npm install tsd -g' - Installs tsd.
  4. Download this repo
  5. Open the command line of your choice and cd to the root directory of this repo on your machine

If you find it useful but you don't have time I'm more than keen into forking and opening a branch with the proposal.

Npm install says unable to connect to raw.githubusercontent.com:443

I tried
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
npm config set strict-ssl false

but that didn't help.

Here's what eventually worked:
Add .typingsrc in the root folder with the following lines:
proxy=http://my-proxy-server:8080
rejectUnauthorized=false

But I don't have to do this for other repos.

C:\Users\Paul.Abrams\Source\Repos\tsMochaWebpackDemo>npm install

[email protected] postinstall C:\Users\Paul.Abrams\Source\Repos
\tsMochaWebpackDemo
typings install --overwrite --save

typings ERR! message Unable to connect to "https://raw.githubusercontent.com/Def
initelyTyped/DefinitelyTyped/5d0f2126c8dac8fce0ff020218aea06607213b0d/mocha/moch
a.d.ts"
typings ERR! caused by getaddrinfo EAI_AGAIN raw.githubusercontent.com:443
typings ERR!
typings ERR! cwd C:\Users\Paul.Abrams\Source\Repos\tsMochaWebpackDemo
typings ERR! system Windows_NT 6.1.7601
typings ERR! command "C:\Program Files (x86)\nodejs\node.exe" "C:\Users\Pau
l.Abrams\Source\Repos\tsMochaWebpackDemo\node_modules\typings\dist\bin.js
" "install" "--overwrite" "--save"
typings ERR! node -v v8.11.1
typings ERR! typings -v 2.1.1
typings ERR! code EUNAVAILABLE
typings ERR!
typings ERR! If you need help, you may report this error at:
typings ERR! https://github.com/typings/typings/issues
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] postinstall: typings install --overwr ite --save
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] postinstall script.
npm ERR! This is probably not a problem with npm. There is likely additional log
ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Paul.Abrams\AppData\Roaming\npm-cache_logs\2018-05-01T15_
30_44_643Z-debug.log

C:\Users\Paul.Abrams\Source\Repos\tsMochaWebpackDemo>

how to build ?

Is there a way to build with a live reload ?
Maybe add a coverage :)

Doesn't run test cases in Chrome

I added the karma-chrome-launcher as a dependency and changed the browser to Chrome..
For some reason it won't run the test cases at all.

webpack: bundle is now VALID.
23 01 2017 10:24:57.159:WARN [karma]: No captured browser, open http://localhost:9876/
23 01 2017 10:24:57.166:INFO [karma]: Karma v0.13.22 server started at http://localhost:9876/
23 01 2017 10:24:57.170:INFO [launcher]: Starting browser Chrome
23 01 2017 10:24:57.970:INFO [Chrome 55.0.2883 (Mac OS X 10.11.4)]: Connected on socket Dsem3WhljY0L371hAAAA with id 85521369
Chrome 55.0.2883 (Mac OS X 10.11.4): Executed 0 of 0 ERROR (0.003 secs / 0 secs)
23 01 2017 10:25:06.994:WARN [Chrome 55.0.2883 (Mac OS X 10.11.4)]: Disconnected (1 times)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.