Mano
Mano

Reputation: 2110

Testcafe - How to run code after all the fixtures are run

I want to create a snapshot of the SQL server DB and then restore it after "all the fixtures" are run.

I can do it after every fixture through .after hook in a fixture. However, that is causing issues while running tests since the DB may be still in transition after a restore. So I would prefer to do it after all the fixtures.

Upvotes: 2

Views: 1522

Answers (3)

boy
boy

Reputation: 483

Since you are usually running your tests in order, you could also make a fixture, call it 99_cleanup and do your cleanup there.

Upvotes: 0

Alex Skorkin
Alex Skorkin

Reputation: 4274

You can also use the TestCafe Programming Interface API. The TestCafe Runner class returns a Promise object. You can use this object to run your custom cleanup code after all tests/fixtures are completed.

Here's an example:  

const createTestCafe = require('testcafe');
let testcafe         = null;

createTestCafe('localhost', 1337, 1338)
    .then(tc => {
        testcafe     = tc;
        const runner = testcafe.createRunner();

        return runner
            .src(['tests/fixture1.js', 'tests/fixture2.js', 'tests/fixture3.js'])
            .browsers(['chrome', 'safari'])
            .run();
    })
    .then(failedCount => {
        // Clean up your database here...
        testcafe.close();
    });

Upvotes: 1

Mano
Mano

Reputation: 2110

I have found a workaround for now. The workaround is:

  1. Add dependency for ts-node. I also had to add tsconfig.json with compiler options' lib property set to es2015 and dom. If not, it was complaining about Promise.
  2. I created a file called create-snapshot and created the snapshot there.
  3. I created another file called restore-snapshot and restored the snapshot in that file.
  4. I added two entries in package.json's scripts like "create-ss": "ts-node ./create-snapshot.ts", "restore-ss": "ts-node ./restore-snapshot.ts"
  5. Now from Powershell, I run the tests with the command: npm run create-ss;npm run test-chrome-hl;npm run restore-ss. This runs the commands sequentially in Powershell. In other terminals you may need to use && or something else instead of ;.

I can avoid "npm run create-ss" by using the .before hook of the fixture by keeping track of a variable to ensure it runs only once. However I cannot do a similar approach when the last test gets executed.

Its a bit of a pain to remember the three commands but I dont see another way so far.

Upvotes: 1

Related Questions