Is it possible to add somewhere a `before(:each)` hook so that all spec file can run it?
In the spec_helper.rb
:
RSpec.configure do |config|
#your other config
config.before(:each) do
#your code here
end
end
There is much configuration available. For instance: config.before(:each, :type => [:routing, :controller, :request])
You can even create your own tags and associate code to it:
config.around :each, unobstrusive: true do |example|
Capybara.current_driver = :rack_test
example.run
Capybara.current_driver = :selenium
end
Jest beforeAll() share between multiple test files
If you're using Jest >=20, you might want to look into creating a custom jest-environment
for the tests that require this common setup. This would be a module that extends either jest-environment-node
or jest-environment-jsdom
, and implements async setup()
, async teardown()
, and async runScript()
to do this setup work.
You can then add a @jest-environment my-custom-env
directive to those files that require this setup.
See the Jest config docs for testEnvironment
for details on how to set this up; there's a simple example there.
How to pass variable from beforeEach hook to tests in jest?
Just declare sandbox so it is available in the scope of beforeEach and test:
let sandbox;
beforeEach(async () => {
sandbox = sinon.sandbox.create()
...
})
test('/add', () => {
// sandbox available for use
})
How to seed the test database before to run Cucumber features?
Rails has a feature called Fixtures which prepopulates the test database before testing. Fixtures uses YAML to seed a table of the same name with data.
The Ruby on Rails guides have a low down on fixtures that may be beneficial to have a look at.
How can I make Mocha load a helper.js file that defines global hooks or utilities?
Mocha does not have any notion of a special file named helper.js
that it would load before other files.
What you are trying to do works when you run mocha --recursive
because of the order in which Mocha happens to load your files. Because helper.js
is one level higher than the other files, it is loaded first. When you specify an individual file to Mocha, then Mocha just loads this file and, as you discovered, your helper.js
file is not loaded at all.
So what you want to do is load a file such that it will set top level ("global") hooks (e.g. before
, after
, etc.). Options:
You could use Mocha programmatically and feed it the files in the order you want.
You could force yourself to always specify your helper file on the command line first before you list any other file. (I would not do this, but it is possible.)
Another option would be to organize your suite like I've detailed in this answer. Basically, you have one "top level" file that loads the rest of the suite into it. With this method you'd lose the ability of running Mocha on individual files, but you could use
--grep
to select what is being run.
You cannot use the -r
option. It loads a module before running the suite but, unfortunately, the loaded module does not have access to any of the testing interface that Mocha makes available to your tests so it cannot set hooks.
Related Topics
What Are Some Good Role Authorization Solutions Used with Authlogic
Ruby -V Dyld: Library Not Loaded: /Usr/Local/Lib/Libgmp.10.Dylib
The Program 'Rails' Can Be Found in the Following Packages - Issue with Some App and Rvm
Rails: Wkhtmltopdf Runtimeerror (Location of Wkhtmltopdf Unknown)
Detecting Overlapping Ranges in Ruby
Bitwise Operations on Strings with Ruby
Is There a Solution to Bypass 'Can't Add a New Key into Hash During Iteration (Runtimeerror)'
Install Ruby 2.2 on MAC Osx Catalina with Ruby-Install
Is Subclassing a User Model Really Bad to Do in Rails
Authlogic Perishable_Token Resets on Every Request
How to Get a List of Gems That Are Installed That Have Native Extensions
How to Use Regex for Utf8 in Ruby
Pg_Config, Ruby Pg, Postgresql 9.0 Problem After Upgrade, Centos 5
Alphabetize Arabic and Japanese Text That Is in Unicode
How Get Best Performance Rails Requests Parallel Sidekiq Worker