[WP] 15.03.2023 part3 | blocking code

 

fig 1.1 node process lifecycle including blocking code

-----------------------------------------------------------------------

Blocking code is an operation in javascript/node that takes long time and thus make other tasks unable to process while it is carried out. We may take care of blocking code with async functions (it immediately returns a promise and asynchronously perform operation and enable other tasks to happen or child processes(not appropriate if there is a possibility of user creating numerous child processes. .then method lets promise to resolve – you provide block of code to be executed when long running process signalized with particular Promise ends and value is provided. .catch method is used to handle any errors thrown by long-running process.


In the last paragraph I want to write about api testing with jest(testing framework) and supertest(integration tests). To install both tools type following commands prefix npm i : jest, supertest, @types/jest, @types/supertest, ts-jestUnit testing is testing individual, isolated blocks of code(eg. returned value of a function). Integration and end-to-end testing involves testing a system of abstraction(eg http request test with supertest). Pure function with no closures and dependencies are easier to test. All testable blocks of code need to be exported. describe(msg, cb) is a function describing a test suite(set of tests) in jest. Message should describe test suite. Handler includes all unit tests introduced by it(msg, cb). msg contain information what kind of behaviour is expected from tested function by a particular test. it function callback contain assertions such as: expect(return_value).toBe(1). Object.is -is an example of equality test. Integration test was done on entire api route using jest and supertest together. Test flow is : make a request, observe response from the API and make assertions on a request result.Request is done by super test. Assertions eg expect(res.headers[„Content-Type”].toMatch(/json). Cleanup should be considered for each test(.beforeAll, .afterAll, .beforeEach, .afterEach). Every test is to be stateless and not relying on the other test.


getting better

g-marcin

Komentarze

Popularne posty z tego bloga

[WP] 23.06.2023 | subjects

15.07.2024 | simplicity

[WP] 05.06.2023 | angular-intro