Run mocha tests individually

My test/
‘s project is full of mocha
tests:

test/
├── a.coffee
└── b.coffee

Say, a.coffee
is

console.log 'Executing A test!'
global.foo = 'FOO'

and b.coffee
is

console.log 'Executing B test!'
console.log 'Expecting foo not defined:', assert.equal(foo, undefined)

When executing mocha:

$ mocha --compilers coffee:coffee-script test/*
Executing A test!
Executing B test!
Expecting foo not defined: false

It looks like tests share the same global object (which I would like to avoid)…

Is there a way to execute each test individually?

Thank you.

Problem courtesy of: abernier

Solution

Mocha’s author answered this question here: https://github.com/visionmedia/mocha/issues/365#issuecomment-4997480

Solution courtesy of: abernier

Discussion

If the code you are testing behaves differently depending on some global state, then you have to take control of that as part of the tests. You can code some “beforeEach” function that sets whatever global state you require.

You might also want to think about re-factoring your code so that it does not depend on global state. If you can pass parameters or something ( explicit vs implicit ), then you can test with more confidence.

Discussion courtesy of: Nick Perkins

Global state in general makes testing difficult. The Clean Code Talks by googletechtalks are worth watching for anyone interested in writing testable software

  1. Unit Tests
  2. Global State and Singletons
  3. Don’t Look For Things

Discussion courtesy of: Noah

This recipe can be found in it’s original form on Stack Over Flow
.

Node.js Recipes责编内容来自:Node.js Recipes (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 前端开发 » Run mocha tests individually

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录