问题
I'm using nodejs to create an end to end test suite for our API. Before each test runs, I need to insert database records for that test. Many of the tables in question do not use native auto-increment type fields for their primary keys.
(I know, bad database design. But I don't have control over that.)
Instead, they use the sequence pattern common to postgresql. But this is in MS SQL Server. So there is a stored procedure that gets the next sequence number for use in the table, etc.
As I begin to set up my tests, I am finding that my transactions are colliding with each other because of the asynchronous nature of javascript. Basically, the transactions are getting the same sequence numbers and then trying to commit on top of each other. So unique constraints are failing.
The first solution that comes to my mind (I'm open to others) is to just set up all of the database records before any tests run. But to my knowledge, Jasmine's beforeAll() function only applies to the file that it's in. I need a beforeAll() function that runs before all Jasmine tests run in all files everywhere.
Does Jasmine have something like that? If not, is there a way I can create a controller in nodejs that will set up the test cases in the database and then spawn jasmine programatically?
Thanks in advance!
回答1:
It looks like Jasmine can be spawned by nodejs programmatically. I found a way to do this with the following code:
... Do database setup stuff here ...
.then(() => {
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
jasmine.loadConfig({
spec_dir: 'spec'
,spec_files: [
'**/*[Ss]pec.js'
]
,helpers: [
'helpers/**/*.js'
]
,random: false
});
jasmine.execute();
});
This will work because I can put this code into a promise.then() callback and execute it after I have done database set-up work.
来源:https://stackoverflow.com/questions/49615638/how-to-run-jasmine-beforeall-for-all-test-files