let message;
message = 'test';
let endsWithM = (<string>message).endsWith('m');
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm install those first | |
const nock = require("nock"); | |
const axios = require("axios"); | |
nock("http://www.example.com") | |
.post("/items") | |
.reply(201, "user created"); | |
axios | |
.post("http://www.example.com/items", { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require("axios"); | |
const postTo = "https://jsonplaceholder.typicode.com/posts"; | |
const patchTo = "https://jsonplaceholder.typicode.com/posts/2"; // todo: make the patch endpoint dynamic | |
const itemsToBePosted = [ | |
{ title: "foo", body: "bar", userId: 1 }, | |
{ title: "foo", body: "bar", userId: 2 }, | |
{ title: "foo", body: "bar", userId: 3 }, | |
{ title: "foo", body: "bar", userId: 4 } |
According to The Mostly Adequate Guide
- changing the file system
- inserting a record into a database
- making an http call
- mutations
- printing to the screen / logging
- obtaining user input
How to determine what this refers to:
1. Check where the function is being called! Then:
- Is there a dot and an object preceding that dot. If yes, this will refer to that object
- Is call or apply or bind used. If yes, this will refer to the first argument which was passed to those methods
- Do you see the usage of the new keyword. If so, this will refer to the newly created object
- Is this inside an arrow function. If yes, this will refer to the this of the parent function. Check the parent/enclosing scope
- If none from above, this will refer to the global object. If in strict mode, this will be undefined
In order to properly handle async code, you can use the following approaches:
- use the done callback
- return the promise(if any of course)
- use async/await: make sure the callback that you're passing to the it/test function is "asynced"
- in order to test async code which should fail, you must use try/catch or switch to the second appoach listed here and use the catch method on the promise that had been rejected;