TheBrainFamily / chimpy

Develop acceptance tests & end-to-end tests with realtime feedback.
https://thebrainfamily.github.io/chimpy
MIT License
52 stars 28 forks source link

How to run setup file before all tests #26

Open samhatoum opened 6 years ago

samhatoum commented 6 years ago

Issue by joshhunt Tuesday Apr 12, 2016 at 23:15 GMT Originally opened as https://github.com/xolvio/chimp/issues/332


Mocha has the 'require' option which allows you to run a setup file before all the tests are even required. We take this opportunity to set up some global variables and functions, which are required for the tests to be required properly.

I noticed that chimp has a similar option --require, however it does not load this file before it loads the others. I put some very basic console.log at the top of key files, which shows the order

$ chimp test/functional/chimp.js --require test/functional/setup.js --path=test/functional --mocha --browser=firefox

[chimp] Running...
modules/navigation.spec.js
features/navigation.spec.js
utils/setup.js

Because setup.js was required after our test cases, the test cases will fail to load properly because they don't have the globals set up yet.

Am I using the --require option correctly, or is there another option to do what we need?

samhatoum commented 6 years ago

Comment by Sanjo Wednesday Apr 13, 2016 at 04:45 GMT


Right now you can probably force a file to load first by prefixing the file name with . E.g. beforeAll.js. Because files are loaded in alphabetical order I think.

samhatoum commented 6 years ago

Comment by samhatoum Thursday May 12, 2016 at 19:21 GMT


@joshhunt did this work for you guys?

samhatoum commented 6 years ago

Comment by joshhunt Tuesday Jun 07, 2016 at 08:49 GMT


Sorry for dropping off the grid - I'll check this out tomorrow and report back!

samhatoum commented 6 years ago

Comment by montogeek Tuesday Aug 02, 2016 at 22:44 GMT


@joshhunt are you back?

samhatoum commented 6 years ago

Comment by tiagolr Friday Mar 17, 2017 at 21:45 GMT


I am using two different options for this:

1 - run code in chimp.config.js that executes in chimp process before tests start 2 - run code in this.BeforeFeatures(...) that executes in cucumber process before tests start

In your case the first option should work well, I'm using it too to setup global variables.

Here is a pasteBin of my config file: http://pastebin.com/fXeVVTiW

edit: the configs contain a verification to prevent cucumber process from parsing the file, that has to do with the script and tests folder location.

samhatoum commented 6 years ago

Comment by paulbjensen Sunday Aug 13, 2017 at 07:19 GMT


Hi,

I tried the suggestion that @Sanjo made, and it works. I created a file named __beforeAll.test.js, and places it inside of the test/controllers folder in my app, as npm test script would load files from that directory first, e.g.

"script": {
  "test": "NODE_ENV=test mocha test/**/* test/*"
}

I needed to load the timekeeper npm module first to intercept the Date class in order to create a test relating to time, and so needed to load timekeeper before all of the application's code, as timekeeper mocks the Date class and Date.now function.

Thanks to @sanjo for the suggestion.