How Is This Possible? Things Should Get Printed But They Dont
I don't understand how the console.logs not getting printed.
import { GenericRepository, getGenericRepository } from '../src/database/repository/GenericRepository';
import { start, stop } from '../src/index';
import request from 'request';
const baseUrl = 'http://localhost:3200/';
const getTable = async (tableName: string) => {
const data = {
'tableName': 'tableName'
};
const header = {
url: baseUrl + 'table',
method: 'POST',
json: true
};
console.log('hello');
request.post({
url: 'http://localhost:3200/getTable',
body: data,
json: true
}, (error, response, body) => {
console.log('getting angry');
if (error) {
console.error('error: ', error);
} else {
console.log(response);
}
});
await new Promise((resolve, reject) => {
request.post({
url: 'http://localhost:3200/getTable',
json: data
}, (error, response, body) => {
console.log('getting angry');
if (error) {
console.error('error: ', error);
reject(error);
} else {
console.log(response);
resolve('response' + response);
}
});
})
}
describe('A suite', function () {
beforeAll(async done => {
// await start()f.catch(error => { console.log(error); });
done();
});
afterAll(() => {
console.log('afterall')
});
it('contains spec with an expectation', async done => {
console.log('spec executed')
getTable('supply').then(result => {
console.log('result: ', result)
}).catch(error => {
console.log('error', error);
});
console.log('after getTable')
done();
// expect(table.length).toBeGreaterThan(0);
});
});
Neither of these are getting printed:
console.log('getting angry');
console.error('error: ', error);
console.log(response);
console.log('result: ', result);
console.log('result: ', result);
console.log(response);
what actually gets printed is:
Started
spec executed
hello
after getTable
.afterall
Please help me understand what's going on!. I tested it with postman the server is working properly. I would expect that the request would return the same result as the postman does.
Answer
it('contains spec with an expectation', async done => {
console.log('spec executed')
getTable('supply').then(result => {
console.log('result: ', result)
}).catch(error => {
console.log('error', error);
});
console.log('after getTable')
done();
// expect(table.length).toBeGreaterThan(0);
});
This code says to synchronously do all of the following: Call getTable, which returns a promise. Then schedule some stuff to be done when (if) that promise resolves. Then call done(), thus ending the test. Since the test has been ended, the async stuff in getTable doesn't happen, nor do the .then
callbacks.
Instead, you need to wait until getTable's promise is done resolving before you finish the test. Also, since you're already in an async function, you don't need to be using .then
callbacks, and don't need to use done()
since jasmine knows to wait until the async function's promise finishes. For example:
it('contains spec with an expectation', async () => {
console.log('spec executed')
try {
const result = await getTable('supply')
console.log('result: ', result)
} catch (error) {
console.log('error', error);
}
console.log('after getTable')
});
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM