mercredi 19 août 2020

How to import mocked data from shell command

I am using request mock and getting json data from external file:

import { RequestMock } from 'testcafe';
const mockData = require('abc.json');

var mock = RequestMock()
    .onRequestTo('https://api.mycorp.com/users/id/135865')
    .respond(mockData);

fixture `My fixture`
    .page `https://mycorp.com`
    .requestHooks(mock);

test('My test', async t => { /* ... */ });

Now, i am trying to import mockData from my storage system. Let's say, CLI command to pull abc.json into current directory is:

mysystem pull abc.json

I am looking for code to do so. I tried using child_process but my CLI command many take few seconds to fetch abc.json and meanwhile testcafe code executes.

import { RequestMock } from 'testcafe';

const getData = require('child_process').exec;
getData('mysystem pull abc.json', (e, stdout, stderr)=> {
    if (e instanceof Error) {
        console.error(e);
        throw e;
    }
    console.log('stdout ', stdout);
    console.log('stderr ', stderr);
});
const mockData = require('./abc.json');
    
    var mock = RequestMock()
        .onRequestTo('https://api.mycorp.com/users/id/135865')
        .respond(mockData);
    
    fixture `My fixture`
        .page `https://mycorp.com`
        .requestHooks(mock);
    
    test('My test', async t => { /* ... */ });

I am getting error, that

mock is not defined.

I can clearly see that my json gets loaded once testcafe execution finishes. So, i need a way for shell command to finish first and then testcafe code to execute.

Aucun commentaire:

Enregistrer un commentaire