microsoft / nodejstools

Node.js Tools for Visual Studio
http://aka.ms/explorentvs
Apache License 2.0
1.8k stars 359 forks source link

Mocha "before all" hooks run before each test when running from Test Explorer #1604

Open ArcanoxDragon opened 7 years ago

ArcanoxDragon commented 7 years ago
Expected Behavior

When running Mocha tests from the VS2017 Test Explorer window, any "before all" hooks should be run once for the entire session (and any state created by the hooks should persist across all tests). Likewise, any "after all" hooks should only run once all tests are complete.

Actual Behavior

Firstly, global hooks are not discovered automatically unless the file describing the hooks is required by all test files. Secondly, any global "before all" and "after all" hooks end up running once per test as opposed to once per session. In my project, I am using a global "before all" hook to call Gulp and compile my TypeScript files before testing. When running from the command line, Gulp runs once before the testing session and then all the tests run. When running from NTVS' Test Explorer adapter, however, Gulp runs before each described test, causing the duration of each test to be upwards of 20 seconds as opposed to the milliseconds they take on the command line.

Steps to Reproduce
  1. Set up a test file which describes at least two sample tests
  2. Set up a global hooks file alongside the test file which defines a global before hook
  3. Run mocha from the command line to observe that the hook runs once followed by both tests (proper behavior)
  4. Run all tests from the Visual Studio Test Explorer to observe first that the two tests run without the hook being run at all
  5. Have the test file require the hook file
  6. Run all tests again from the Visual Studio Test Explorer and observe that the hook is now run twice, once before each test
ozyx commented 7 years ago

Could you share an example test file? I recently worked on fixing a similar issue. Remember that before() hook runs before each test suite, and describe() defines a test suite. Are you defining tests using it()?

ArcanoxDragon commented 7 years ago

I'm setting up a sample project now...of course I can't get the Test Explorer to find any tests now, even though as far as I can tell, it's set up exactly the same as my real project.

ozyx commented 7 years ago

Check to make sure that TestFramework for your test file is set to Mocha and also that Mocha package is installed locally.

image

ArcanoxDragon commented 7 years ago

Double checked those...it seems to be a completely separate issue with finding the NodeJsTools.targets file. I'm investigating now.

ArcanoxDragon commented 7 years ago

@ozyx check out my test repo here.

If you npm install and then npm test from the command line, you'll see that the tests run and pass just fine. If you try and run them from Test Explorer, however, you'll get an error that should is not defined because the runner does not run the 0-hooks.js file before running the tests as it should (and as Mocha does when running from the CLI).

Now, uncomment the first line of both 1-values.js and 2-functions.js so that each of those files requires the 0-hooks.js file. Now the tests run and pass fine, but if you look at the Output of each test, the before() and after() global hooks run before each test (not even each suite, but each test).

When running from the CLI, they only run once per session, even though there are multiple suites (describes) and tests (its) in that session.